Hi I was just looking up the difference between cin.get() and cin.getline().

It seems they can both function the same way except cin.get() keeps the delimiter in the buffer while cin.getline() throws it out.

My question is with this code:

char name[200];
char name1[200];
char name2[200];
string hi;

cin >> hi;
cout << hi << endl;

cin.get(name, 255);
cout << "name " << name << "!" << endl;

cin.getline(name1,255);
cout << "name1 " << name1 << "!" <<  endl;

cin.getline(name2, 255);
cout << "name2 " << name2 << "!" << endl;

From what I read on online articles the cin >> hi should input into hi one word but leave the '\n' delimiter in the butter. The cin.get(name, 255) would then skip over user input because it sees '\n' right away. Then cin.getline(name1, 255) should also roll over right away because cin.get(name, 255) would not have thrown away the delimiter.

However my issue is the first cin.getline() should have thrown away the '\n' but for some reason it also skips over the second cin.getline(name2, 255) as if there is another '\n' that it reads immediately. It works as I expected when during the cin >> hi I leave a space at the end of my input ex:"hello " but if I do "hello" it rolls over all 3 get functions.

Why does it do this? Does the cin >> hi leave more than one '\n' in the buffer?

Recommended Answers

All 6 Replies

I'll also post my test output so to better show what happens.
Input is as follows:

If I cin >> hi "hello":

hello
name: !
name1: !
name2: !

It does not ask me for input for any of the get or getlines

However if I do cin >> hi with "hello ":

hello
name:  !          //Extra space here since theres a space in the cin line.
name1: !
//Asks me for input here, I input "asd".
name2: asd!

It seems odd that the space at the end of cin >> hi makes it work as I expected, but without the whitespace it rolls over the last getline as if there is still a '\n' in the buffer which the previous getline should have thrown away.

Am I missing something really simple here?

Anyone have any information on this?

You're right, that is the proper behaviour.

If an error occurs on input operations, subsequent input operations will fail to work. So I'm guessing an error occurs when you just enter "hello" with no space.

Check for error/EOF flags with fail() or eof() methods.

For example,

char name[200];
cin.get(name, 200);    // btw, should be 200 for streamsize n (length of array)
if (cin.eof()) {
    cerr << "Input on cin is flagged with EOF" << endl;
    cin.clear(ios::eofbit);    // clear eof flag
}
if (cin.fail()) {
    cerr << "Input failure on cin object << endl;
    // clear fail and bad flags, any input operations after will work
    cin.clear(ios::failbit | ios::badbit);  
}

You can read more about flags here http://www.cplusplus.com/reference/iostream/ios_base/iostate/

Thanks for the information still a little unsure of why it causes an error. From what I read on various articles cin >> would leave a '\n' in the io buffer Which the cin.get should leave there. However the first cin.getline should throw away this buffer which should cause the last cin.getline to work ok because the default delimiter is gone.

Which error is encountered exactly for it to roll over the last cin.getline in my first posts code?

The error occurs I believe is when you call cin.get(name, 255) on line 9.

When you enter "hello" with no space on the first input, the newline \n is left. Then you call cin.get(name, 255) to get the next input. The get() function will try to process the data in the input buffer and store it in name. This does not happen because it's just a newline. So the get() function reads nothing into name and still leaves the newline in the input buffer. Because "nothing" was read into 'name' it triggers the error.

If you add the space, cin.get() reads in the space (at least) and stops at the newline. It reads a space and stores it in name. Because something was read (the space) then the input operation was successful.

The same kind of error occurs when you want an integer but get a string instead.

int age;
cin >> age;

If you enter "red", the input operation failed (get an integer from the user). That doesn't happen so it triggers the error.

Ahh Ok that makes more sense. Thank you for your help.

Tested it a bit further and I noticed only cin.get() gets this problem. cin.getline() can process just a '\n' fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.