Hello, I need some help with the below program: Write a program that plays a Hi-
ID: 646164 • Letter: H
Question
Hello,
I need some help with the below program:
Write a program that plays a Hi-Low game as follows:
Generate and store a random number between 0 and 200.
Prompt the user to guess the number.
Compare the user input to the generated number and output a statement indicating if it is higher or lower than the generated number.
Repeat steps 2 and 3 until the number is guessed. ( use a do-while loop)
When the correct number is guessed, output a statement stating that the player won and the number of tries it took to win.
Sample run:
Please enter a number between 0 and 200: 56
The number entered is too low.
Please enter a number between 0 and 200: 131
You win in 2 tries
Note:
The program should keep executing the loop till the correct number is guessed. The only way to stop the loop is to guess the right number.
Use an object of type Random to generate the random number.
Explanation / Answer
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main(){
int guess = 500, num,count = 0;
srand(time(0));
num = rand() % 201;
do{
cout << "Olease enter a number between 0 and 200 : ";
cin >> guess;
if(guess < num){
cout << "The number entered is too low ";
}
if(guess > num){
cout << "The number entered is too high ";
}
count++;
}while(guess != num);
cout << "You win in " << count << " tries ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.