While loop, and if-else-statement In this C++ project, you will write a program
ID: 3674515 • Letter: W
Question
While loop, and if-else-statement
In this C++ project, you will write a program to play a guessing game with the user. The program should randomly generate an integer between 1 and 100, and then ask the user to try to guess the number. If the user guesses incorrectly, the program should ask them to try again until the guess is correct; when the guess is correct, the program should print a congratulatory message. (See page 996 of the textbook for description of functions rand () and srand(int)).
Follow steps below to develop your program:
- Use the rand() function and the modulo operator (%) to generate a random number between 1 and 100 inclusive (this is the target value).
- Ask the user to guess the number and read in the value (this is the guess value).
- Add a while-loop where the loop is entered only if the guess value is incorrect (i.e., the guess value is not equal to the target value). If the guess is incorrect, give them a hint and then ask them to reenter the value. If the guess is correct, break out of the loop and print a congratulatory message (e.g., You guessed correctly!) before ending the program.
Each time through the loop do the following:
If the value entered by the user is greater than the target value print:
The number is lower. Try again.
If the value entered by the user is less than the target value print:
The number is higher. Try again.
Prompt for and read in the value of the new guess
SAMPLE OUTPUT: (Assuming target is set to 57):
Programmer: hhhhhhhhhhh
Course: 111111,
Lab: #1
Guess a number between 1 and 100: 80
The number is lower. Try again.
Guess a number between 1 and 100: 30
The number is higher. Try again.
Guess a number between 1 and 100: 60
The number is lower. Try again.
Guess a number between 1 and 100: 57
You guessed correctly!
Now, modify the program so that it prints a score after the congratulatory message. The score is the number of tries till the user guesses the right number. The score is 4 for the sample output above.
Next, modify the program so that it allows the user to play the game as many times as (s)he wishes. For every new game the computer must select a new target value. The program will display a game summary (number of games played, and the best score) when the user quits. (demo this before moving on to the next step).
SAMPLE OUTPUT: Assuming that the computer selects 57 for the first game and 15 for the second one.
Programmer: hhhhhhhh
Course: 1111111
Lab: #1
Date: 01-01-2000
Guess a number between 1 and 100: 80
The number is lower. Try again.
Guess a number between 1 and 100: 30
The number is higher. Try again.
Guess a number between 1 and 100: 60
The number is lower. Try again.
Guess a number between 1 and 100: 57
You guessed correctly!
Your score is: 4
Play another game, yes (or no): Yes
Guess a number between 1 and 100: 20
The number is lower. Try again.
Guess a number between 1 and 100: 10
The number is higher. Try again.
Guess a number between 1 and 100: 15
You guessed correctly!
Your score is: 3
Play another game, yes (or no): no
Game summary
Number of games played: 2
Best score: 3
Finally, write another C++ program similar to the one above but this time switch the role of the computer and the user. The user now chooses a number by entering it at the keyboard, and the computer will attempt to guess what that number is.
SAMPLE OUTPUT:
Programmer: hhhhh
Course: 1111111
Lab: #1
Date: 01-01-2000
Enter a target value: 75
Computer guesses: 21 15 100 86 42 75
Score: 6
Play another game, yes (or no): no
Game summary
Number of games played: 1
Best score: 6
Explanation / Answer
1)
#include <iostream>
#include <stdlib.h>
using namespace std;
int main ()
{
int randomNo,guess=0;
randomNo = rand() % 100 + 1;
do{
cout<<"Enter guess: ";
cin>>guess;
if(guess<randomNo){
cout<<"The number is higher. Try again. ";
}
else if(guess>randomNo){
cout<<"The number is lower. Try again. ";
}else{
cout<<"You guessed correctly!";
}
}while(guess!=randomNo);
return 0;
}
2)
#include <iostream>
#include <stdlib.h>
using namespace std;
int main ()
{
int randomNo,guess,score=0;
cout<<"Enter a random no between 1 and 100:";
cin>>randomNo;
cout<<"Computer guesses: ";
do{
guess=rand() % 100 + 1;
cout<<guess<<" ";
if(guess<randomNo){
score+=1;
}
else if(guess>randomNo){
score+=1;
}else{
cout<<" Score: "<<score<<" ";
}
}while(guess!=randomNo);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.