Public Group

# Beginner learning C++ stumped creating a reverse guessing game.

This topic is 2832 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I hope this is the right spot. I am having a good time so far learning C++. In a way I'm relearning it so most of it is familiar and I'm also trying to teach my son at the same time. I'm learning from Michael Dawson's beginners book and everything has been crisp until the end of the second chapter. At the end of the second chapter completed a guessing game. Here is the code.

// Guess My Number

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main ()

{
srand(time(0)); // seed random number generator

int theNumber = rand() % 100 + 1; // random number between 1 and 100
int tries = 0, guess;

cout << "\tWelcome to Guess my Nunber!!\n\n";

do

{

cout << "Enter a guess: ";
cin >> guess;
++tries;

if (guess > theNumber)
cout << "Too High!\n\n";
if (guess < theNumber)
cout << "Too Low!\n\n";

} while (guess != theNumber);

cout << "\nThat's it! You got it in " << tries << " guess!\n";

return 0;

}

The bolded didnt make sense when I wrote it but it worked and I didnt make much of it untiiiill I tried to do an exercise where you create a reverse guessing game. In the reverse guessing game you think of a number and the computer guesses it. So far I think I'm doing pretty well with the program because I actually got it to compile, guess a random number and let the user input higher or lower so the computer can guess again. Thats when it breaks down. I dont know how to get it to track the number of guesses, and I really dont know how to set the next guess with a new scale. So if the computer is told their first guess of 83 was too high I'm not sure how to change the scale for the next guess and the next etc. The problem with tracking the number of guesses bothers me more because I figured that it would be easier. I think it might be related to the line of code from the book that I didnt understand. In the bolded code I cant figure out why tries is = to guess. If that is to track guesses which I dont see it doing why is tries incremented with ++tries in every loop? I figured the ++tries would be all I needed in my code. At first I tried an easy way to solve the scaled guesses.
if (higher = 'h')
(theguess = (rand() % 100 + 1 > theguess));
else
(theguess = (rand() % 100 + 1 < theguess));

That obviously did not work. I'm thinking I have to set a variable for actual guess and build from there.

Also as you can see from code the bottom is amateur night at the Apollo with those if statments. I couldnt get it to complie && but I'm not sure if those if statements arent working because its not counting 'tries'.
Here is my code.

// This Program is from scratch meaning we made this shit up based what we know.

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main ()

{
srand(time(0)); // seed random number generator

int theguess = rand() % 100 + 1; // random number between 1 and 100
char guess = 'n';
int tries = 0,;
char higher = 'h';
char lower = 'l';

cout << "\tThis is take the Cookies!!\n\n";
cout << "\nIf I guess ya number in less than six guesses I take the cookies!";
cout << "\nIf I guess yo number in 6 we can share the cookies.";
cout << "\nSo go ahead and pick a number from 1 to 100 and dont cheat.";
system("\npause");

//player picks a number in their head

while (guess == 'n')

{
cout << "\nI guess " << theguess;
cout << "\nIs that right?: y/n ";
++tries;
cin >> guess;

if (guess == 'y')
break;

cout << "\nhigher: h ";
cout << "\nlower: l ";
cin >> higher || lower;
if (higher = 'h')
(theguess = (rand() % 100 + 1 > theguess));
else
(theguess = (rand() % 100 + 1 < theguess));
}

if (tries = 6)
cout << "Six guesses we share the cookies";

if (tries = 1)
cout << "One Guess punk! Them my cookies!!";

if (tries < 6)

cout << "I get the cookies!!";

if (tries > 6)

return 0;

}

##### Share on other sites
This is where it starts to go pear shaped as far as I can tell

cin >> higher || lower if (higher = 'h') (theguess = (rand() % 100 + 1 > theguess)); else (theguess = (rand() % 100 + 1 < theguess)); 

cin >> higher || lower

higher || lower is a bool value, (your saying higher logical or lower). It doesn't make much sense. You could ditch lower since its not really used. cin >> higher;

if (higher = 'h')

