display | more...
First of all, for you c++ guru's stop laughing and move on. This is for us new guys. :)

So you make your loop and figure you'll take input using a good old getline:


You run your code and sit there dumb founded as it flys by you on the screen. After several minutes of tinkering you finally give up and use another method of getting the input such as gets.

The problem is obvious and the solution quite simple, after someone tells you. :)
Clear the buffer....

Yep, that's right just clear the buffer:

char ch;             //declare empty char variable

cin.get(ch); //clear buffer
This problem usually only manifests itself in loops but sometimes it sneaks into other areas. The strange part is that it doesn't need to be cleared after each .getline, just once at the beginning of the loop. Apparently the buffer gets the '\n' escape character stuck in it and that causes the annoying problem.

I'm not the only one who's likely to have run into this problem and it can be a bitch, especially since I've yet to find a book that says anything about it.

I'd like to take this opportunity to thank my C++ teacher for letting me and the rest of the class in on this little piece of knowledge.

Log in or register to write something here or to contact authors.