Thread: Number guessing game

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    10

    Number guessing game

    First off, I did do a search but came up with nothing that specifically helped me. I'm doing a problem where the user inputs a number and the computer has to guess it based on the user saying higher or lower. There's more to the program, but that doesn't matter.

    Is it possible to make the computer generate a value using binary search between the max number and min number?

    For example, I enter 500 and the minimum is 1. The computer's first guess is obviously 250 (or number/2). If I say higher, the computer will now have a minimum value of 250. So the next guess would be between 250 and 500, and would be 375 ( 250 / 2 + 250).

    When I run the program, it assigns the min and max numbers accordingly, but never (or rarely) produces a value in between the two values, which is always should.

    Here's a portion of my code for the number being higher:

    I think the statement "number= rand() % n + 1;"
    is wrong. Can anyone lead me in the right direction? I realize this should be easy and I'm probably making it harder than it really is! Thanks for any help that can be given

    Code:
     if (response == 'h')
    {
    	number = rand() % n + 1;
           cout << "Next guess: " << number << ". Is your number higher or lower? " ;
           cin >> response;
           min = number;			// set min number to last guess by computer
    }

  2. #2
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    Looks like your getting a new random number on each guess.

    You should only generate the random number you want to guess once. since it is inside the block each time you enter "h" it is going to generate a new random number. Move it outside the block.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    10
    Actually, I just figured it out and the program works perfectly now! Thanks for the reply anyway manofsteel, much appreciated!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D Game project requires extra C++ programmers, new or experienced
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 05-16-2007, 10:46 AM
  2. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  3. Random guessing game
    By Nalif in forum C Programming
    Replies: 16
    Last Post: 10-26-2006, 03:05 AM
  4. Number guessing.
    By Lunatic Magnet in forum C Programming
    Replies: 5
    Last Post: 04-07-2006, 12:43 AM
  5. beach bar (sims type game)
    By DrKillPatient in forum Game Programming
    Replies: 1
    Last Post: 03-06-2006, 01:32 PM