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

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

  • Write a JAVA program that plays a number guessing game with the user. A sample run...

    Write a JAVA program that plays a number guessing game with the user. A sample run for the game follows. User input is shown in boldface in the sample run. Welcome to the game of Guess It! I will choose a number between 1 and 100. You will try to guess that number. If your guess wrong, I will tell you if you guessed too high or too low. You have 6 tries to get the number. OK, I am...

  • Use visual studio C# Tasks You are going to build a program that generates a random...

    Use visual studio C# Tasks You are going to build a program that generates a random integer between 1 and 10, then prompts user to make several guesses of the number until user gets the right number - For each guess, tell user if the guess is higher or lower than the number - Handle the potential exception with user guess. Sample Output DAC programPrajects ExameExam1bin,DebugExarm1.exe Welcome! Guess the number I'm thinking of . It is between 1 and 1e...

  • python question Question 1 Write a Python program to play two games. The program should be...

    python question Question 1 Write a Python program to play two games. The program should be menu driven and should use different functions to play each game. Call the file containing your program fun games.py. Your program should include the following functions: • function guess The Number which implements the number guessing game. This game is played against the computer and outputs the result of the game (win/lose) as well as number of guesses the user made to during 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
ADVERTISEMENT