Jan 9, 2013 at 3:56am UTC
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?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
srand(static_cast <unsigned int >(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 Jan 9, 2013 at 4:16am UTC
Jan 9, 2013 at 5:15am UTC
Instead of using cin with integer value you can use the following code in the place of line number 25 code
cin >> number;
1 2 3 4
char s[2];
fflush(stdin);
fgets(s,2,stdin);
number=atoi(s);
Last edited on Jan 9, 2013 at 5:15am UTC
Jan 9, 2013 at 5:19am UTC
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 Jan 9, 2013 at 5:21am UTC
Jan 10, 2013 at 11:19pm UTC
Hmm... I don't really understand any of that. Guess it's too advanced for me right now!