Question
Has to be written in C++. Visual studio.
Lab Work 3: 1. Write a c++ program to realze the game of guessing the number. Ge the game will generate a random number and the player has to find out the number. In each guess, the program will glve 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, it will generate a random integer number in the range of [o, 99). As a player, you wont know the number generated. You can use the following code to generate such a number. srand time(NULL)): int number-rand() % 100; Note that you need to include library time.h in your program to make this code work. b).Your program would ask the user to input a number that is smaller than 100 c). Your program would take your input and compare it with the number generated in step a). 1) If your number is larger than the number generated in step a), the program should give a feedback saying Your guess is too large. Thern repeat from step b) to ask the user to input a number again. 2) If your number is smaller than the number generated in step a), the program should give a feedback saying Your guess is too small. Then repeat from step b) to ask the user to input a number again. 3) If your number is exactly the same as the number generated in step a), the program should give a feedback saying Bingo! You got the number A, where A is the value of the number. Then exit the loop and go to step d). d). Your program prints Thank you for playing! and exits.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

#include <iostream> //you can replace it with iostream.h if using other compiler

#include <cstdlib> //you can replace it with stdlib.h if using other compiler

#include <ctime> //you can replace it with time.h if using other compiler

using namespace std;

int main()

{

int guess;

srand(time(NULL)); //seed random number generator

int number = rand() % 100; // random number between 1 and 100

cout << "Guess Number Game\n\n";

do

{

cout << "Enter a number that is smaller than 100 : ";

cin >> guess; //Stores player's guess

//we compare player's guess
if (guess > number)

   cout << "Your guess is too large.\n\n";

else if (guess < number)

   cout << "Your guess is too small.\n\n";

else

   cout << "\nBingo! You got the number " << number << "\n";

} while (guess != number);

cout << "\nThank you for playing!\n";

cin.ignore();

cin.get();

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Has to be written in C++. Visual studio. Write a C++ program to realize the game...
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
  • Number Guessing Game Games Write program in C++. For this game, the computer will select a...

    Number Guessing Game Games Write program in C++. For this game, the computer will select a random number between 1 and 100 (inclusive). The computer will then ask the (human) player to guess the number the computer has selected. After the player’s guess is input to the computer, the computer will output one of three responses, depending on the relationship of the number the player guessed to the number the computer selected: “Your guess was too low.” “Your guess was...

  • Write a C++ program main.cpp that implements a simple number guessing game with multiple questions /...

    Write a C++ program main.cpp that implements a simple number guessing game with multiple questions / answers. For each game, the program generates a random number between 1 and 10. User enters an answer with a numeric input. If the user input number matches the generated number, then print a message to inform users that he/she has a correct guess. If the guess is not correct, allow the user to have two more chances to guess the correct number. At...

  • 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...

  • 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...

  • please c++ with functions *Modify the Guessing Game Write the secret number to a file. Then...

    please c++ with functions *Modify the Guessing Game Write the secret number to a file. Then write each user guess and answer to the file. (on separate lines) Write the number of guesses to the file at the end of the game. After the game is finished, ask the user if they want to play again. If 'n' or 'N' don't play again, otherwise play again! NOTE: your file should have more than one game in it if the user...

  • 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...

  • This program will implement a simple guessing game... Write a program that will generate a random...

    This program will implement a simple guessing game... Write a program that will generate a random number between 1 and 50 and then have the user guess the number. The program should tell the user whether they have guessed too high or too low and allow them to continue to guess until they get the number or enter a 0 to quit. When they guess the number it should tell them how many guesses it took. At the end, the...

  • Task 4 (20 points) Implement a simple guessing game. Name your file task4.c. Your game will...

    Task 4 (20 points) Implement a simple guessing game. Name your file task4.c. Your game will use rand() to generate a random number from 1 to 10 then prompt user to guess that number. User will have unlimited number of tries to guess the correct number. Your program should work like this hb117@uxb4:$ gco -Wall task4 . c -o task4 hb117@uxb4:~$ ./guessing Guess a number from 1-10: 1 Guess again: 4 Guess again: 2 Guess again: 9 Guess again: 10...

  • python Exercise: ex9.py Write a program that plays a number and you guess the number. The...

    python Exercise: ex9.py Write a program that plays a number and you guess the number. The program helps you with a response 1t the number you guessed is higher or lower than the mystery number. To generate a random number, we need to import some routines. guessing game. It generates a random mystery import random mysteryNumber random.randint (1, 100) The random.randint (1, 100) creates a random integer between 1 and 100. Here is a sample output: Let's play the guessing...

  • Can someone upload a picture of the code in matlab Write a "Guess My Number Game"...

    Can someone upload a picture of the code in matlab Write a "Guess My Number Game" program. The program generates a random integer in a specified range, and the user (the player) has to guess the number. The program allows the use to play as many times as he/she would like; at the conclusion of each game, the program asks whether the player wants to play again The basic algorithm is: 1. The program starts by printing instructions on the...

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