# noob needs helps

I've just started doing C++ and finding it quite tricky! I'm trying to make the classic number guessing game in which the user has 3 guesses. So far i've managed to get the random number generatorand the 3 three guesses sorted and when the user enters the number i'd like the game to say if it's hot or cold. i'm using the code:
if((usersGuess - random == 1) || (usersGuess - random == -1))

cout<<"HOT";

else if((usersGuess - random == 2) || (usersGuess - random == -2))

cout<<"WARM";

when i run the program, it doesn't go further than the first guess, which is always correct! Any help???

• : I've just started doing C++ and finding it quite tricky! I'm trying
: to make the classic number guessing game in which the user has 3
: guesses. So far i've managed to get the random number generatorand
: the 3 three guesses sorted and when the user enters the number i'd
: like the game to say if it's hot or cold. i'm using the code:
: if((usersGuess - random == 1) || (usersGuess - random == -1))
:
: cout<<"HOT";
:
: else if((usersGuess - random == 2) || (usersGuess - random == -2))
:
: cout<<"WARM";
:
: when i run the program, it doesn't go further than the first guess,
: which is always correct! Any help???

Post the full code, since the problem isn't located in the code you posted.
• : : I've just started doing C++ and finding it quite tricky! I'm trying
: : to make the classic number guessing game in which the user has 3
: : guesses. So far i've managed to get the random number generatorand
: : the 3 three guesses sorted and when the user enters the number i'd
: : like the game to say if it's hot or cold. i'm using the code:
: : if((usersGuess - random == 1) || (usersGuess - random == -1))
: :
: : cout<<"HOT";
: :
: : else if((usersGuess - random == 2) || (usersGuess - random == -2))
: :
: : cout<<"WARM";
: :
: : when i run the program, it doesn't go further than the first guess,
: : which is always correct! Any help???
:
:
: Post the full code, since the problem isn't located in the code you
: posted.
[color=Green]
ige> What is the number guessing game?[/color]
• : : : I've just started doing C++ and finding it quite tricky! I'm trying
: : : to make the classic number guessing game in which the user has 3
: : : guesses. So far i've managed to get the random number generatorand
: : : the 3 three guesses sorted and when the user enters the number i'd
: : : like the game to say if it's hot or cold. i'm using the code:
: : : if((usersGuess - random == 1) || (usersGuess - random == -1))
: : :
: : : cout<<"HOT";
: : :
: : : else if((usersGuess - random == 2) || (usersGuess - random == -2))
: : :
: : : cout<<"WARM";
: : :
: : : when i run the program, it doesn't go further than the first guess,
: : : which is always correct! Any help???
: :
: :
: : Post the full code, since the problem isn't located in the code you
: : posted.
: [color=Green]
: ige> What is the number guessing game?[/color]
:
Ige, see this site: http://en.wikipedia.org/wiki/Number_(game)
• : : : : I've just started doing C++ and finding it quite tricky! I'm trying
: : : : to make the classic number guessing game in which the user has 3
: : : : guesses. So far i've managed to get the random number generatorand
: : : : the 3 three guesses sorted and when the user enters the number i'd
: : : : like the game to say if it's hot or cold. i'm using the code:
: : : : if((usersGuess - random == 1) || (usersGuess - random == -1))
: : : :
: : : : cout<<"HOT";
: : : :
: : : : else if((usersGuess - random == 2) || (usersGuess - random == -2))
: : : :
: : : : cout<<"WARM";
: : : :
: : : : when i run the program, it doesn't go further than the first guess,
: : : : which is always correct! Any help???
: : :
: : :
: : : Post the full code, since the problem isn't located in the code you
: : : posted.
: : [color=Green]
: : ige> What is the number guessing game?[/color]
: :
: Ige, see this site: http://en.wikipedia.org/wiki/Number_(game)

