When cin>>(int) and cin>>(string) are called, when the first input is not correct for integer, it seems that cin>>(string) will fail to retrieve the second input even if it is correct string.
The source code is simple as:
cout<<"Please enter count and name"<<endl;;
int count;
cin>>count; // >> reads an integer into count
string name;
cin>>name; // >> reades a string into name
cout<<"count: "<<count<<endl;
cout<<"name: "<<name<<endl;
The test cases are:
Case 1: Type characters(which not fit for int) and characters
Please enter count and name
ad st
count: 0
name:
Case 2: Type numbers and characters
Please enter count and name
30 ad
count: 30
name: ad
Case 3: Type numbers and numbers (which could be taken as strings)
Please enter count and name
20 33
count: 20
name: 33
std::cinhas a state that you can inspect eg viastd::cin.good(). Input can fail, and often it does, so you need to do something about it - 463035818_is_not_a_numbercinis in an invalid state, any further operations are meaningless. 2) "Or does it mean that before every use of cin, need to check or clear state of cin?" Yes, one needs to get into habit of checking that operations succeeded, instead of assuming that they did. 3) "I have never seen any pre-handling of cin state in code"if (cin>>count) {/* operation succeeded */ }- Algirdas Preidžius