### Error checking Int for a non-Int

Hi there... I wrote a reverse "Guess The Number" game while studying from a textbook, and I put in some error checking, but I couldn't figure out how to check an integer for a non-integer character. If the user enters a letter, the program goes bonkers. I included the code below... I know it's kind of messy and inefficient when the computer has to narrow down its guesses, but putting that aside, can someone help me check the first cin for letters and loop if it doesn't get an integer?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103`` ``````#include #include #include using namespace std; int main() { srand(static_cast(time(0))); // Seeds random number. int randomNumber = rand(); // Generates random number. int number, guess, guesscount = 0; int notgreaterthan = 101, notlessthan = 0; char playagain = 'n', highlowcorrect = 'a'; cout << "\tCOMPUTER GUESSES THE NUMBER\n\n\n"; do { do { cout << "Enter a number for me to guess between 1 and 100.\n"; cout << "I promise I won't look at it!\n\n"; cout << "Enter number (1-100): "; cin >> number; if(number < 1 || number > 100) cout << "No cheating! You must select a number between 1 and 100.\n\n"; } while(number < 1 || number > 100); guess = (randomNumber % 100) +1; //Guesses first random number. ++guesscount; while(highlowcorrect != 'c') { if(highlowcorrect == 'h') //Guesses a random number if the previous guess was too high. { notgreaterthan = guess; while(guess >= notgreaterthan || guess <= notlessthan) { randomNumber = rand(); guess = (randomNumber % 100) +1; } ++guesscount; } if(highlowcorrect == 'l') //Guesses a random number if the previous guess was too low. { notlessthan = guess; randomNumber = rand(); while(guess >= notgreaterthan || guess <= notlessthan) { randomNumber = rand(); guess = (randomNumber % 100) +1; } ++guesscount; } do { cout << "I guess " << guess << ". Is my number (h)igher, (l)ower, or (c)orrect? "; cin >> highlowcorrect; if((highlowcorrect == 'h' && guess <= number) || (highlowcorrect == 'l' && guess >= number) || (highlowcorrect == 'c' && guess != number)) { cout << "\n\nNo cheating! You must tell the truth.\n\n"; highlowcorrect = 'a'; } if(highlowcorrect != 'h' && highlowcorrect != 'l' && highlowcorrect != 'c') cout << "\nYou must enter 'h' for Higher, 'l' for Lower\nor 'c' for Correct.\n\n"; } while(highlowcorrect != 'h' && highlowcorrect != 'l' && highlowcorrect != 'c'); } if(highlowcorrect == 'c') cout << "\n\nI win! It took me " << guesscount << " guesses!"; cout << "\n\nPlay again (y/n)? "; cin >> playagain; if(playagain == 'y') { cout << "\n\n"; notgreaterthan = 101; notlessthan = 0; highlowcorrect = 'a'; guess = 0; guesscount = 0; randomNumber = rand(); } } while (playagain == 'y'); return 0; }``````
Last edited on
Instead of using cin with integer value you can use the following code in the place of line number 25 code
`cin >> number;`

 ``1234`` `````` char s[2]; fflush(stdin); fgets(s,2,stdin); number=atoi(s);``````
Last edited on
Like TV said, read them in char by char and you can use the `isdigit(char c)` method to check if the char is an integer.

Just throw an exception if it isn't an int or just use a simple if statement to check.
Last edited on
Hmm... I don't really understand any of that. Guess it's too advanced for me right now!
Topic archived. No new replies allowed.