Question

C++ Guessing game. Create a project titled Lab4_Guess with a single file titled guess.cpp Program the...

C++

Guessing game. Create a project titled Lab4_Guess with a single file titled guess.cpp Program the following game. The computer selects a random number between 1 and 100 and asks the user to guess the number. If the user guesses incorrectly, the computer advises to try larger or smaller number. The guessing repeats until the user guesses the number. The dialog should look as follows (user input is shown in bold): Guess a number between 1 and 100: 50 try larger: 75 try smaller: 68 try larger: 71 you got it! For your program, you need to use predefined randomization functions rand() and srand() as well as the time function time() Function time() returns current time (as kept by the computer) in the number of seconds passed since midnight, January 1, 1970. You need to include ctime to use this function. The function takes one argument but for this assignment the argument value is a special named constant nullptr. Check this program for an example usage. To use functions rand() and srand() you need to include cstdlib Function srand() initializes the random number generator of the program. It takes a single integer argument. The value passed to srand() determines the values output by rand(). This value is called seed. rand() takes no arguments. Every time rand() is invoked it returns an integer value between 0 and constant RAND_MAX defined in cstdlib. Note that the sequence of values returned by repeated calls of rand() is uniquely determined by the seed passed to srand(). That is, if srand() is passed the same seed, rand() is going to return the same sequence of numbers. This is helpful for debugging. Check this program for an example of random number manipulation. Hints: Use fixed values of the seed for your initial debugging, use the output of time() as an unpredictable seed to start the guessing game once you debugged your program. Note that random value produced by rand() is out of the required range of 1-100. To get the random number into the required range, remainder it by 100 (that puts the number in 0-99 range) and then add one. Do not put srand() inside the loop or anywhere, where it may be invoked more than once - it resets the random number generator.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here's the code for the guessing game. Appropriate comments are written in the code itself. You can consider changing the seed value, if you want to try something else.

Screenshot of the code

Text version of the code

//include all the required libraries
#include <ctime>
#include <cstdlib>
#include <iostream>
// using std namespace
using namespace std;

// Logic
int main()
{
   // num is random number
   // guess is user input
   int num, guess;
   srand(time(0)); // seed random number generator
   // Modul to make number between 1 and 100
   num = rand() % 100 + 1;
   cout << "Guessing game ";

   // Game Logic
   do
   {
       cout << "Guess a number between 1 and 100: ";
       cin >> guess;
       if (guess > num)
           cout << "Try smaller ";
       else if (guess < num)
           cout << "Try Larger ";
       else
           cout << "You got it!";
   } while (guess != num);
   // Hold the screen after finishing the game
   cin.ignore();
   cin.get();
   return 0;
}

Note - If this answer is helpful, please consider giving a thumbs up.Your feedback will be truly appreciated.

Add a comment
Know the answer?
Add Answer to:
C++ Guessing game. Create a project titled Lab4_Guess with a single file titled guess.cpp Program the...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, al...

    Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, allow the user to try up to 10 times to guess the number, and tell the user if each guess is too high, too low, or correct. The actual game portion must be a function...you can use more than one function in your program. The amount...

  • Specification Write a program that allows the user to play number guessing games. Playing a Guessing...

    Specification Write a program that allows the user to play number guessing games. Playing a Guessing Game Use rand() function from the Standard C Library to generate a random number between 1 and 100 (inclusive). Prompt the user to enter a guess. Loop until the user guesses the random number or enters a sentinel value (-1) to give up. Print an error message if the user enters a number that is not between 1 and 100. Print an error message...

  • python code for guessing game users enter the number the computer will guess 8:38 PM 100...

    python code for guessing game users enter the number the computer will guess 8:38 PM 100 nstructure.com 1. Number guessing game : (5 points) 1) Requirements: Your program will ask the user to enter a number between 1 and 1000. Then it will try to guess the correct number with the guaranteed minimal average number of guesses. Which search algorithm should you use? 2) Expected Output: Enter a number between 1 and 1000? 784 Computer guesses: 500 Type 'l' if...

  • Write a C++ program to play the number guessing game with user as follows.

    Write a C++ program to play the numberguessing game with user as follows.The user determines a number between 1~100.The program has 4 tries to guess the number byproviding a range (low, high) on each try.The user tells the program whether the number isbelow, within, or above the range.The program announces a game loss after 4 incorrecttries.

  • Has to be written in C++. Visual studio. Write a C++ program to realize the game...

    Has to be written in C++. Visual studio. Write a C++ program to realize the game of guessing the number. Generally, the game will generate a random number and the player has to find out the number. In each guess, the program will give you a feedback as your guess is correct, too large, or too small. Then the player play repetitively until find out the number. Specifically, the game plays as the following. a). When the game is started,...

  • A Backwards Guessing Game For this assignment, you will be a making a guessing game with...

    A Backwards Guessing Game For this assignment, you will be a making a guessing game with a twist. You will play the role of the computer, and the computer will try to guess the number. You will think of a number (you don’t have to read it in as input) between 1 and 100. You will create a loop that runs 5 times. In this loop, the computer will try to guess the number in the range it knows is...

  • Please write a C# program that where a player will play a guessing game with the...

    Please write a C# program that where a player will play a guessing game with the range of 1-100. Each time the play guesses a number, and it is not correct the program should indicate if the number is more or less than what the player guessed. The play should be able to guess until the correct number has been guessed. If an invalid number is entered, such as: 1.24 (real number), or asd (string). The program should tell user...

  • Write the Code in C program. Create a random password generator that contains a user-specified number...

    Write the Code in C program. Create a random password generator that contains a user-specified number of characters. Your program should prompt the user for the desired length of the password. Length of password: To create your password, use the random number generator in the stdlib.h C library. To generate random numbers, you will need the srand() and rand() functions. srand(seed) is used to "seed" the random number generator with the given integer, seed. Prompt the user for the seed...

  • Write a program that allows a user to play a guessing game. Pick a random number...

    Write a program that allows a user to play a guessing game. Pick a random number in between 1 and 100, and then prompt the user for a guess. For their first guess, if it’s not correct, respond to the user that their guess was “hot.” For all subsequent guesses, respond that the user was “hot”if their new guess is strictly closer to the secret number than their old guess and respond with“cold”, otherwise. Continue getting guesses from the user...

  • Python Program Python: Number Guessing Game Write a Python function called "Guess.py" to create a number...

    Python Program Python: Number Guessing Game Write a Python function called "Guess.py" to create a number guessing game: 1. Function has one input for the number to Guess and one output of the number of attempts needed to guess the value (assume input number will always be between 1 and 1000). 2. If no number was given when the function was called, use random.randint(a,b) to generate a random number between a=1 and b=1000. You will also need to add an...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT