Send to a Friend

HeroicZach's avatar

Why do I need "clear" the I/O input with a dummy getline() call before I use getline() in terminal I/O?

Asked by HeroicZach (195points) October 19th, 2009

So I’m writing a C++ program, see, and it uses cin>> a lot. For some sub-parts of the program, however, I need to use getline(cin,whatIWant); to access data values. No matter what I do, the FIRST call to getline after I have already used a cin>> above that does absolutely nothing – C++ skips it as if there is data already in the terminal that it just reads. I have determined it is reading absolutely nothing with a test dummy variable. Is there any more elegant solution than:

string dummy;
getline(cin, dummy); // OK, now I can get real values now

Thanks for your help!

Using Fluther

or

Using Email

Separate multiple emails with commas.
We’ll only use these emails for this message.