Question

In: Computer Science

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. Then the program should display “Congratulations. You figured out my number.” Also Enhance the program so it keeps a count of the number of guesses the user makes. When the user correctly guesses the random number, the program should display the number of guesses along with the message of congratulations.


d.  Use of sufficient comments to clarify use of syntax.                                                    
e.  Use of meaningful variable (identifier) in your program                                                
f.  Separate input, process, and output instructions by a comment (In-Process-Out)       
g. Working program without any error                                            

Solutions

Expert Solution

Hi, I have written a simple c++ program as you requested. I have added appropriate comments, I hope it will be easy to understand. Please find the code below.

#include <iostream>
using namespace std;

int main() {
    // Seed the time to generate random number
    srand(time(NULL)); 
    // generate a random Number
        int randomNumber = rand()%100+1;
        // flag to check if user has made the correct guess, initial value will be false
        bool guessCheck=false;
        // initiate total number of guesses as 0
        int totalGuesses=0;
        // loop until the user makes a correct guess , i.e. loop till guessCheck is true
        while(!guessCheck){
            // temporary variable to store user input
            int userGuess;
            
            cout<<"guess a number between 1 and 100\n";
            
            // GET USER INPUT
            cin>>userGuess;
            // END USER INPUT
            
            // The totalGuesses count will now increase by one, as user made a guess
            totalGuesses++;
            
            // PROCESS
            // check if user has made the correct guess, i.e. check if it is equal to randomNumber
            if(userGuess==randomNumber){
                // if true print the required output
                
                // OUTPUT START
                cout<<"Congratulations. You figured out my number\n";
                cout<<"You have made "<<totalGuesses<<" guesses \n";
                // OUTPUT END
                
                // now that user has made the correct guess, set guessCheckas true to stop the loop
                guessCheck=true;
            }
            
            // if userGuess is greater than randomNumber print too high, try again
            
            if(userGuess>randomNumber){
                // OUTPUT START
                cout<<"Too high. Try again.\n";
                // OUTPUT END
            }
            
            // if userGuess is less than randomNumber print too low, try again
            if(userGuess<randomNumber){
                // OUTPUT END
                cout<<"Too low. Try again.\n";
                // OUTPUT END
            }
        }
        return 0;
}

Related Solutions

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...
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...
Write a program in C++ coding that generates a random number between 1 and 500 and...
Write a program in C++ coding that generates a random number between 1 and 500 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. Count the number...
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...
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...
C++. Write a program that generates a random number between 5 and 20 and asks the...
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 the user makes until...
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...
Write a program that generates a random number between 1 and 100 and asks the user...
Write a program 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. Your program should also keep a...
1. [100] Create a C program for a number guessing game. Here are the requirements: a....
1. [100] Create a C program for a number guessing game. Here are the requirements: a. Your program generates a random number between -100 and 100 and keeps asking the user to guess the number until the user guesses it correctly. Your random number generator should be implemented as a C function which takes min and max values as input parameters and returns a random number between those values including the min and the max. b. If the user guesses...
*Write in C* Write a program that generates a random Powerball lottery ticket number . A...
*Write in C* Write a program that generates a random Powerball lottery ticket number . A powerball ticket number consists of 5 integer numbers ( between 1-69) and One power play number( between 1-25).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT