I am looking for a way to handle the comparison of input datatypes. In other words, in my cin>>a where a=int, how would I generate an error message if input were a char type.
You can check if the input was successful by using it as a condition for a loop or if statement.
1 2 3 4 5
int a;
if (cin >> a)
{
cout << "error!" << endl;
}
If you want to use cin again after it has failed to read the input you will have to call cin.clear() to clear all error flags. You probably also want to remove the invalid input from cin.
1 2
cin.clear(); // Clears the error flags.
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Removes the whole line