Number Guessing game with a twist

I'm trying to get this to adjust the original range of 1 - 100 to whatever is left after the guess. For example: If I were to guess 20, it should update to "Guess a number between 21 - 100. Then say I guess 90: "between 21 - 89". And so on. First, I keep running into a problem with the "cout" line of the "high" and "low" integers. Won't compile. Secondly, if I take those out and compile it, I run into an issue where when I guess the right number it says "your number is too high" followed by "your guess is correct!" I am a beginner at this sort of thing trying to take the original homework assignment and add to it for added personal learning by making it more dynamic.


#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;

int main () {

int number;
int guess;
int tries = 0;
int low = 1;
int high = 100;
number = rand() % 100 + 1;

cout << "Guessing Game" << endl;

do {

cout << "Enter a number between:" << low; " and " << high; <<endl;
cin >> guess;

tries++;

if (guess < number)
{
low = guess +1;
cout << "Your number is too low." << endl;
}

else {
high = guess - 1;
cout << "Your number is too high. " << endl;
}
} while (number != guess);

if (guess = number);
cout << "Your guess is correct!" << "Your number of tries is: " << tries << endl;


return 0;

}
Your compiler error would have told you the line number of the first error. Once you get used to their format, they're really quite useful in tracking down problems.

I suspect it'll complain about this line:

 
cout << "Enter a number between:" << low; " and " << high; <<endl;


The semi-colons after low and high are terminating the statement at those points, turning it into three statements (the second two being nonsense).

1
2
3
cout << "Enter a number between:" << low;
" and " << high; 
<<endl;


Take a look at the error with the above in mind and you'll get some insight into what it's telling you.

Oh, and remove the two semi-colons ;-)

Jim
Last edited on
Also, the test

1
2
if (guess = number);
cout << "...";


is wrong on many counts.

First, the semi-colon at the end has formed an empty body, so you then have

1
2
3
if (guess = number)
  ;
cout << "...";


and your cout is always called.

Second, the guess = number is using the assignment operator '=', not the comparison operator '=='. The result of guess = number will be the resulting value of guess. If this is non-zero, it'll be translated as 'true' for the if's boolean expression, 'false' if it's zero.

Thirdly, the do-while loop iterates while (number != guess);
So after the loop, we know that number must == guess, so the actual check is pointless - it's always going to be true.

Cheers,
Jim
Thanks for the help Jim. I figured out what I was doing wrong. Originally I had it as cout << "Enter num betwee:" << low " and " << high << endl;

I was getting the error about not having the ";". So I plopped them in and it obviously didn't work. What I was missing was the "<< before the "and"... Doh!

Plus, I did a bit of cleaning up on the code. Firstly removing that extra while statement. Here's the finished product:

#include <cstdlib>
#include <iostream>

using namespace std;

int main () {

int number;
int guess;
int tries = 0, lownum = 1, highnum = 100;
number = rand() % 100 + 1;

do{
cout << "Guess a number between " << lownum << " and " << highnum << endl;
cin >> guess ;
cout << endl;
tries++;



if (guess < number);


cout << "Your number is too low." << endl;
lownum = guess + 1;
cout << "Guess a number between " << lownum << " and " << highnum << endl;
cin >> guess ;
cout << endl;
tries++;

if (guess > number);
highnum = guess - 1;
cout << "Your number is too high." << endl;
cout << "Guess a number between " << lownum << " and " << highnum << endl;
cin >> guess ;
cout << endl;
tries++;







} while (number == guess) ;


cout << "Your guess is correct!" << endl <<"Your number of tries:"<< tries << endl;

system ("PAUSE");
return 0;

}
Topic archived. No new replies allowed.