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"