I currently have a function set up that asks the user for an int, obtains that int, and then checks to make sure that the input meets a certain specification. In this case, its expected that the input will be an integer between -10 and 100. As of now, if I input any string of letters E.G. "gfUIWYDUF", the function returns a 0. Why is this happening and how do I fix it?
int readUserInput() {
cout << "What is the answer?: " << endl;
int answer;
do {
cin >> answer;
if (!cin || answer < -10 || answer > 100) {
cout << "Invalid Input!" << endl;
cout << "What is the answer?: " << endl;
cin.clear();
cin.ignore();
}
} while(!cin || answer < -10 || answer > 100);
return answer;
}