cin's operator>> uses whitespace as a delimiter:
string s;
cin>> s; // Type "John Doe"
cout<< s; // Prints "John". "Doe" left in the stream
>I tried flushing the input buffer with cout.flush()
What made you think that calling the flush member function of an output stream would somehow "flush" the input stream? I think you're confused about the definition of flush, and the relationship of cin and cout.
To flush a stream is to write the contents of the stream buffer to the external device for an output stream. This in no way implies that flushing an input stream will somehow discard the contents of the stream. In fact, basic_istream doesn't even have a flush member function because it's nonsensical.
cin and cout are related, but only because they are tied together so that any request for input using cin will automatically flush cout. Notice that any unwritten characters in
cout's stream buffer are
written to the
output device. No part of this has an effect on cin's stream buffer.
So calling cout.flush() will force the stream buffer for cout to be written to the output device. It won't affect cin in any way. The proper way to "flush" the input stream is with basic_istream's ignore member function:
cin.ignore(); // Discard one character
cin.ignore(80); // Discard up to 80 characters or EOF
cin.ignore(80, '\n'); // Discard up to 80 characters or a newline
A good way to "flush" the input stream without using arbitrary magic numbers is using numeric_limits, defined in <limits>:
cin.ignore(numeric_limits<streamsize>::max(), '\n');
>What am I to do here?
The general rule of thumb is that if you want to use whitespace as a separater, use operator>>. Otherwise use getline.
>you might be able to put " " around what you're typing
Then again, you might not. How the shell works is beyond the scope of C++, so it's hard to guarantee a correct and portable program when you rely on it.