Basically after every guess i want the loop to run until the user guesses right.
This is just a simple number guessing game.
I ask the user for more input after every wrong guess but the program ends after the second guess is given.
Can anyone help?
#import <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
srand((unsigned)time(0));
int random_integer;
int lowest=1, highest=10;
int range=(highest-lowest)+1;
for(int index=0; index<1; index++){
random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
}
cout << random_integer << endl;
int user_guess;
cout << "please try and guess the number between " << lowest << " and "
<< highest << endl << endl;
cin >> user_guess;
int number_of_tries = 0;
if (user_guess > 10)
{
cout << endl << "You're supposed to pick a number between " << lowest << " and "
<< highest << "!" << endl << endl;
cout << "Please pick another number..." << endl << endl;
number_of_tries = (number_of_tries + 1);
cin >> user_guess;
}
else if (user_guess < random_integer)
{
cout << endl << "Sorry, but that number is too LOW!" << endl << endl;
cout << "Please Guess again" << endl << endl;
number_of_tries = (number_of_tries + 1);
cin >> user_guess;
}
else if (user_guess > random_integer)
{
cout << endl << "Sorry, but that number is too HIGH!" << endl << endl;
cout << "Please Guess again" << endl << endl;
number_of_tries = (number_of_tries + 1);
cin >> user_guess;
}
else if (user_guess == random_integer)
{
cout << "Good guess, you won!" << endl << endl;
cout << "It took you " << number_of_tries << " tries" << endl << endl;
cout << "but... I bet you can't do that again!" << endl;
}
system("pause");
}