Guess My Number (Need help)

This is a discussion on Guess My Number (Need help) within the C++ Programming forums, part of the General Programming Boards category; I am starting back up on my C++ programming and I've been out of it for about 3 years. I ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    30

    Question Guess My Number (Need help)

    I am starting back up on my C++ programming and I've been out of it for about 3 years. I am going back through one of my books and it asks you to create a game.

    Guess My Number:
    -Player picks number 1-100
    -Computer proceeds to guess a number till it hits the number you chose
    -Be creative!


    I have already written the program about 30 min ago and after some trial and error, it seems to be working except for:
    -Kinda sloppy/redundant
    -Once the computer guesses the correct number, it skips the output of the correct number and proceeds the the end of the program.

    Code:
    //Guess My Number
    //Dustin Hardin 12-08-09
    
    /*  The Player picks a number between 1 and 100 and the
        computer guesses till it gets it right.  */
        
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    int main()
    {
        int theNumber;
        int tries = 0, guess, prevGuessHigh = 100, prevGuessLow = 1;
        
        cout << "\tWelcome to Guess My Number\n\n";
        
        cout << "Player, please pick a number between 1 and 100: ";
        cin >> theNumber;
        cin.ignore();
        
        //Generate "random" number between 1 and 100 as computers guess
        srand(time(0));
        guess = rand() % 100 + 1;
        
        do
        {
                     //display previous input, the number you picked, and ask for a number.
                     cout << "High: " << prevGuessHigh << ", Low: " << prevGuessLow << ", The Number: " << theNumber << "\n\n";
                     cout << "Computer, please guess a number: " << guess << "\n\n";
                     cin.get();
                     ++tries;
                     
                     if(guess > theNumber)
                     {
                              cout << "Too High!\n\n";
                              prevGuessHigh = guess;
                              
                              guess = rand() % (prevGuessHigh - prevGuessLow) + prevGuessLow;  //the number is between the high guess and low, so random between these two
                              
                              if(guess == prevGuessLow)//Increment guess if it's equal to the last lowest number
                                       guess++;
                     }
                              
                     else if(guess < theNumber)
                     {
                              cout << "Too Low!\n\n";
                              prevGuessLow = guess;
                              
                              guess = rand() % (prevGuessHigh - prevGuessLow) + prevGuessLow;  //the number is between the high guess and low, so random between these two
                              
                              if(guess == prevGuessLow)//Increment guess if it's equal to the last lowest number
                                       guess++;
                     }
                     
                     
                              
        }while(guess != theNumber);
        
        cout << "That's it!  You got it in: " << tries << " guesses!";
        
        cout << "\n\nPlease press the enter key to exit....";
        cin.get();
        
        return 0;
    }
    Last edited by dhardin; 12-08-2009 at 08:05 PM.

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425

    .

    Code:
    cout << "That's it!  You got it in: " << tries << " guesses!";
        
        cout << "\n\nPlease press the enter key to exit....";
    You are only printing the number of tries here, not the target number itself, your condition may be matched but then nothing happens except showing number of tries
    Last edited by rogster001; 12-09-2009 at 12:36 AM.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    30
    Oh, okay. I thought is was something in the game loop itself. lol, that makes sense though.


    Thanks!

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    30
    I fixed it another way....(this is excluding the include/using statements...see above)

    Everything in bold is what I changed
    -added boolean variable to see whether or not its comps first guess
    -moved if statements to top of do loop and added in parameters
    check for initial guess


    Code:
    int main()
    {
        int theNumber;
        int tries = 0, guess, prevGuessHigh = 100, prevGuessLow = 1;
        bool initialGuess = true; //initial guess for computer
        
        cout << "\tWelcome to Guess My Number\n\n";
        
        cout << "Player, please pick a number between 1 and 100: ";
        cin >> theNumber;
        cin.ignore();
        
        //Generate "random" number between 1 and 100 as computers guess
        srand(time(0));
    
        
        do
        {
                     if(initialGuess == true)
                     {
                         guess = rand() % 100 + 1;
                         initialGuess = false;  //computer has had initial guess
                     }
    
                     else if(guess > theNumber)
                     {
                              cout << "Too High!\n\n";
                              prevGuessHigh = guess;
                              
                              guess = rand() % (prevGuessHigh - prevGuessLow) + prevGuessLow;  //the number is between the high guess and low, so random between these two
                              
                      else if(guess == prevGuessLow)//Increment guess if it's equal to the last lowest number
                                       guess++;
                     }
                              
                     else if(guess < theNumber && initialGuess == false)
                     {
                              cout << "Too Low!\n\n";
                              prevGuessLow = guess;
                              
                              guess = rand() % (prevGuessHigh - prevGuessLow) + prevGuessLow;  //the number is between the high guess and low, so random between these two
                              
                              if(guess == prevGuessLow)//Increment guess if it's equal to the last lowest number
                                       guess++;
                     }
                     
                     //display previous input, the number you picked, and ask for a number.
                     cout << "High: " << prevGuessHigh << ", Low: " << prevGuessLow << ", The Number: " << theNumber << "\n\n";
                     cout << "Computer, please guess a number: " << guess << "\n\n";
                     cin.get();
                     ++tries;
                     
                     
                     
                     
                              
        }while(guess != theNumber);
        
        cout << "That's it!  You got it in: " << tries << " guesses!";
        
        cout << "\n\nPlease press the enter key to exit....";
        cin.get();
        
        return 0;
    }
    Last edited by dhardin; 12-09-2009 at 10:21 AM. Reason: fixed code

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    you still don't print out the actual number at the end though? which is what i thought you wanted

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    30
    well, i'm constantly displaying what the computer is guessing and the actual number...what was occurring before is that when the computer guessed the right number, it would only congratulate the computer but not display its guess. But thank you for your help, I did a couple more changes to the code and it works great!


    thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 02-19-2009, 06:19 PM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  3. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  4. small problem
    By ucfmustang in forum C Programming
    Replies: 1
    Last Post: 02-10-2003, 03:55 PM
  5. Newton-Raphson number squaring method/pointers
    By inakappeh in forum C Programming
    Replies: 2
    Last Post: 08-29-2001, 11:04 AM

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