C++ programming: Build a Guessing Game Program Write a program to implement the
ID: 3673386 • Letter: C
Question
C++ programming:Build a Guessing Game Program
Write a program to implement the Number Guessing Game. The main method should loop as long as the user wants to play the game again. Write a function called PlayGame( ). In this function write the code to play the game. In the 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, that game is over. C++ programming:
Build a Guessing Game Program
Write a program to implement the Number Guessing Game. The main method should loop as long as the user wants to play the game again. Write a function called PlayGame( ). In this function write the code to play the game. In the 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, that game is over.
Build a Guessing Game Program
Write a program to implement the Number Guessing Game. The main method should loop as long as the user wants to play the game again. Write a function called PlayGame( ). In this function write the code to play the game. In the 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, that game is over. Build a Guessing Game Program
Write a program to implement the Number Guessing Game. The main method should loop as long as the user wants to play the game again. Write a function called PlayGame( ). In this function write the code to play the game. In the 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, that game is over.
Explanation / Answer
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
void PlayGame()
{
int n,guess,count=0;
cout<<"Computer has chosen a random number between 1 and 100 (inclusive). Guess it!! ";
n = rand()%100+1;
guess = 0;
while(guess!=n)
{
cout<<"Enter your guess: ";
cin>>guess;
count++;
if(guess<n)
cout<<"Too low!! ";
else if(guess>n)
cout<<"Too high!! ";
}
cout<<"Right!! Total attempts = "<<count<<endl;
}
int main ()
{
srand(time(NULL));
bool quit = false;
char ch;
while(!quit)
{
cout<<"Do you want to play the guessing game? (y/n): ";
cin>>ch;
if(ch == 'y')
PlayGame();
else if(ch == 'n')
break;
else
cout<<"Wrong choice entered!! PLease try again ";
}
return 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.