A Write a C program to implement the Number Guessing Game In
A. Write a C++ program to implement the Number Guessing Game. In this game the computer chooses a random number between 1 and 100, and the player tries to guess the number in as few attempts as possible. Each time the player enters a guess, the computer tells him whether the guess is too high, too low, or right. Once the player guesses the number, the game is over. You can use the following functions to generate the random numbers: srand(time(0)); //seed random number generator number = rand() % 100 + 1; // random number between 1 and 100 You may need to include the #include header to your program.
B. Update the game to ask the player if he would like to play again.
C. Update the game to print the number of attempts the player needed to guess the number.
D. Update the game to limit the number of attempts to 7 attempts.
EASH PART OF QUESTION MUST BE IN SEPRATED CPP FILE .
Solution
#include <iostream>
#include <cstdlib>
#include <ctime>
int main(void)
{
srand(time(NULL));
while(true)
{
int number = rand() % 100 + 1;
int guess;
int tries = 0;
char answer;
while(true)
{
std::cout << \"Enter a number between 1 and 100 (\" << 20 - tries << \" tries left): \";
std::cin >> guess;
std::cin.ignore();
if(tries >= 20)
{
break;
}
if(guess > number)
{
std::cout << \"Too high! Try again.\ \";
}
else if(guess < number)
{
std::cout << \"Too low! Try again.\ \";
}
else
{
break;
}
tries++;
}
if(tries >= 20) {
std::cout << \"You ran out of tries!\ \ \";
}
else
{
std::cout<<\"Congratulations!! \" << std::endl;
std::cout<<\"You got the right number in \" << tries << \" tries!\ \";
}
while(true)
{
std::cout << \"Would you like to play again (Y/N)? \";
std::cin >> answer;
std::cin.ignore();
if(answer == \'n\' || answer == \'N\' || answer == \'y\' || answer == \'Y\') {
break;
}
else
{
std::cout << \"Please enter \\\'Y\\\' or \\\'N\\\'...\ \";
}
}
if(answer == \'n\' || answer == \'N\') {
std::cout << \"Thank you for playing!\";
break;
}
else
{
std::cout << \"\ \ \ \";
}
}
std::cout << \"\ \ Enter anything to exit. . . \";
std::cin.ignore();
return 0;
}



