#include <iostream>
#include <time.h>
usingnamespace std;
int main()
{
int number;//Stores the random number
int guess;//stores the guesses
int tries;//Stores the amount of tries.
srand(time(NULL));
number = (rand() % 10) + 1;//Generates Random Number between 1 and 5.
cout << "A Random number between 1 and 10 has been generated. Try and guess it in 3 tries" << endl; //Begins the game
bool done = false;//Bool to declare the games end.
while (done == false)//Will turn true when game end
{
cin >> guess;//Allows the user to input a guess
if (guess == number) //Checks if the guess equals the number generated
cout << "Good job! You correctly guessed the Number!" << endl; // If thy matched is tells you you got it right
done = true; // Ends the game since its correctly guessed.
if (guess != number)
cout << "That number is incorrect!" << endl;// This gets diplayed if the numbers don't match
tries + 1;
if (guess > 10)
cout << "That number is greater than 10! Please guess a number between 1 and 10." << endl;// Reminds the player to have a number lower then 10.
if (guess == 0)
cout << "The number you guessed is 0! Please guess a number between 1 and 10." << endl;// Reminds the player to have a number can't be 0.
if (tries == 3)
cout << "Your 3 tries are up! You lost!" << endl;// Tells the player that there 3 tries are up!
return (0);
}}
Tried it. Nothing. Every time I run the program it picks a number. lets you input your guess. Then it says incorrect or correct. Then closes. I want it to be like. Incorrect. Then lets you guess 2 more times. Then if you still don't get it. Closes.
#include <iostream>
#include <time.h>
usingnamespace std;
int main()
{
int number;//Stores the random number
int guess;//stores the guesses
int tries = 0;//Stores the amount of tries.
srand(time(NULL));
number = (rand() % 10) + 1;//Generates Random Number between 1 and 5.
cout << "A Random number between 1 and 10 has been generated. Try and guess it in 3 tries" << endl; //Begins the game
bool done = false;//Bool to declare the games end.
while (done == false)//Will turn true when game end
{
cin >> guess;//Allows the user to input a guess
if (guess == number) //Checks if the guess equals the number generated
cout << "Good job! You correctly guessed the Number!" << endl; // If thy matched is tells you you got it right
done = true; // Ends the game since its correctly guessed.
if (guess != number) {
cout << "That number is incorrect!" << endl;// This gets diplayed if the numbers don't match
++tries;
}
if (guess > 10)
cout << "That number is greater than 10! Please guess a number between 1 and 10." << endl;// Reminds the player to have a number lower then 10.
if (guess == 0)
cout << "The number you guessed is 0! Please guess a number between 1 and 10." << endl;// Reminds the player to have a number can't be 0.
if (tries == 3)
cout << "Your 3 tries are up! You lost!" << endl;// Tells the player that there 3 tries are up!
}
return (0);
}
The if statement on line 21 needs curly braces too. Line 24 is being executed without condition, thus closing the loop every time. Remember to always use curly braces to delimit a statement block if you have more than one statement to be executed on condition.