//Includers
#include <iostream>
#include <iomanip>
#include <ctime>
usingnamespace std;
//Function to print out the welcome banner
void printwelcome()
{
//Display Chart
cout << "******************************************" << endl;
cout << "**** Welcome to the High-Low game! ****" << endl;
cout << "******************************************" << endl;
// Display difficulty
cout << "Choose the difficulty level: " << endl;
cout << setw(16) << "1. Easy" << endl;
cout << setw(18) << "2. Medium" << endl;
cout << setw(16) << "3. Hard" << endl;
}
//This is the main function that contian the levels and user inputs
int main()
{
srand(time(NULL)); //the function that generates random numbers
int number = rand() % 100 + 1; //the range of the random numbers
int lvl;
int guess;
int triesx = 0; //The guess is stored here
int tries = 0; //The number of tries stored here
char answer = 'n';
char digit;//The answer to the question is stored here
digit = 'm';
do {
printwelcome();
//
cout << "Number Generated: " << number << endl;
//Makes Sure to loop if someone doesnt enter a valid level number
while (true)
{
cout << "Level: ";
cin >> lvl;
if (lvl == 1 || lvl == 2 || lvl == 3)
{
break;
}
else
{
cout << "This is not a level" << endl;
cout << "Please choose a number 1-3 for difficulty" << endl;
}
}
//This is level Easy to user get 20 tries
if (lvl == 1)
{
tries = 20;
cout << "You choose Easy!" << endl;
while (tries <= 20 && tries >= 1
&& digit == 'm' || digit == 'M')
{
cout << "You have " << tries << " Tries" << endl;
cout << "Enter a number between 1 and 100: " << endl; //The user is asked for a guess
cout << "Guess here: ";
cin >> guess; //The guess is stored here
tries = tries - 1;
triesx++;
if (guess == 0 || guess > 100) //If statement that produces an error message if user enters a number out of the peramiters
{
cout << "This is not an option try again/n" << endl; //Error message
}
if (number == guess) //If the user guesses the number
{
cout << "Tries left: " << tries << endl;
tries++;
tries = tries - 20;
cout << "Congratualtions!! " << endl; //Message printed out if the user guesses correctly
cout << "You got the right number in " << triesx << " tries" << endl; //Lets the user know how many guess they used
}
if (number < guess)//if the guess is to high
{
cout << "Too high" << endl; //This message prints if guess it to high
}
if (number > guess) //if the guess is to low
{
cout << "Too low" << endl; //This message prints if the guess is to low
}
if (tries >= 20) //If the user uses all their guesses
{
cout << "You've run out of tries!" << endl; //The message that prints when a user is out of guesses
}
}
}
//This is level Medium the user get 15 tries
if (lvl == 2)
{
tries = 15;
cout << "You choose Easy!" << endl;
while (tries <= 15 && tries >= 1 && digit == 'm' || answer == 'M')
{
cout << "You have " << tries << " Tries" << endl;
cout << "Enter a number between 1 and 100: " << endl; //The user is asked for a guess
cout << "Guess here: ";
cin >> guess; //The guess is stored here
tries = tries - 1;
triesx++;
if (guess == 0 || guess > 100) //If statement that produces an error message if user enters a number out of the peramiters
{
cout << "This is not an option try again/n" << endl; //Error message
}
if (number == guess) //If the user guesses the number
{
cout << "Tries left: " << tries << endl;
tries++;
tries = tries - 15;
cout << "Congratualtions!! " << endl; //Message printed out if the user guesses correctly
cout << "You got the right number in " << triesx << " tries" << endl; //Lets the user know how many guess they used
}
if (number < guess)//if the guess is to high
{
cout << "Too high" << endl; //This message prints if guess it to high
}
if (number > guess) //if the guess is to low
{
cout << "Too low" << endl; //This message prints if the guess is to low
}
if (tries >= 15) //If the user uses all their guesses
{
cout << "You've run out of tries!" << endl; //The message that prints when a user is out of guesses
}
}
}
//This is level Hard the user get 10 tries
if (lvl == 3)
{
tries = 10;
cout << "You choose Easy!" << endl;
while (tries <= 10 && tries >= 1 && digit == 'm' || digit == 'M')
{
cout << "You have " << tries << " Tries" << endl;
cout << "Enter a number between 1 and 100: " << endl; //The user is asked for a guess
cout << "Guess here: ";
cin >> guess; //The guess is stored here
tries = tries - 1;
triesx++;
if (guess == 0 || guess > 100) //If statement that produces an error message if user enters a number out of the peramiters
{
cout << "This is not an option try again/n" << endl; //Error message
}
if (number == guess) //If the user guesses the number
{
cout << "Tries left: " << tries << endl;
tries++;
tries = tries - 10;
cout << "Congratualtions!! " << endl; //Message printed out if the user guesses correctly
cout << "You got the right number in " << triesx << " tries" << endl; //Lets the user know how many guess they used
}
if (number < guess)//if the guess is to high
{
cout << "Too high" << endl; //This message prints if guess it to high
}
if (number > guess) //if the guess is to low
{
cout << "Too low" << endl; //This message prints if the guess is to low
}
if (tries >= 10) //If the user uses all their guesses
{
cout << "You've run out of tries!" << endl; //The message that prints when a user is out of guesses
}
}
}
cout << "Do you want to restart Y/N ?";
cin.ignore(256, '\n');
cin.get();
} while (answer == 'n' || answer == 'N');
//System pause here
system("pause");
return 0;
}/*
*/
okay so I took char answer = 'n'; out and made it char answer;
then it gave me an error
so I went down to the cin.get();
changed it to answer = cin.get()
but it still doesn't loop the program if I choose y for yes. it just closes it
Take a look at line 204, } while (answer == 'n' || answer == 'N');
It will continue to loop, because char answer = 'n'; . What you may want to do is change the while statement to } while (answer != 'n' && answer != 'N');
1 2 3 4 5 6
cout << "Do you want to restart Y/N ?";
cin >> answer;
//cin.ignore(256, '\n');
//cin.get();
} while (answer != 'n' && answer != 'N');
If you want to generate a random number every restart, then you may want to place the int number = rand() % 100 + 1; inside the do/while loop. Furthermore, if you want to reset the number of tries the user takes to answer the question on every restart, then you should put it inside the do/while loop, as well, like so:
1 2 3 4
do {
printwelcome();
int triesx = 0; //The guess is stored here
int number = rand() % 100 + 1; //the range of the random numbers
Take a look at the lines 60, 109, 156. They all indicate Easy. I hope it helps.
It works !Thank you Kevin yours worked! Chicofeo thank you I tried yours and it worked but kevin got to you before you did. Thank you so much i've been sitting here for a 2 hours trying to get it to work!