I'm trying to error handle the conversion from string to int using ss.fail and after the program loops back to the input, it keeps giving an error even if I enter integers. Note that this only happens after the error handle loops. I've tried ss.clear(), cin.ignore, etc and it still loops indefinitely when I enter an integer. How do I correctly error handle this?
string strNumber; //User to input a number
stringstream ss; //Used to convert string to int
int number; //Used to convert a string to number and display it
bool error = false; //Loops if there is an input error
do{
//Prompts the user to enter a number to be stored in string
cout << endl << "Enter an integer number: ";
getline(cin, strNumber);
//Converts the string number to int and loops otherwise
ss << strNumber;
ss >> number;
if(ss.fail()){
cout << "This is not an integer" << endl;
ss.clear();
//cin.ignore(numeric_limits<streamsize>::max(), '\n');
error = true;
}
else
error = false;
} while(error == true);