Hello, below is my code for a HiLo game. I added the try again option, and now when you guess the right number, it still says the number is too high or too low, until you continue to guess that number. It also doesn't compute the right amount of tries. Could anyone tell me whats wrong? Thank you to anyone who can help (:
Code:
#include <iostream>
#include <ctime>
using namespace std;
int main ()
{
int range;
int num = 0;
int guess = 0;
char answer;
srand(time(0));
cout << "Hello! Welcome to the HiLo game!" << endl;
cout << "To start, please enter the range: ";
cin >> range;
while (answer != 'N' && answer != 'n')
do
{
int tries = 0;
num = rand() % range + 0;
{cout << "Enter a guess between 0 and "<<range<<":";
cin >> guess;
tries++;}
if (guess > num)
{cout << "Too high! Try again!\n\n" << endl;}
else if (guess < num)
{cout << "Too low! Try again!\n\n" << endl;}
else
{cout << "\nCorrect! It took you " << tries << " tries!\n";
cout << "Would you like to play again? (Y for yes N for no)";
cin >> answer;}
if ( guess == -999)
{cout << "The correct number was:" << num << endl;}
{if ( guess > range || guess < 0)
{cout << "Error: invalid guess, try again." << endl;
tries--;}}
}
while (guess != num);
system("pause");
}