[color=Blue]ige> Ok... nice ... So, as Lundin said, we need more code. noob, where is the loop ( I am pretty sure you have made one ). Probably there is some mistake in condition of the loop.[/color]
• : : : : : I've just started doing C++ and finding it quite tricky! I'm trying
: : : : : to make the classic number guessing game in which the user has 3
: : : : : guesses. So far i've managed to get the random number generatorand
: : : : : the 3 three guesses sorted and when the user enters the number i'd
: : : : : like the game to say if it's hot or cold. i'm using the code:
: : : : : if((usersGuess - random == 1) || (usersGuess - random == -1))
: : : : :
: : : : : cout<<"HOT";
: : : : :
: : : : : else if((usersGuess - random == 2) || (usersGuess - random == -2))
: : : : :
: : : : : cout<<"WARM";
: : : : :
: : : : : when i run the program, it doesn't go further than the first guess,
: : : : : which is always correct! Any help???
: : : :
: : : :
: : : : Post the full code, since the problem isn't located in the code you
: : : : posted.
: : : [color=Green]
: : : ige> What is the number guessing game?[/color]
: : :
: : Ige, see this site: http://en.wikipedia.org/wiki/Number_(game)
:
: [color=Blue]ige> Ok... nice ... So, as Lundin said, we need more
: code. noob, where is the loop ( I am pretty sure you have made one
: ). Probably there is some mistake in condition of the loop.[/color]
:

[code]
if((usersGuess - random == 1) || (usersGuess - random == -1))
cout<<"HOT";
else if((usersGuess - random == 2) || (usersGuess - random == -2))
cout<<"WARM";
[/code]

Not to jump forward, but your condition is done twice, whihc seems unneccessary

This does the same, but is cleaner code.

[code]
if(usersGuess - random == -1)
cout<<"HOT";
else if(usersGuess - random == 2)
cout<<"WARM";
[/code]

{2}rIng
• : Not to jump forward, but your condition is done twice, whihc seems
: unneccessary
:
: This does the same, but is cleaner code.
:
: [code]:
: if(usersGuess - random == -1)
: cout<<"HOT";
: else if(usersGuess - random == 2)
: cout<<"WARM";
: [/code]:
:
: {2}rIng

Not neccesarily: what if usersGuess > random and usersGuess - random == 1.
Then you'd want it to print "HOT" because you're only one off. Your version of the code doesn't do this.

Best Regards,
Richard

The way I see it... Well, it's all pretty blurry
• : when i run the program, it doesn't go further than the first guess,
: which is always correct! Any help???

Without code to look at, I'll just have to guess here.
It looks to me that if the guess is always correct (when it's not supposed to be) that you're setting the guess equal to the number they're trying to guess.

I suppose you have something in your code checking to see if they're equal to see if they guess correctly. Something like:
{cout << "Victory!" << endl;}

Make sure there are two equal signs in your code like this otherwise you SET the guess to the correct number AND then cause the 'win' message to get printed out. Also, check all the other ='s and =='s in your code to make sure they're set to = to change a variables value and == to compare them.

~makkura
• Hey, here's the full code that i've managed to get down so far;

#include
#include //RANDOM NUMBER GENERATOR
#include

int main()
{
int random; //RANDOM NUMBER TO BE GUESSED
int usersGuess;

const int LIMIT=10; // MAX RANDOM NUMBER

cout << "Welcome to the game

"
<< "I will generate a number between 1 and 10

"
<< "and you have 3 chances to guess it

";

srand((unsigned)time(0));

int lowest=1, highest=10; //RANDOM NUMBER RANGE
int range=(highest-lowest)+1;
random = lowest+int(range*rand()/(RAND_MAX + 1.0));
cout<<random;

cout <<"Guess a number between 0 and "
<<LIMIT << "
";

cout << "Input guess : ";
cin >> usersGuess;

int attempts = 1;
while (attempts )
{
if((usersGuess - random == 1) || (usersGuess - random == -1))

cout<<"HOT";

else if((usersGuess - random == 2) || (usersGuess - random == -2))

cout<<"WARM";

else if((usersGuess - random == 3) || (usersGuess - random == -3))

cout<<"COLD";

if (usersGuess = random)

cout<<"YOU WON
";

attempts++;

attempts = 3;

}

return(0);
}

I know there's gonna be a few mistakes, any help would be appreciated
• : Hey, here's the full code that i've managed to get down so far;
:
: #include
: #include //RANDOM NUMBER GENERATOR
: #include
:
: int main()
: {
: int random; //RANDOM NUMBER TO BE GUESSED
: int usersGuess;
:
:
: const int LIMIT=10; // MAX RANDOM NUMBER
:
: cout << "Welcome to the game

"
: << "I will generate a number between 1 and 10

"
: << "and you have 3 chances to guess it

";
:
:
: srand((unsigned)time(0));
:
: int lowest=1, highest=10; //RANDOM NUMBER RANGE
: int range=(highest-lowest)+1;
: random = lowest+int(range*rand()/(RAND_MAX + 1.0));
: cout<<random;
:
: cout <<"Guess a number between 0 and "
: <<LIMIT << "
";
:
: cout << "Input guess : ";
: cin >> usersGuess;
:
: int attempts = 1;
: while (attempts )
: {
: if((usersGuess - random == 1) || (usersGuess - random == -1))
:
: cout<<"HOT";
:
: else if((usersGuess - random == 2) || (usersGuess - random ==
: -2))
:
: cout<<"WARM";
:
: else if((usersGuess - random == 3) || (usersGuess - random ==
: -3))
:
: cout<<"COLD";
:
:
: if (usersGuess = random)
:
: cout<<"YOU WON
";
:
: attempts++;
:
: attempts = 3;
:
:
: }
:
:
: return(0);
: }
:
: I know there's gonna be a few mistakes, any help would be appreciated
:
[color=Green]
ige>
hi, as i suspected, there is a problem in the loop.

1.
You are reading the input from user outside the loop , and thus reading it only once.[code][/code]
This is outside the loop
[code]cin >> usersGuess;[/code]
It should be inside the loop. [ i.e. after while (attempts ) ]
[code][/code]
2.
In the end of the loop,[code][/code]
you set attempts = 3, why?[code][/code]
When you put this statement,[code][/code]
[code]attempts = 3;[/code]
This statement will set the number of attempts to 3, at end of 1st loop itself. And so you will never enter the loop again.[code][/code]
Remove this statement.[code][/code]
----------[code][/code]
You should use curly brackets "{", for if statements. Although they are not required for "if" with single statements inside them, its good if you use them (at least, at initial stages when you are learning the language, so that you make less mistakes).[code][/code]
----------[code][/code]
And Change this statement
[code]
if (usersGuess = random)
cout<<"YOU WON
";
[/code]
to
[code]
if (usersGuess = random)
{
cout<<"YOU WON
";
break;
}
[/code]
"break" will take you out of the loop, even if attempts is less than 3 (thats is what you want). [code][/code]
----------[code][/code]
Also, you have 3 "if" statements, when the user has guessed a number that is 1,2 or 3 away from the actual number. What if he has entered a number even further away from the actual number? Think about it :-)

