std::basic_istream::operator>>的行为已从C 11改变.自C 11以来,
If extraction fails, zero is written to value and failbit is set. If
extraction results in the value too large or too small to fit in
value, std::numeric_limits::max() or std::numeric_limits::min()
is written and failbit flag is set.
请注意,直到C 11,
If extraction fails (e.g. if a letter was entered where a digit is
expected), value is left unmodified and failbit is set.
string first_name;
if (!(cin>>first_name)) {
first_name = "???";
cin.clear(); //Reset stream state after failure
}
int age;
if (!(cin>>age)) {
age = -1;
cin.clear(); //Reset stream state after failure
}
cout<