Write a C++ program to do the following number guessing game. First, the compute
ID: 673549 • Letter: W
Question
Write a C++ program to do the following number guessing game. First, the computer should pick a random number from 1 to 9 without telling you the number. You get three chances to try and guess the number the computer picked. After you make one of your guesses, the computer should tell you one of the following statements: "Close" if you are within 1 of correct answer. "Too low!" if you are below the number and not within 1. "Too high!" if you are above the number and not within 1. "Correct!" if you guessed the number exactly. If the player has not guessed the correct number after 3 tries, inform the player that they lost the game, tell them the correct answer and end the program. If the user correctly guesses the number ("Correct!" above), end the program.Explanation / Answer
#include<iostream>
#include<time.h>
using namespace std;
int main() {
srand(time(NULL));
int random = (rand()%9) + 1;
int count = 0 ;
int in;
while(count != 3) {
cout<<"What is the random number I guessed? :";
cin>>in;
if(random - in == 1 || random-in == -1) {
cout<<"Close!";
}
else if(in < random) {
cout<<"Too low!";
}
else if (in > random) {
cout<<"too high!";
}
else {
cout<<"Correct!";
cout<<" ";
return 0;
}
cout<<" ";
count++;
}
cout<<"You loose, the number was "<<random<<"!";
cout<<" ";
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.