Write a number guessing game in which the computer selects a random number in th
ID: 3935026 • Letter: W
Question
Write a number guessing game in which the computer selects a random number in the range of 0 to 100. and users get a maximum of 20 attempts to guess it. At the end of each game, users should be told whether they won or lost, and then be asked whether they want to play again. When the user quite, the program should output the total number of wins and losses. To make the game more interesting, the program should van the working of the messages that it outputs for winning, for losing, and for asking for another game. Use Do-While and switch statements where possible. Write a C + + program that reads a time in numeric form and prints it out in English. The time is input as hours and minutes, separated by a space. Hours are specified in 24-hour military time (e.g. 15 is 3pm) but the output should be in 12-hour AM/PM format. Note that noon and midnight are special cases. Here are some examples:Explanation / Answer
Answering 1st question :
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
string winningMessages[3] = {"You won!!!","Congratulations, you guessed it correctly", "Correct guess!!!"};
string losingMessages[3] = {"Sorry, you lost it","Sorry, allowed attempts are finished","Sorry, good luck next time"};
string playAgainMessages[3] = {"Wanna play again? ","Wanna try again? ","Wanna have another round? "};
void number_guessing_game(){
int wins = 0;
int losses = 0;
char inp;
while(1){
cout << "Let the games begin!!! "<<endl;
int number = rand()%101; //for number in range 0 to 100
bool Won = false;
for(int attemptNo = 1; attemptNo <= 20; attemptNo++ ){
int guess;
cout << "Attempt " << attemptNo << ", Your guess: ";
cin >> guess;
if( guess == number ){
Won = true;
break; }
cout << "Sorry, it is incorrect" << endl;
}
int msgNo = rand()%3;
if( Won ){ cout << winningMessages[msgNo] << endl; wins++; }
else{ cout << losingMessages[msgNo] << endl; losses++; }
msgNo = rand()%3;
cout << playAgainMessages[ msgNo ];
cin >> inp;
if( inp == 'N' || inp == 'n' ){ break; }
}
cout << "Total number of wins: " << wins << endl;
cout << "Total number of losses: " << losses << endl;
}
int main(){
number_guessing_game();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.