hey all. I've been banging my head on this for hours.
I'm trying to get only whole numbers from a user and if they input anything different, I want it to display a validation error and restart the function. Here's what I have:
I'm either getting an infinite loop of validation errors or it keeps the old cin input. It's getting very frustrating :angry:
void takeAGuess() {
// I have a random number function.
cout << "\nI'm thinking of a number between 0 and 10..." << endl;
cout << "\nYour Guess:" << endl;
int userGuess;
cin >> userGuess;
while (!cin >> userGuess) {
cout << "INVALID INPUT.";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
takeAGuess();
}
}
What am I doing wrong?!
I could easily solve this with a Try/Catch block with a NumberFormatException in Java; I'm very new to C++