Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-01-2007, 02:27 PM
3n! 3n! is offline
Newbie
 
Join Date: Dec 2007
Posts: 1
Rep Power: 0
3n! is on a distinguished road
Default 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"
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 12-02-2007, 12:22 PM
Fedex Fedex is offline
Newbie
 
Join Date: Dec 2007
Posts: 4
Rep Power: 0
Fedex is on a distinguished road
Default

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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-02-2007, 04:44 PM
jbakid jbakid is offline
Newbie
 
Join Date: Dec 2007
Posts: 6
Rep Power: 0
jbakid is on a distinguished road
Default A Better Solution

(rand() % (guessHigh - guessLow + 1)) + guessLow;
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
if text1.text = number ikkeugh Visual Basic Programming 2 10-21-2007 11:04 AM
Floating Point random number generator Paradine PHP Tutorials 0 08-26-2007 03:09 PM
How to calculate the number of pixels in a polygon extreme Java Help 5 04-17-2007 11:34 AM
Number Cloning Ronin Java Help 3 12-21-2006 08:23 AM
I need to generate a random number in PHP dirkfirst PHP Forum 7 07-05-2006 10:58 PM


All times are GMT -5. The time now is 10:55 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 101%


Complete - Celebrate!

Ads