Question

In: Computer Science

Write a C++ console application to simulate a guessing game. Generate a random integer between one...

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.

Solutions

Expert Solution

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 :


Related Solutions

JAVA Write a number guessing game that will generate a random number between 1and 20 and...
JAVA Write a number guessing game that will generate a random number between 1and 20 and ask the user to guess that number. The application should start by telling the user what the app does. You should then create the random number and prompt the user to guess it. You need to limit the number of times that the user can guess to 10 times. If the user guesses the number, display some message that tell them that they did...
Random Number Guessing Game Write a program in C++ that generates a random number between 1...
Random Number Guessing Game Write a program in C++ that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high. Try again.” If the user’s guess is lower than the random number, the program should display “Too low. Try again.” The program should use a loop that repeats until the user correctly guesses the random number....
Random Number Guessing Game C++. Write a program that generates a random number between 5 and...
Random Number Guessing Game C++. Write a program that generates a random number between 5 and 20 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display Too high. Try again. If the user’s guess is lower than the random number, the program should display Too low, Try again. The program should use a loop that repeats while keeping a count of the number of guesses...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number (int) between 0 and 100, call it N 2. Read user input (a guess) 3. check the number, if it's smaller than N, output "The number is larger than that" 4. If the input is larger than N, output "The number is smaller than that" 5. If the input is equal to N, output " You got it!", and exit 6. Repeat until the...
Part (a) Write a number guessing game using System.Collections.Generic.Dictionary. Generate 10 distinct random numbers in the...
Part (a) Write a number guessing game using System.Collections.Generic.Dictionary. Generate 10 distinct random numbers in the range of 1 to 20. Each random number is associated with a prize money (from 1 to 10000). Use a Dictionary to store the mapping between the random number and the prize money. Ask user for two distinct numbers, a and b, both from 1 to 20; if a and b are not distinct, or out of range, quit the program Lookup the prize...
Create the Guessing Game Application. The application should receive two integers from the user, namely minimum and maximum and generate a random integer from minimum through maximum, inclusive. It then should give the user five chances to guess the integ
In Python Guessing Game ApplicationCreate the Guessing Game Application. The application should receive two integers from the user, namely minimum and maximum and generate a random integer from minimum through maximum, inclusive. It then should give the user five chances to guess the integer. Each time the user makes a guess, the application should display one of two messages: “Guess higher” or “Guess lower”. If the user guesses the generated number the application should let her/him know and also display the...
guessing game in Java. It will have a guess input used for guessing the random number...
guessing game in Java. It will have a guess input used for guessing the random number that is generated from 1 - 100. When the user makes a guess it will tell them if the number is lower or higher than the guess. There is also a choice to give up which then reveals the correct number. The last choice will be new game which resets the random number. Last, the program should keep counts of tries. When the user...
Random Number Guessing Game (python) *use comments Write a program guess.py that generates a random number...
Random Number Guessing Game (python) *use comments Write a program guess.py that generates a random number in the range of 1 through 20, and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again." If the user's guess is lower than the random number, the program should display "Too low, try again." If the user guesses the number, the application should congratulate the...
Number guessing Game (20 Marks) Write a C program that implements the “guess my number” game....
Number guessing Game Write a C program that implements the “guess my number” game. The computer chooses a random number using the following random generator function srand(time(NULL)); int r = rand() % 100 + 1; that creates a random number between 1 and 100 and puts it in the variable r. (Note that you have to include <time.h>) Then it asks the user to make a guess. Each time the user makes a guess, the program tells the user if...
Develop a Java application to simulate a game played in an elementary classroom. In this game,...
Develop a Java application to simulate a game played in an elementary classroom. In this game, the teacher places herself in the center of a circle of students surrounding her. She then distributes an even number of pieces of candy to each student. Not all students will necessarily receive the same number of pieces; however, the number of pieces of candy for each student is even and positive. When the teacher blows a whistle, each student takes half of his...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT