Write a C++ program to realize the game of guessing the number. Generally, the g
ID: 3887105 • Letter: W
Question
Write a C++ program to realize the game of guessing the number. Generally, the game will generate a random number and the player has to find out the number. In each guess, the program will give you a feedback as your guess is correct, too large, or too small. Then the player play repetitively until find out the number. Specifically, the game plays as the following. a). When the game is started, it will generate a random integer number in the range of [0, 99]. As a player, you won't know the number generated. You can use the following code to generate such a number. srand time(NULL)): int number = rand() % 100: Note that you need to include library "time.h" in your program to make this code work. b).Your program would ask the user to input a number that is smaller than 100 c). Your program would take your input and compare it with the number generated in step a). If your number is larger than the number generated in step a), the program should give a feedback sayingExplanation / Answer
Program:
#include <iostream> //you can replace it with iostream.h if using other compiler
#include <cstdlib> //you can replace it with stdlib.h if using other compiler
#include <ctime> //you can replace it with time.h if using other compiler
using namespace std;
int main()
{
int guess;
srand(time(NULL)); //seed random number generator
int number = rand() % 100; // random number between 1 and 100
cout << "Guess Number Game ";
do
{
cout << "Enter a number that is smaller than 100 : ";
cin >> guess; //Stores player's guess
//we compare player's guess
if (guess > number)
cout << "Your guess is too large. ";
else if (guess < number)
cout << "Your guess is too small. ";
else
cout << " Bingo! You got the number " << number << " ";
} while (guess != number);
cout << " Thank you for playing! ";
cin.ignore();
cin.get();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.