Question

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 program should ask them if they want to play again. (Note: The program should use at least one function.)

For example:

Enter a guess 1-50, or 0 to quit: 25
Too high!

Enter a guess 1-50, or 0 to quit : 15
Too low!

Enter a guess 1-50, or 0 to quit : 18
That's it!
You took 3 guesses to get the number.

Would you like to play again? (Y/N) N
Good bye!

Note: The program should include appropriate input validation and error checking.

Submit:

  • Pseudocode
  • Fully documented and running program.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

//header files

#include<iostream>

#include<cstdlib>
#include<ctime>
using namespace std;

int main()

{

int number, i;

char ans;

srand(time(NULL));

//creating random number between 1 to 50

number = 1 + rand()%50;

//printing the random number to identify the guess

int guess;

int count=0;

do

{

cout<<"Enter a guess 1-50, or 0 to quit: ";

do

{

//enter a guess

cin>>guess;
if(guess==0)
return 0;
//if guess is equal to number

if(guess == number)

{

cout<<"That's it! You took "<<count<<" guesses to get the number."<<endl;

break;

}

//if guess is less than number

else if(guess < number && guess >0 && guess <=50)

{

count++;

cout<<"Too low!\nEnter a guess 1-50, or 0 to quit: ";

}

//if guess is greater than number

else if(guess > number && guess >0 && guess <= 50)

{

count++;

cout<<"Too high!\nEnter a guess 1-50, or 0 to quit: ";

}

//if guess in not in valid range

else if(guess<0 || guess > 50)

{

count++;

cout<<"Invalid!Try again: ";

}

}while(guess != number);

cout<<"Would you like to play again? (Y/N) ";

cin>>ans;

if(ans != 'y' && ans != 'Y')

{

break;

}

}while(ans == 'y' || ans == 'Y');

return 0;

}//end of the class

Pseudo code:

start

generate random number

Accept guess number from user

while(repeat=='Y') do

while(1) do

if(guess==0) do

quit the program

end if

Increment count  

if(guess > number)

print Too high!

end if

else if (guess < number)

Too low!

end else if

else

That's it!

print number of guesses to get the number

end else

Accept guess number from user

end while

print Would like to play again? (Y/N)

accept choice

end while

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
This program will implement a simple guessing game... Write a program that will generate a random...
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
  • 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...

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

  • Java Guessing Game Class The class will generate a random number of 1 to 15, and...

    Java Guessing Game Class The class will generate a random number of 1 to 15, and then check to see if the user guessed the number correctly. If the number is incorrect, the user should have the chance to guess again, until they guess the right number or they guess 10 times. The class method should keep track of the number of guesses the user has had, and return this value.   The class should have one constructors, a default and...

  • on python i need to code a guessing game. After the player has guessed the random...

    on python i need to code a guessing game. After the player has guessed the random number correctly, prompt the user to enter their name. Record the names in a list along with how many tries it took them to reach the unknown number. Record the score for each name in another list. When the game is quit, show who won with the least amount of tries. this is what i have so far: #Guess The Number HW assignment import...

  • Write a program that prompts the user for an integer that the player (maybe the user,...

    Write a program that prompts the user for an integer that the player (maybe the user, maybe someone else) will try to guess. If the player's guess is higher than the target number, the program should display "too high" If the user's guess is lower than the target number, the program should display "too low" The program should use a loop that repeats until the user correctly guesses the number. Then the program should print how many guesses it took....

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

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

  • Required in JAVA language Write a program that enables a user to play number guessing games....

    Required in JAVA language Write a program that enables a user to play number guessing games. The following is a nutshell description of a number guessing game. + a random number is generated + loop prompting the user to enter guesses until the user guesses the number or hits the maximum number of allowed guesses or enters the "quit" sentinel value The rest of this specification documents number guessing games in more detail. The documentation uses manifest constants that can...

  • Write a Java application program that plays a number guessing game with the user. In the...

    Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() :                    new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...

  • For this lab you will write a Java program using a loop that will play a...

    For this lab you will write a Java program using a loop that will play a simple Guess The Number game. Th gener e program will randomly loop where it prompt the user for a ate an integer between 1 and 200 (including both 1 and 200 as possible choices) and will enter a r has guessed the correct number, the program will end with a message indicating how many guesses it took to get the right answer and a...

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