[/color]
• : Hey, here's the full code that i've managed to get down so far;
:
: #include
: #include //RANDOM NUMBER GENERATOR
: #include
:
: int main()
: {
: int random; //RANDOM NUMBER TO BE GUESSED
: int usersGuess;
:
:
: const int LIMIT=10; // MAX RANDOM NUMBER
:
: cout << "Welcome to the game

"
: << "I will generate a number between 1 and 10

"
: << "and you have 3 chances to guess it

";
:
:
: srand((unsigned)time(0));
:
: int lowest=1, highest=10; //RANDOM NUMBER RANGE
: int range=(highest-lowest)+1;
: random = lowest+int(range*rand()/(RAND_MAX + 1.0));
: cout<<random;
:
: cout <<"Guess a number between 0 and "
: <<LIMIT << "
";
:
: cout << "Input guess : ";
: cin >> usersGuess;
:
: int attempts = 1;
: while (attempts )
: {
: if((usersGuess - random == 1) || (usersGuess - random == -1))
:
: cout<<"HOT";
:
: else if((usersGuess - random == 2) || (usersGuess - random ==
: -2))
:
: cout<<"WARM";
:
: else if((usersGuess - random == 3) || (usersGuess - random ==
: -3))
:
: cout<<"COLD";
:
:
: if (usersGuess = random)
:
: cout<<"YOU WON
";
:
: attempts++;
:
: attempts = 3;
:
:
: }
:
:
: return(0);
: }
:
: I know there's gonna be a few mistakes, any help would be appreciated
:

As stated by others, you do have some problems in your loop.
You also have an = / == problem in your loop too.

if (usersGuess = random)
cout<<"YOU WON
";

This section here sets the usersGuess to the random number you made, so they always win.

attempts = 3;
Will set your number of attempts to 3 and cause you to leave the loop as it now things you've tried three times.

Once those are taken care of, you will have one more issue with your loop.
You have it in the while loop as long as attempts < 3 and attempts start at 1. So attempt 1 will go into the loops, attempt 2 (once attemps = 3 is edited out) will go through, and attempt three will cause the loop to end. Try changing the loop to attempts <= 3.

Also, as stated, you'll need to do something about your cin section for the guess. You'll probably simply want to move it into the loop area.