New Problem [Beginner]

This is a discussion on New Problem [Beginner] within the C++ Programming forums, part of the General Programming Boards category; Hi, I got another problem with my code? I have a small guessing game I made. You guess a number ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    6

    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

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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 >=.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    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: ";
    }

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    6
    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.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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!
    */
    Last edited by Dave_Sinkula; 08-16-2005 at 09:49 PM. Reason: Added code sample.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    >> 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21