How do I display leftover characters after a whitespace-delimited-input? Or, if that's incorrectly asked, how do I know that they're hiding there and need to be ignored?
What must I do to avoid using the
cin.ignore()
? I was trying to learn how to useget()
andgetline()
. It seems they only work if I first usecin.ignore()
, which seems kind of inefficient to me.
My code prompts me for a number, and looks much like this:
int previousVar;
std::cout<<"\n\tWhat value should previousVar be?:\t";
std::cin>>previousVar;
Immediately afterwards, I added this code (which is copy-pasted from the cpp reference website):
std::string name;
//std::cin.ignore();
std::cout << "Please, enter your full name: ";
std::getline (cin, name);
std::cout << "Hello, " << name << "!\n";
cin>>beforeExit;
My code skips past the name-gathering section entirely to my end-of-program pause, unless I have the cin.ignore()
. I read on one of the cpp reference pages that I may have leftover characters from a previous cin
input. I saw a similar questions at http://www.cplusplus.com/forum/beginner/111268/ and also at difference between cin.get() and cin.getline() , but I'm not sure they're asking the same thing as I am.