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...
In JAVA, write a number guessing game that randomly assigns a integer between one and twenty...
In JAVA, write a number guessing game that randomly assigns a integer between one and twenty for the user to guess. Write three methods to make this particular program work. 1. Have a method which generates a random number and returns the value to the main (stored as a variable) 2. Have a method which prompts the user for a guess. Return the guess to the main. 3. Evaluate the guess and the random "secret" number by parameters, and then...
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...
C# programming Write a number guessing game using System.Collections.Generic.Dictionary. Generate 10 distinct random numbers in the...
C# programming 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...
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...
Develop a C# console application that will implement one method that will return an integer value,...
Develop a C# console application that will implement one method that will return an integer value, one void method that will calculate an average, and one void overload for the calculate method that will compute a total. Please read the requirements below carefully. In Main: You will need four variables: an integer value to hold a random value, a double value to hold an average, a double value to hold a total, and a double value to hold an input...
Write a program to simulate a calculator. Generate two random numbers (between 1-15) and an ask...
Write a program to simulate a calculator. Generate two random numbers (between 1-15) and an ask the user for an arithmetic operator. Using a switch statement, your program has to perform the arithmetic operation on those two operands depending on what operator the user entered. Ask the user if he/she wants to repeat the calculations and if the answer is yes or YES, keep repeating. If the answer is something else, then stop after the first calculation. c++
Using pseudocode design a number guessing game program. The program should generate a random number and...
Using pseudocode design a number guessing game program. The program should generate a random number and then ask the user to guess the number. Each time the user enters his or her guess, the program should indicate it was too high or too low. The game is over when the user correctly guesses the number. When the game ends, the program should display the number of guesses that the user made.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT