Getline Malfunction

I'm back again with another question -_-
sorry for asking too much in this single project but i ask these questions because for me, these are "no way out" questions

this is the original code fragment in my program
1
2
3
cout << "Enter the name of the movie you want to enter: "; cin >> Mname;
cout << "Enter the year when the movie was shown: "; cin >> Myear;
insert2(Mname, Myear);

and the output was

Enter the name of the movie you want to enter: taken
Enter the year when the movie was shown: 2008


but since not all movies has only one word... i knew cin won't work. so i changed the code to this
1
2
3
cout << "Enter the name of the movie you want to enter: "; getline(cin, Mname);
cout << "Enter the year when the movie was shown: "; cin >> Myear;
insert2(Mname, Myear);

notice in line 1 that i changed cin to getline(cin, Mname) in order to store movies that has a title of more than one word

but things become worse because the output of the changed code was this

Enter the name of the movie you want to enter: Enter the year of the movie was shown: 2008


the program doesn't ask for the movie title anymore and it skips the getline(cin, Mname) part. i was very sure that i typed #include<string> at the very beginning of the program and cin >> Mname was the only code i changed

I wonder where i went wrong? :(

Note: this is only a tiny fragment of my program. if you need to see the rest pls feel free to ask
The streams get a bit funky when you mix getline and the >> operator for cin. I would advise using one or the other. In fact, I would advise solely using getline.

Try putting this before getline:
1
2
cin.ignore( 256, '\n' );
cin.clear();


Ignore gets rid of any junk sitting in the buffer. This can include residual values from its last use. Clear will reset any error flags. The latter probably isn't totally necessary but I tend to include it.
Last edited on
Ah, the clumsiness of iostream.


What's happening is this:

- The >> operator ignores leading whitespace. Then it extracts the data, and leaves following whitespace in the buffer.

So if you input 2 values, the buffer looks like this: "movie\nyear\n" Where '\n' is the newline when the user presses enter.

The first >> operator extracts the "movie", leaving this in the buffer: "\nyear\n".

The second >> operator throws away whitespace until it finds non-whitespace data, then extracts it. So it'll pull out "\nyear", but will throw that first newline away. Leaving "\n" in the buffer.



The problem now is that getline does NOT throw away the leading whitespace. So if you do >> followed by a getline, the >> will leave a newline character in the buffer, which the getline will see and think the user input a blank line.


The solution is to wipe the buffer before you getline() so any trailing whitespace is cleared out. You can do this with ignore:

1
2
cin.ignore(100, '\n');  // discard the next 100 characters in the buffer...
  // or discard up to the next newline character... whichever happens first) 



EDIT: ninja'd by iHutch
Last edited on
ooooh~ it worked :D tnx guys

can i ask a follow up question? i'm new to this. what's with the number under the parenthesis of cin.ignore(). do i have to always watch for it? do i always need to type the exact number in each of my program if this error appears? or just type the number 256 or 100 when i encounter this kind of error? should i think about the number or should i just type 256/100? o.O
See my code snippit again.. I explained what the number is:

1
2
cin.ignore(100, '\n');  // discard the next 100 characters in the buffer...
  // or discard up to the next newline character... whichever happens first) 


The number is just the maximum number of characters to throw away. So if you use 10 for the number... it'll only throw away 10 characters from the buffer. This will work fine for most situations, but if the user is stupid and puts in a bunch of spaces after their input, you might not throw all of them out.

For example... if the user inputs "1234" followed by 20 spaces.
When you do a >> operation, it'll extract the 1234, leaving 20 spaces plus a newline in the buffer.

If you do cin.ignore(100,'\n'), it'll remove all that data from the buffer and throw it away. Then the following getline() will prompt the user for more input.


However if you do cin.ignore(10,'\n'), it'll only remove 10 of the spaces, leaving 10 spaces and a newline in the buffer. Then the following getline() will take those 10 spaces and treat it as the data the user input.





TLDR;

For your purposes, "bigger is better". Using larger numbers won't hurt you here.
Last edited on
oh I get it now :) tnx.... i think i'll go for a bigger number if i encounter this type of error again. no harm will be done :)
Last edited on
Topic archived. No new replies allowed.