# Thread: Else if noobie trouble

1. ## Else if noobie trouble

Hi,

I am trying to create a game where a random integer between 1 and 10 is created and if the player guesses correct it says "win", if they guess too low it says "Too low, if they guess to high it says "Too high" BUT if they guess out of range it says "invalid guess".

My problem is that when the player enters a number within range, the last part of my If Else statement is still running (the invalid guess).

Code:
```
if (guess_number == random_integer)
{
cout << "You win! ";
}
else if (guess_number < random_integer)
{
cout << "Your guess was too low ";
}
else if (guess_number > random_integer)
{
cout << "Your guess was too high";
}
else ( (guess_number < 1) || ( guess_number > 10) );
{
cout << "invalid guess, needs to be between 1 and 10";
}```
I need it to not run the last ELSE if the guess is within range.

Thank you!

3. I changed indentation.
Now you should be able to see what's wrong
Code:
```    if (guess_number == random_integer) {
cout << "You win! ";
}
else
if (guess_number < random_integer) {
cout << "Your guess was too low ";
}
else
if (guess_number > random_integer) {
cout << "Your guess was too high";
}
else
( (guess_number < 1) || ( guess_number > 10) );
{
cout << "invalid guess, needs to be between 1 and 10";
}```
Kurt

4. else has no conditional, it is implicitly "everything else" (hence the name).

5. Thank a lot! Is this a precedence issue?

Will look at that

6. Code:
```    if (guess_number < random_integer)
{
cout << "Your guess was too low ";
}
else if (guess_number > random_integer)
{
cout << "Your guess was too high";
}
else if (guess_number == random_integer)
{
cout << "You win! ";
}
else if ( (guess_number < 1) || ( guess_number > 10) )
{
cout << "invalid guess, needs to be between 1 and 10";
}```
Thanks everyone, the above is working.

Very kind.

I think the point was that you should check if you have valid input as the first thing not the last.

8. Originally Posted by Swerve
Thanks everyone, the above is working.
Very kind.
Don't think so
Code:
```    if (guess_number < random_integer)
{
cout << "Your guess was too low ";
}
else if (guess_number > random_integer)
{
cout << "Your guess was too high";
}
else if (guess_number == random_integer)
{
cout << "You win! ";
}
else if ( (guess_number < 1) || ( guess_number > 10) )  // can never be true
// it's either lower greater or equal
{
cout << "invalid guess, needs to be between 1 and 10";
}```
Kurt

9. Thanks Zuk and everyone!

Now I am trying to limit the player to 3 guesses, but the IF ELSE's are looping for ever:-

Code:
```#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int main()
{
int human_guess = 0;
int guess_count = 0;
int random_integer;
srand((unsigned)time(0));

random_integer = (rand()&#37;10);

cout << "Hi and welcome to the Guess the Number game.\nThe BLAH BLAH BLAH ur guess and then press Enter. Good luck! ";

while (guess_count < 3)
{
guess_count ++;
cin >> guess_count;

if (human_guess < random_integer)
{
cout << "Your guess was too low " << endl;

}

else if (human_guess > random_integer)
{
cout << "Your guess was too high" << endl;

}

else if (human_guess == random_integer)
{
cout << "You win! " << endl;

}

else if (human_guess < 1)
{
cout << "invalid guess, needs to be 1 or higher" << endl;

}
else if (human_guess > 10)
{
cout << "invalid guess, needs to be 10 or less" << endl;

}
else if (human_guess == 3)
{
cout << "you have ran out of guesses" << endl;
}

cin.get();

}

cin.get();

}```
Could I get a point in the right direction please.

Many thanks

10. Youre doin' it again.
You can't just add any number of else if statements
In your most recent code the last 3 if blocks can never be reached.
Kurt

11. Firstly you need to realize that if there is a possibility of invalid input, you must check this first.

Code:
```    int human_guess = 0;
int guess_count = 0;
//...

while (guess_count < 3)
{
guess_count ++;
cin >> guess_count;

if (human_guess < random_integer)```
You never get any input to human_guess, instead you get input for the counter that is supposed to be managed by your code.

You need to input into human_guess. There should be exactly one input statement in the loop and the loop should take care that it is called at least 3 times (more if you don't count invalid input).

Actually I wouldn't let the code get any further from the input statement until user has entered acceptable input. For that you'll need another loop that runs until user has entered valid choice.