When using any control statement in c++ (if, else, while etc.) never put a semicolon (;) after the statement.
The control statement affects only what comes directly after it. If you want to affect more then 1 line, you need to put
the code in a code block ({..}).
if (randomNumberGuess > randomGeneratedNumber); <-- remove semicolon.
Basically, the 'if' statement will effect whats right after it, and guess what that is? a semicolon... you want to have your code block right after the 'if':
Code: Select all
if (randomNumberGuess > randomGeneratedNumber) // <-- No semicolon, the if is applied on whats directly after it, the code block ({..})
{
cout << "Lowerrrrrrr! Try again!" << endl;
cin >> randomNumberGuess;
}
same with 'else':
else (randomNumberGuess == randomGeneratedNumber) stillGuessing = false; <-- the 'else' will affect only what's after it, which is:
stillGuessing = false; it will not affect the code block ({..}) at all!
You need to get all the things the the 'else' should affect into the code block:
Code: Select all
else (randomNumberGuess == randomGeneratedNumber) // <-- No semicolon
{
stillGuessing = false; // <-- In the code block!
cout << "CORRECT!!!11one!11!" << endl;
}
And another thing, you forgot an 'if':
else (randomNumberGuess == randomGeneratedNumber)
should be:
else if(randomNumberGuess == randomGeneratedNumber) .
you can't put a condition after an 'else', if you want to put a condition after it, you have to put an 'if'...
Remember, all of this applies to all control statements (while, if, else etc.) So your code should look like this:
Code: Select all
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(static_cast<unsigned>(time(0)));
int randomGeneratedNumber = rand() % 10 + 1;
int randomNumberGuess = 0;
cout << "Guess a number 1-10: ";
cin >> randomNumberGuess;
bool stillGuessing = true;
while (stillGuessing) // <-- No semicolon
{
if (randomNumberGuess > randomGeneratedNumber) // <-- No semicolon
{
cout << "Lowerrrrrrr! Try again!" << endl;
cin >> randomNumberGuess;
}
else if (randomNumberGuess < randomGeneratedNumber) // <-- No semicolon
{
cout << "HIGHER!!! Try again!" << endl;
cin >> randomNumberGuess;
}
else if (randomNumberGuess == randomGeneratedNumber) //<-- added 'if'
{
stillGuessing = false; // <-- All code inside the code block ({..})
cout << "CORRECT!!!11one!11!" << endl;
}
}
system ("pause");
return 0;
}