cin.getline() not working

Hello,

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
const int 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?
                 
Last edited on
You'll need to explain what you mean by "doesn't work".

What do you expect to happen, and what does actually happen?
Try flushing the stream (with something like cin.ignore();) before you call cin.getline().

It’s just a hunch that there may be a ‘\n’ left in the stream from another part of your program.
Last edited on
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
Try Grey Wolf's suggestion. Consider this:
1
2
3
4
5
6
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():
1
2
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
anuanju wrote:
> 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.

Best regards to all
Last edited on
rpgfan3233,
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.clear();
          
          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
Last edited on
Topic archived. No new replies allowed.