In: Computer Science
Write a C++ console application to simulate a guessing game. Generate a random integer between one and 100 inclusive. Ask the user to guess the number. If the user’s number is lower than the random number, let the user know. If the number is higher, indicate that to the user. Prompt the user to enter another number. The game will continue until the user can find out what the random number is. Once the number is guessed correctly, display a message to tell the user his/her guess is correct. Also, display the number of guesses that lead the user to the correct number. Label your output properly. Do not accept numbers outside the range; and do not count invalid numbers as an attempt. Ask the user if they want to continue the game. If so, generate another random number and allow the user to play again. When the user decides to stop, end the program.
C++ Code :
#include<iostream>
#include<stdlib.h>
// for using rand() and srand()
#include<ctime>
// for using time()
using namespace std;
int main()
{
while(true)
// iterating till user wants
to play the game or until user exits the game
{
srand(time(NULL));
// for
generating different random numbers everytime
int value = rand() % 100 +
1;
// generating random numbers between 1 and 100
inclusive
cout << "\nRandomly generated
number is : " << value << "\n";
// printing Randomly
generated number
int count = 0;
// for counting the total
number of guesses
while(true)
//
iterating until user guesses the correct number
{
int
x; // for storing
user input of guessed number
cout <<
"\n\nNow, Guess a number : ";
// asking for the input of guessed number
cin >>
x;
// taking input of guessed number by user
if(x<1
&& x>100)
// if the guessed number if
out of range
{
cout << "\nGuessed number is of outside
the range ! Please try again";
continue;
// continue for asking again input of guess
number
}
if(x >
value) // if the
guessed number is greater than the Randomly generated number
{
cout << "\nGuessed number is greater than
the Randomly generated number";
// printing the required messgae
count++;
// incrementing the count of guesses
continue;
// continue for asking again input of guess
number
}
else
if(x<value) //
if the guessed number is smaller than the Randomly generated
number
{
cout << "\nGuessed number is smaller than
the Randomly generated number";
// printing the required messgae
count++;
// incrementing the count of guesses
continue;
// continue for asking again input of guess
number
}
else
// if the
guessed number is correct and equal to the Randomly generated
number
{
cout << "\nCongrats! Guessed
correctly"; //
printing the required messgae
count++;
// incrementing the count of
guesses
cout << "\n\nYou have lead " <<
count << " number of guesses to the correct
number."; //
printing the total number of guesses lead
break;
// breaking out from the
loop
}
}
char choice;
// for
storing the user's choice whether user wants to play the game again
or exit the game
cout << "\n\nDo you want to
play this game again (y/n) ? ";
// asking for the input of user choice
cin >> choice;
// taking input of user
choice
if(choice=='n')
// if user
wants to exit the game
{
cout<<"\n\nThanks for playing the game, Bye\n";
//
printing the required messgae
break;
// and breaking out of the while loop for
exiting the game
}
}
}
Output :