be very careful when comparing values, what your actually doing there is assigning 'h'h to higher, which in turn returns 'h' so that if statement will always be true. It should be:

if(higher == 'h')

You have the same mistake a little further on if(tries = ...). These kind of mistakes are quite hard to spot (since they will compile just fine and usually without warning).

You seem to be working out your guess wrong too.

First you guess 1-100 which is fine, next time round, if the actual number is higher then you want to make a guess between old guess and 100. Otherwise you want 1 to old guess.

int maxGuess = 100;
int minGuess = 1;
int range = maxGuess - minGuess;
// get a random number in our range
int r = rand() % range;
// number is only in the range and doesn't take into account the offset so add min value to it
theguess = r + minGuess;

You could wrap that little bit up in a function that returns a random between two values.

##### Share on other sites
You've got a couple of problems with these lines:

 cout << "\nhigher: h "; cout << "\nlower: l "; cin >> higher || lower; if (higher = 'h') (theguess = (rand() % 100 + 1 > theguess)); else (theguess = (rand() % 100 + 1 < theguess)); } 

First of all:
cin >> higher || lower
You can't use the OR operator like that. In fact, you don't even need two variables. Just do this:
 char higherorlower; ... cin >> higherorlower; if (higherorlower == 'h') ... else if (higherorlower == 'l' ... 
Also, while typing that, I noticed that you have a mistake in the code if (higher = 'h'). You should use == for comparison, not =.

Second, you've got this line:
 (theguess = (rand() % 100 + 1 > theguess)); 

Your entire statement is surrounded by a pair of parentheses. You need to get rid of those.
Also, you're not using the > operator correctly. What you probably want is something like this:
 theguess = theguess + 1 + (rand() % (100 - theguess)); 
And for the "lower" guess:
 theguess = 1 + (rand() % (theguess - 1)); 

I just typed that off the top of my head, and I haven't tested it or anything, but it should work for what you need.

Also, I just noticed a fairly big problem with your program. In a real guessing game, a person would use their knowledge from all their previous guesses to make their next one. (The number is larger than 36, but smaller than 75, etc.) However, your program uses only the very most recent guess in determining the next one! Suppose the number you thought of was 42, and the computer guesses 30. You tell the computer that your number (42) was larger than its guess (30). So, it guesses 69. You tell it that your number (42) was smaller than its guess (69). So then it guesses 16! This could easily happen because the computer doesn't remember that your number was larger than 30. Rather than storing the previous guess between tries, you might want to try storing an upper limit and a lower limit for the number. The upper limit would start at 100, and the lower limit would start at 1. Then, if the computer guessed a number, and you said your number was higher, the computer would set its lower limit to the previous guess + 1, and vice versa if your number was lower. The computer would then generate a random number between the lower limit and the upper limit for its next guess. You'd also be able to know the player was "cheating" if the lower limit ever became higher than the upper limit. I hope that made sense.

Wow, that was longer than I thought it would be. Anyway, I hope it helps, and good luck learning C++

EDIT: Darn, Nanoha beat me to it!

##### Share on other sites
Its funny I made the mistake assigning with the single =. I warn my son of that.

Thanks for the feedback so far. Its true I'm going to have to constantly loop a guess value that is based on all of the previous. That will be interesting
.

##### Share on other sites
I'm still not sure why in the first program tries is = to guess.

int tries = 0, guess;

Is that just a way to set guess to 0?

##### Share on other sites
that creates an int called tries and assigns 0 to it, it also creates an int called guess but does NOT assign anything to it. It s the same as doing:

int tries = 0;
int guess;

From my experience thats usually frowned upon (as is declaring values without assigning anything to them). Better off doing:

int tries = 0;
int guess = 0;

##### Share on other sites
Hidden

int tries = 0, guess;

Is that just a way to set guess to 0?

No. This statement declares two integers, "tries" and "guess," and initializes tries to zero. It is the equivalent to:

 int tries = 0; int guess; 

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
13
5. 5
frob
12

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633669
• Total Posts
3013257
×