Write a C++ program that implements a simple number guessing game. User enters a
ID: 3545288 • Letter: W
Question
Write a C++ program that implements a simple number guessing game. User enters a number between 1 and 10. The program then generates a random number between 1 and 10. If the user input number matches the generated number, then print a message to inform users that he/she has a correct guess. If the guess is not correct, allow the user to have two more chances to guess the correct number. At any time, if users enter 0, then the program should exit. The program should keep track of the correct and incorrect guesses and print the counts when user chooses to exit by entering 0. The program should generate a new random number only after user enters the correct guess or after user has tried 3 times.
When users give a wrong guess, it shows:
Not correct, try again:
When users give a wrong answer after the third trials for the same question, the program displays:
Explanation / Answer
#include <iostream>
#include<cstdlib>
#include <time.h>
using namespace std;
int main ()
{
int iMysNum, iUsrNum=11,count=0,countQue=0,countCor=0,countInC=0;
cout<< "Welcome to the number guessing game! ";
cout<< "For each game, you have at most, 3 chances to guess a secret number from 1 to 10 ";
/* initialize random seed: */
srand ( time(NULL) );
while(iUsrNum != 0){
/* generate mystery number: */
iMysNum = rand() % 10 + 1;
countQue++;
count=0;
while (count<3){
cout<< "Enter a number from 1 to 10. Enter 0 to exit: ";
cin>>iUsrNum ;
count++;
if (iMysNum < iUsrNum && iUsrNum != 0 &&count!=3){
cout<< "Not correct, try again: ";
countInC++;
}
if (iMysNum > iUsrNum && iUsrNum != 0 &&count!=3 ){
cout<< "Not correct, try again: ";
countInC++;
}
if(iMysNum == iUsrNum){
countCor++;
cout<< "Congratulation, correct! Let
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.