Question

Write a program that asks the user to guess a random number. The program should use...

Write a program that asks the user to guess a random number. The program should use a loop to prompt the user for the guess then display if the guess is 'too high' or 'too low'. In the end, display how many times it took to guess the number.

Use the attached template.  

// Chapter 5 - Lab exercise, Random Number Guessing Game
//
// Name: 
//
#include <iostream>
#include <cstdlib>              // Needed to use random numbers
#include <ctime>                // Needed to call the time function
using namespace std;

int main()
{
        const int MAX_NUM = 100;    // The maximum random value used
        unsigned seed;              // "Seed" for the random number generator
        int secret,            // The computer's randomly generated number
            guess;                  // The user's current guess

        // Seed the random generator and get a random integer between 1 and MAX_NUM
        seed = time(0);
        srand(seed);
        secret = rand() % MAX_NUM + 1;

        // Explain the game and get the user's first guess
        cout << "I am thinking of a number between 1 and " << MAX_NUM << ". \n";
        cout <<   "Can you guess what it is?  Enter your guess. ";
        cin  >> guess;
        cout << endl;
        

        // Use a loop to prompt the user for the value of the secret number. Display 
        // a message if the user's guess it too high or too low.  Print how many guesses
        // were required to guess the number.


        return 0;
}

Homework Answers

Answer #1
// Chapter 5 - Lab exercise, Random Number Guessing Game
//
// Name: 
//
#include <iostream>
#include <cstdlib>              // Needed to use random numbers
#include <ctime>                // Needed to call the time function
using namespace std;

int main()
{
        const int MAX_NUM = 100;    // The maximum random value used
        unsigned seed;              // "Seed" for the random number generator
        int secret,            // The computer's randomly generated number
            guess;                  // The user's current guess

        // Seed the random generator and get a random integer between 1 and MAX_NUM
        seed = time(0);
        srand(seed);
        secret = rand() % MAX_NUM + 1;

        // Explain the game and get the user's first guess
        cout << "I am thinking of a number between 1 and " << MAX_NUM << ". \n";
        cout <<   "Can you guess what it is?  Enter your guess. ";
        cin  >> guess;
        cout << endl;
        

        // Use a loop to prompt the user for the value of the secret number. Display 
        // a message if the user's guess it too high or too low.  Print how many guesses
        // were required to guess the number.
        int count = 1;
        while(guess != secret){
            if(guess > secret){
                cout<<"Too high"<<endl;
            }
            else{
                cout<<"Too low"<<endl;
            }
            cout <<   "Can you guess what it is?  Enter your guess. ";
            cin>>guess;
            cout << endl;
            count+=1;
        }
        
        cout<<"Number of guesses = "<<count<<endl;

        return 0;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a script that plays a simple “guess the number” game with the user. It should...
Write a script that plays a simple “guess the number” game with the user. It should select a random integer in the range [0 - 10], ask the user to guess the value, and provide feedback of the form “too high”, “too low”, or “congratulations” as appropriate. After the correct value is guessed, the script should terminate.
PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program...
PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program that comes up with a random number and the player has to guess it. The program output can be like this: I am thinking of a number between 1 and 20. Take a guess. 10 Your guess is too low. Take a guess. 15 Your guess is too low. Take a guess. 17 Your guess is too high. Take a guess. 16 Good job!...
Write a c++ program that has the user guess a number between 1 and 100, you...
Write a c++ program that has the user guess a number between 1 and 100, you program should output correct guess if the user guess the right number, too high if they guess a number higher, too low if the guess a number lower and handle invalid input
Code a program to reproduce Madame Esmerelda’s clairvoyant test: Mme. Esmerelda conjures up a random number...
Code a program to reproduce Madame Esmerelda’s clairvoyant test: Mme. Esmerelda conjures up a random number 1-10 in her mind and commands the user to guess what it is. If the user’s guess is equal to (remember your double equal sign!) the conjured number, Mme. Esmerelda extends a job offer as a clairvoyant assistant. Add a user-driven loop that allows the user to play repeatedly if desired (but your program should conjure a new random number with every loop iteration)....
Write a program to pull a random number between 1-100 inclusive. Ask the user to guess...
Write a program to pull a random number between 1-100 inclusive. Ask the user to guess the number. If the random number is higher than the guess, print "Higher". If the random number is less than the guess, print "Lower". If the random number is equal to the quess, print "Correct!". Create a variable to count the number of guesses and intitialize it to zero. int guesses=0; Increase this variable by 1 every time the user enters a new guess....
This is a very short trivia game. See if the user can guess the year that...
This is a very short trivia game. See if the user can guess the year that MDC (formerly Dade County Junior College) first started offering classes (1960). If the user guesses incorrectly, ask if they want another attempt. Once the user gets it right, report the number of guesses needed and end the program. Initial Output: MDC was originally named Dade County Junior College. Output (per loop) In what year were classes first offered? [user types: 1980] That answer is...
Write a program that asks the user of a positive integer value. The program should use...
Write a program that asks the user of a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1,2,3,4,…,50. using namespace std;
c++ program can you please explain how it works and the process? Question: You will design...
c++ program can you please explain how it works and the process? Question: You will design a program in C++ that plays hangman using classes (polymorphism and inheritance).... Hangman Game CODE: #include <iostream> #include <cstdlib> #include<ctime> #include <string> using namespace std; int NUM_TRY=8; //A classic Hangman game has 8 tries. You can change if you want. int checkGuess (char, string, string&); //function to check the guessed letter void main_menu(); string message = "Play!"; //it will always display int main(int argc,...
Write a program that asks the user to enter the number of days and then converts...
Write a program that asks the user to enter the number of days and then converts that value to weeks and days. For example, it would convert 18 days to 2 weeks, 4 days. Display results in the following format: 18 days are 2 weeks, 4 days. Use a while loop to allow the user to repeatedly enter day values; terminate the loop when the user enters a nonpositive value, such as 0 or -20.
Please use Python 3 4). Write a program that asks the user to enter 10 numbers....
Please use Python 3 4). Write a program that asks the user to enter 10 numbers. The program should store the numbers in a list and then display the following data: • The lowest number in the list • The highest number in the list •The total of the numbers in the list • The average of the numbers in the list   Sample input & output: (Prompt) Enter Number 1: (User enter) 4 (Prompt) Enter Number 2: (User enter) 7...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT