{
int i,game = 0,guess,count = 0;
int rando;
srand (time(0));
rando = rand() % 50 1;
cout << "****Welcome To The Game****\n";
cout << "1: Start the game\n";
cout << "2: End the game\n";
cin >> game;
while (game != 2 && count != 11)
{
cout << "Enter a number between 1 and 50: ";
cin >> guess;
count ;
if(guess == rando)
{
cout << "you got it!\n";
cout << "would you like to play again?\n";
cin >> game;
}
else if(guess < rando)
{
cout << "too low, try again " << endl;
}
else if(guess > rando)
{
cout << "Too high, try again" << endl;
}
if (count == 11)
{
cout << "too many guesses. the number was: " << rando << "." << endl;
cout << "would you like to play again?\n";
cin >> game;
}
if (game == 2)
{
cout << "@@@ Thank you. See you next time.@@@\n";
}
}
return 0;
}
when I start the game, it generates a new number every time I run the program, but not when I ask to play again. I tried putting it in the while, but that messes up the code. how do I fulfill the second option in the first sentence?
CodePudding user response:
int randomGame()
{
int gameMenuChoice = 0;
srand(time(0));
std::cout << "****Welcome To The Game****\n";
std::cout << "1: Start the gameMenuChoice\n";
std::cout << "2: End the gameMenuChoice\n";
std::cin >> gameMenuChoice;
while (gameMenuChoice != 2)
{
const int rando = rand() % 50 1;
int guess = 0;
for (int count = 0; count < 11 && guess != rando; count)
{
std::cout << "Enter a number between 1 and 50: ";
std::cin >> guess;
if (guess < rando)
{
std::cout << "too low, try again \n";
}
else if (guess > rando)
{
std::cout << "Too high, try again\n";
}
}
if (guess == rando)
{
std::cout << "you got it!\n";
}
else
{
std::cout << "too many guesses. the number was: " << rando << ".\n";
}
std::cout << "would you like to play again?\n"; // I think people will enter Y for yes here.
std::cin >> gameMenuChoice;
}
std::cout << "@@@ Thank you. See you next time.@@@\n";
return 0;
}
I have split you menu and game routine loops and simplified the logic a bit.