That's where new C++ programmers get into trouble: the mental model doesn't match the language model.
Your mental model is correct in the sense that whenever a user is asked to input information he (the user!) expects that he is to press ENTER at the end of every input.
However, the >> operator is whitespace-delimited, not line-delimited. This leads to unexpected failures. For example, Ms. Zappa could answer the question with
Moon Unit Zappa
whereafter 'firstName' would be "Moon" and 'surName' would be "Unit" and "<SPACE>Zappa<ENTER>" would be, as yet, unread. This is, of course, incorrect.
This is not an error in the design of the STL, but rather an error in using it properly. Remember, the >> operator is used to separate information out of a stream. It is not meant to be used for direct user input.
To be correct, we must first fix our program to match the user's mental model:
1 2 3 4 5 6
|
string firstName, surName;
cout << "Please enter your first name(s): ";
getline( cin, firstName );
cout << "Please enter your last name(s): ";
getline( cin, surName );
|
In this case, we are getting input by line, rather than by whitespace. Make sense?
So, what about numbers?
I prefer to read everything in as a string, then convert it to a number (or whatever the appropriate object is) if possible:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
string s;
unsigned age;
cout << "Please enter your age: ";
getline( cin, s );
{
// Try to convert whatever the user typed into an unsigned integer.
streamstring ss( s );
ss >> age;
if (!ss)
{
cout << "Alas, \"" << s << "\" is not a valid number.\n";
return 1;
}
}
// At this point, I know that 'age' is correctly entered.
if (age < 4) cout << "Precocious scamp, ain't ya?\n";
else if (age > 120) cout << "Yeah, right...\n";
else if ...
|
The other way (and I'm almost out of time here) is to simply get and discard that ENTER key (and any other garbage in front of it) after reading what you want:
1 2 3 4
|
unsigned age;
cout << "How old are you? ";
cin >> age;
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
|
Hope this helps.