To obtain a delimiter terminated character sequence from an input stream.
basic_istream<charT, traits>& getline(char_type* s, streamsize n, char_type delim = traits::newline());
The unformatted getline() function retrieves character input, and stores it in a character array buffer s if possible until the following conditions evaluated in this order occur. If no characters are extracted setstate(failbit) is called.
end-of-file occurs in which case setstate(eofbit) is called.
A delimiter (default value is the newline character) is encountered. In which case the delimiter is read and extracted but not stored.
A limit (the second argument minus one) is read.
If n-1 chars are read, that failbit gets set.
In any case it stores a null char into the next successive location of the array.
The this pointer is returned.
#include <iostream> const int size = 120; int main() { using namespace std; char compiler[size]; cout << "Enter your compiler: "; cin.getline(compiler, size); cout << "You use " << compiler; return 0; }
Result:
Enter your compiler:CodeWarrior <enter> You use CodeWarrior
#include <iostream>
const int size = 120;
#define TAB '\t'
int main()
{
using namespace std;
cout << "What kind of Compiler do you use: ";
char compiler[size];
cin.getline(compiler, size,TAB);
cout << compiler;
cout << "\nsecond input not needed\n";
cin >> compiler;
cout << compiler;
return 0;
}
Result:
What kind of Compiler do you use: CodeWarrior<tab>Why? CodeWarrior second input not needed Why?