# New Problem [Beginner]

• 08-16-2005
Vintik
New Problem [Beginner]
Hi, I got another problem with my code? I have a small guessing game I made. You guess a number 1-100, and it tells you whether its too high or too low.

Once you guess the number it tells you how many tries it took you to guess the number. But I found a bug where if you type in an invalid character the program just goes haywire.

First off here's my code

Code:

```// Guess My Number // The classic number guessing game #include <iostream> #include <cstdlib> #include <ctime> #include <limits> using namespace std; int main() {         srand(time(0)); // seed random number generator         int upTo         int theNumber = rand() % 100 + 1; // random number between 1 and 100         int tries = 0, guess;             cout << "\tWelcome to Guess My Number\n\n";         do         {                 cout << "Enter a guess: ";                 cin >> guess;                 ++tries; //                if ((guess >=100) && (guess <= 0))    //                { //                //                  while ((guess >=100) && (guess <= 0)) //                  { //                  cout << "Invalid Response\n"; //                  cout << "Enter another guess: "; //                  cin >> guess; //      };     };                 if (guess > theNumber)                         cout << "Too high!\n\n";                 if (guess < theNumber)                         cout << "Too low!\n\n";                                             } while (guess != theNumber);         cout << "\nThat's it! You got it in " << tries << " guesses!\n";         cin.ignore(numeric_limits<streamsize>::max(), '\n');     cin.get();     return 0; }```
The commented part in the middle was my idea of a solution. I couldn't find a way to fix a invalid type input, so I first set out to find a way to fix an input of lets say 105. So I got an idea, but then it would get the number of tries wrong, then I tried something else and it would always ask for a second input even if the first was correct.

If someone could help me with this problem, I'd appreciate it.

Thanks,
-Vic
• 08-16-2005
swoopy
Code:

```                if ((guess >=100) && (guess <= 0))                    {                                   while ((guess >=100) && (guess <= 0))```
Instead of AND, I think you want OR here:
Code:

```                if ((guess >100) || (guess <= 0))                    {                                   while ((guess >100) || (guess <= 0))```
Also for between 1 and 100, use > versus >=.
• 08-16-2005
Daved
Check the return value of operator >>. If it is false, then you must clear the fail state. This is why the program goes haywire, because cin is in a fail state when somebody types in an illegal character. Here's an exaple of how to check for a legal number:
Code:

```while (!(cin >> guess) || guess < 1 || guess > 100) {   cin.clear();   cin.ignore(numeric_limits<streamsize>::max(), '\n');   cout << "Invalid Response\n";   cout << "Enter another guess: "; }```
• 08-16-2005
Vintik
Is there a way to check for input type?

I'd like to solve my first problem with an input of a character for example. So could I for example have some sort of way to say:

Code:

```                if ((guess >100) || (guess <= 0) || (guess ! int)) //Need a way to have || (guess ! int), I want it to check if the input was a number or a letter. Thanks                 {                                   while ((guess >100) || (guess <= 0))                   {                                     cout << "Invalid Response\n";                   cout << "Enter another guess: ";                   cin >> guess;                   }; };```
As I said in the comment: Need a way to have
Code:

` || (guess ! int)`
, I want it to check if the input was a number or a letter. Thanks.
• 08-16-2005
Dave_Sinkula
Quote:

Originally Posted by Vintik
Need a way to have
Code:

` || (guess ! int)`
, I want it to check if the input was a number or a letter. Thanks.

Read input as a string, then attempt to convert it to a number; if it fails to convert, it is not a number.
Code:

```#include <iostream> #include <string> #include <sstream> #include <ctime> #include <limits> using namespace std; int main() {   srand(time(0)); // seed random number generator   int theNumber = rand() % 100 + 1; // random number between 1 and 100   int tries = 0, guess;   string line;   cout << "\tWelcome to Guess My Number\n\n";   do   {       cout << "Enter a guess: ";       if ( getline(cin, line) )       {         istringstream oss(line);         if ( !(oss >> guess) )         {             cout << "Guess a number\n";             continue;         }         if ( guess < 1 || guess > 100 )         {             cout << "Guess a number from 1-100\n";             continue;         }         ++tries;         if ( guess > theNumber )         {             cout << "Too high!\n\n";         }         else if ( guess < theNumber )         {             cout << "Too low!\n\n";         }       }   } while ( guess != theNumber );   cout << "\nThat's it! You got it in " << tries << " guesses!\n";   cin.get();   return 0; } /* my output         Welcome to Guess My Number Enter a guess: four Guess a number Enter a guess: 50 Too low! Enter a guess: Guess a number Enter a guess: -1 Guess a number from 1-100 Enter a guess: 75 Too low! Enter a guess: 90 Too low! Enter a guess: 95 Too low! Enter a guess: 99 Too high! Enter a guess: 98 Too high! Enter a guess: 97 That's it! You got it in 7 guesses! */```
• 08-16-2005
Daved
>> Is there a way to check for input type?

Is there something wrong with the code I posted? It will enter the loop if the user enters a character instead of an int. Basically, anything the user types that doesn't fit into the guess variable (which is an int) will make (cin >> guess) return false. The extra stuff is to clear the error caused by the character so you can use cin again.

Reading in as a string and parsing is a good way to go if you want that extra control.