Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

RandomGame.cpp (2pt) Write a program that asks the user to enter his/her name. T

ID: 3831923 • Letter: R

Question

RandomGame.cpp (2pt)

Write a program that asks the user to enter his/her name. Then begin a do while loop that asks the user to enter a number between 1 and 10.

Have the random number generator produce a number 1 and 10.

Display the user’s name and both numbers to the screen.

Compare the two numbers and report if the entered number is greater than, less than, or the same as the generated number.

Ask the user if he/she’d like to go again.

Say goodbye when finished playing.

If the user enters a number out of range, display an out of range message, then display the “ask again” code.

Sample run

Please enter your name: Tony

Please enter a number between 1 and 10: 8

        Tony, you entered 8 and the computer generated 8.

        Your number is the same.

        Go again? yes/no: yes

Please enter a number between 1 and 10: 7

        Tony, you entered 7 and the computer generated 4.

        Your number is greater than the computer's number.

        Go again? yes/no: yes

Please enter a number between 1 and 10: 12

        Tony, your number is out of range.

        Go again? yes/no: no

Goodbye for now.

!!!!! Please C++.

Explanation / Answer

#include <iostream>
#include <cstdlib>   
#include <ctime>
using namespace std;

int main()
{
string name, choice = "yes";
cout<<"Please enter your name: ";
cin >>name;
int n;
srand (time(NULL));
do{
int randNum = rand() % 10 + 1;
cout<<"Please enter a number between 1 and 10: ";
cin >> n;
if(n>0 && n <=10){
cout<<name<<", you entered "<<n<<" and the computer generated "<<randNum<<"."<<endl;
if(n > randNum){
cout<<"Your number is greater than the computer's number."<<endl;
}
else if(n < randNum){
cout<<"Your number is less than the computer's number."<<endl;
}
else{
cout<<"Your number is the same."<<endl;
}
}
else{
cout<<name<<", your number is out of range."<<endl;
}
cout<<"Go again? yes/no: ";
cin >> choice;
}while(choice=="yes");
return 0;
}

Output:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                                              

sh-4.2$ main                                                                                                                                                                                                                                                           

Please enter your name: Tony                                                                                                                                                                                                                                           

Please enter a number between 1 and 10: 5                                                                                                                                                                                                                              

Tony, you entered 5 and the computer generated 1.                                                                                                                                                                                                                      

Your number is greater than the computer's number.                                                                                                                                                                                                                     

Go again?  yes/no: yes                                                                                                                                                                                                                                                 

Please enter a number between 1 and 10: 6                                                                                                                                                                                                                              

Tony, you entered 6 and the computer generated 6.                                                                                                                                                                                                                      

Your number is the same.                                                                                                                                                                                                                                               

Go again?  yes/no: no