Closed Thread
Results 1 to 3 of 3

Thread: Guess a Number

  1. #1
    3n!
    3n! is offline Newbie
    Join Date
    Dec 2007
    Posts
    1
    Rep Power
    0

    Guess a Number

    Yeah I know, it's a lame old game that beginner programmers make.

    The book I'm working out of, "Beginning C++ through Game Programming" has a 'challenge' if you will at the end of Chapter 2. Instead of having the computer choose a random number and you guess, you have to choose the number and the computer guesses. I'm trying to figure out how to set up the random number generator to set its limits based on two variables.

    What I'm trying to do is, guessLow = 1, guessHigh = 100. When the computer guesses a number, if its over my chosen number, it will assign the variable guessHigh with the computers guess, and loop back to the rand function, where it randomly chooses between guessLow and guessHigh again, eventually narrowing it down to my chosen number.

    The problem I run into is with the rand function itself. How do I set it up to pick a number between two variables that already have numbers assigned to them?

    I don't care about anything but getting this function to work.
    Here is what I have so far, though obviously incomplete:

    Code:
    // Guess the Number
    // The player chooses a number and the computer guesses what that number is.
    
    #include "stdafx.h"
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
    	srand(time(0)); // seed the number generator with the time
    
    	int guess;
    	int guessLow = 1;
    	int guessHigh = 100;
    	int pNumber;
    	
    	cout << "Select a number between 1 and 100: ";
    	cin >> pNumber;
    
    	if (pNumber >= 101) // tell the player that if he chooses something above 100, he's wrong
    		cout << "Please don't choose a number above 100, or while we're at it anything negative.";
    	else
    		cout << "Thank you, now the computer is going to guess your number.";
    
    
    	/* The computer guesses between the lowest guess thus far, and the highest
    	then based on whether its too high or too low, the number is assigned to the
    	variables 'guessLow' and 'guessHigh'. Then the computer executes the same
    	process a the top, resulting in it eventually being narrowed down to the
    	player's number of choice.*/
    
    	while (guess != pNumber)
    	{
    		int guess = rand(guessLow - guessHigh);
    		cout << "\n\nComputers guess: " << guess << endl;
    		
    		if (guess < pNumber)
    		{
    			cout << "\nToo low, guess again.";
    			guessLow = guess;
    		}
    	}
    	return 0;
    }
    I know that "int guess = rand(guessLow - guessHigh);" is way out there, cause thats like Low minus High, what I'm looking for is the operator that says "through"

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Fedex is offline Newbie
    Join Date
    Dec 2007
    Posts
    4
    Rep Power
    0
    The line:

    int guess = rand(guessHigh - guessLow)

    will give you a random number between 0 and (guessHigh minus guessLow).

    So let's say guessHigh is 70 and guessLow is 20. Then the line at the top will give you a random number between 0 and 50.

    To get a random number between guessLow and guessHigh, simply add guessLow.

    In otherwords,

    int guess = rand(guessHigh - guessLow) + guessLow

    Is that what you were asking?

  4. #3
    jbakid is offline Newbie
    Join Date
    Dec 2007
    Posts
    6
    Rep Power
    0

    A Better Solution

    (rand() % (guessHigh - guessLow + 1)) + guessLow;

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Problem with Guess the number in C++
    By Azrian in forum Classes and Code Snippets
    Replies: 3
    Last Post: 04-05-2010, 11:47 PM
  2. Guess the number! - Python
    By Azrian in forum Classes and Code Snippets
    Replies: 6
    Last Post: 02-17-2010, 04:13 AM
  3. Guess The Number VS. Computer Opponent.
    By CommittedC0der in forum C Tutorials
    Replies: 4
    Last Post: 12-29-2009, 11:50 AM
  4. guess the number problem
    By CommittedC0der in forum C and C++
    Replies: 2
    Last Post: 11-14-2009, 08:28 AM
  5. Guess the number game
    By drdebcol in forum Classes and Code Snippets
    Replies: 1
    Last Post: 06-13-2009, 08:09 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts