I have an integer variable that that I need to check if someone enters only integers. I have the following code but if they enter something such as "dfgdsfdsfdsf" but it goes into an infinite loop and doesnt wait for the user to input data again. The variable guess is the variable that is an integer and cannot accept characters. The following is what I have so far:
rndNbr = randomNbr(); //Generate a random number
cout << "I have a number between 1 and 1000.\n";
cout << "Can you guess my number?\n";
cout << "Please type your first guess.";
do
{
if(!(cin >> guess)||(guess > 1000)||(guess < 1))
{
cin.clear();
cout << "\n" << "Not between 1 and 1000! Try again.\n";
}
}
while((guess > 1000)||(guess < 1));