Why does my cin.getline(); not work sometimes? In these circumstances, I am forced to add another cin.getline to get it to work but I really want to understand why the first one is ignored sometimes(see code below). Any assistance will be appreciated.
1 2 3 4 5 6 7 8 9 10
constint size = 50;
char locateThis[ size ];
cout << "\n\n\t\tSearch Books File\n\n\n"
<< "\t\tEnter 1 of the following:\n\n "
<< "\t\tISBN, title, author's OR publisher's name\n\n"
<< ": ";
cin.getline( locateThis, size );// does not work!
cin.getline( locateThis, size ); // why put a second one?
Sometimes when I put just one cin.getline statement, and I run the program, it is ignored and the program moves onto the nextquestion or code. I think this usually happens with just the first cin.getlne in a block of code, if I remember correctly. ANyway, in the code above, the first cin.getline is ignored and this problem is "solved " by placing another below
int num = 0;
cout << "Enter a number: ";
cin >> num;
cout << "You entered " << num << endl;
cin.get(); //this should make it pause
Unfortunately, what happens is that cin stores the number entered in the variable 'num', and the trailing newline character(s) needed to submit the entry is left in the buffer. This means that the newline needs to be discarded since it is useless:
1 2 3
cin >> num;
cin.clear(); //clear the error bits for the cin input stream
cin.sync(); //synchronize the input buffer, discarding any leftover characters in the buffer
That is one way to do it. In addition, it helps in the case of bad input polluting the input buffer because when you enter improper data (such as a float when the compiler is expecting an int), cin keeps getting skipped until the data can successfully be read (after all, a '.' can't be read by an int can it?)
Another way, as Grey Wolf mentioned, is to use cin.ignore():
cin >> num;
cin.ignore(256, '\n'); //discard up to 256 characters or the '\n', whichever comes first
This works for when you only need to discard newline characters.
> instead of using cin.getline u can use cin.gets(),parameter is stdio.h
I'm confused. What do you mean by this? stdio.h is a C header, and cin is in a C++ header. Also, it is cin.get(), not cin.gets()
Thank you ropez, Greywolf, rpgfan3233 and anuanju for your prompt replies and assistance..
Special thanks to Greywolf (your idea of placing cin.ignore() before the cin.getline() worked!) and rpgfan3233 (your detailed explaination and examples were not only helpful, but much appreciated!). Also,cin.sync() is new to me so I am going to do some more research on that.
you know, I reread your post several times and it has not only helped me with my cin.getline() problem; it has also helped me better understand the following code I use for validating user input:
1 2 3 4 5 6 7 8 9 10 11 12 13
short num = 0;
cout << "Enter a number <1-5>: ";
while (!(cin >> num) ||(num < 1 || num > 5) || cin.get() != '\n' )
cin.ignore( 1000, '\n' );
cout << "Incorrect entry\n"
<< "Please enter a number <1-5>: ";
// Note always put other validations BETWEEN (!(cin >> num) and //cin.get() != '\n')
Your post has finally helped me understand the '\n' part of that code! Thanks again