I am new. Very new. I made this in a couple minutes and I was wondering whats wrong? When you type in a number it should either say correct or incorrect. But it says nothing and lets you keep on putting in numbers. Whats wrong? I think it has something to do with my loop.
#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 ends
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.
}
elseif (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);
}
Ok.. So this? I tried this and I did not work. I typed in a number. It said incorrect (because it was wrong) and then finished. I wanted it to give you 2 more chances.
#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);
}}