1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
juin [17]
3 years ago
5

Write a program that generates a random number between 5 and 15 and asks the user to guess what the number is. If the user’s gue

ss is higher than the random number, the program should display Too high. Try again. If the user’s guess is lower than the random number, the program should display Too low, Try again. The program should use a loop that repeats while keeping a count of the number of guesses the user makes until the user correctly guesses the random number. Then the program should display the number of guesses along with the following message Congratulations. You figured out my number. Suggest that you also give the user the opportunity to play the game again or quit.
Computers and Technology
1 answer:
stepan [7]3 years ago
4 0

Answer: The c++ program for the number guessing game is given below.

#include <iostream>

using namespace std;

// method declaration without parameters

void game();

int main() {    

char reply;

// calling the game method

game();  

cout<<"Would you like to continue with the game ? Enter y to continue. Enter q to quit the game. " <<endl;

cin>>reply;  

if(reply == 'y')

    game();      

if(reply == 'q')

    cout<<"Quitting the game..."<<endl;  

return 0;

}

void game()

{

   int num, guess, attempt=0;  

num = (rand() % 10) + 6;  

cout<<endl<<"Welcome to the game of guessing the correct number "<<endl;  

   do

{    

    cout<<"Enter any number between 5 and 15. "<< endl;

    cin>>guess;      

    if(guess<5 || guess>15)

    {

        do

        {

            cout<<"Invalid number. Enter any number between 5 and 15. "<<endl;

            cin>>guess;

        }while(guess<5 || guess>15);

    }  

    if(guess < num)

    {

        cout<<"Too Low"<<endl;

        attempt++;        

    }

    if(guess > num)

    {

        cout<<"Too High"<<endl;

        attempt++;          

    }      

}while(guess != num);  

cout<<"Congratulations. You figured out my number in "<<attempt<< " attempts."<<endl;

}

Explanation:

The expression

(rand() % 10)

generates a random number between 0 and 9. 6 is added to generate a random number between 5 and 15.

The integer variable attempt is initialized to 0.

Next, user is asked to guess the number. If the user enters an invalid input, the do-while loop begins until a valid input is received from the user.

    cout<<"Enter any number between 5 and 15. "<< endl;

    cin>>guess;      

    if(guess<5 || guess>15)

    {

        do

        {

            cout<<"Invalid number. Enter any number between 5 and 15. "<<endl;

            cin>>guess;

        }while(guess<5 || guess>15);

    }  

User is prompted to input the guess using the same do-while loop as above. Until the user guesses the correct number, the loop continues. Each incorrect guess by the user, increments the variable attempt by 1. This represents the number of attempts done by the user to guess the correct number.

    if(guess < num)

    {

        cout<<"Too Low"<<endl;

        attempt++;        

    }

    if(guess > num)

    {

        cout<<"Too High"<<endl;

        attempt++;          

    }  

The random number generation and the guessing logic is put in a separate method game().

The entry and exit from the game only is put inside the main method.

You might be interested in
How do the following improve security <br> User Id <br> Password<br> Encryption
natima [27]
I would say password.
5 0
3 years ago
Read 2 more answers
Select the item that best represents technology transfer?
zubka84 [21]

Answer:

Printing Press

Explanation:

Technology transfer is the simple process of sharing or dissemination of information from person to person (or from one organisation to another)

Therefore, the item that best represents technology transfer from the given answer choices is the printing press.

This is because, the printing press helped to print paper containing information which was sent from one location to another.

5 0
2 years ago
Write a demo test to verify the above Rectangle object accessible from demo test main program and can execute display() method t
Sati [7]

Answer:

Explanation:

The Rectangle class was not provided in this question but after a quick online search I was able to find the class code. Due to this I was able to create a test code in the main Method of my program to create a Rectangle object and call it's display() method. The Test code and the output can be seen in the attached image below. While the code below is simply the Main Test Program as requested.

class Brainly {  

   public static void main(String[] args) {

       Rectangle rectangle = new Rectangle(20, 8);

       rectangle.display();

   }

}

4 0
2 years ago
Which statement correctly defines the function of a servo
____ [38]

Answer:

B . It amplifies the power to speed up the motor and generate more torque

6 0
2 years ago
Write an expression that executes the loop body as long as the user enters a non-negative number. Note: If the submitted code ha
Mama L [17]

In the program While loop is used. In the while loop it divides the userNum by 2 in each iteration and prints the value of userNum. The inputs and corresponding outputs are written in the explanation.

<u>Explanation</u>:

In the while loop it divides the userNum by 2 in each iteration and prints the value of userNum.

import java.util.Scanner;  

public class NonNegativeLooper

public static void main (String [] args)

Scanner scnr = new Scanner(System.in);  

//initialize the userNum with 9

int userNum = 9;

//Repeat the loop until the userNum is not negative

while (userNum>=0)

System.out.println(Body);

//Prompt the user to enter the usrNum again

userNum = scnr.nextInt();

System.out.println(Done.);

return;

8 0
3 years ago
Other questions:
  • In Python, what kind of error is returned by the following code? (e.g. NameError, ValueError, IOError, etc.) def my_func(n1, n2)
    8·1 answer
  • 16.
    7·1 answer
  • Help it don’t let me click what do I do
    13·1 answer
  • ...................is a high level, structured , open source programming language​
    10·1 answer
  • with the advent of technology one can experience special features such as 3D theater experiences true or false
    8·1 answer
  • ____________ are the in – built functions that perform a particular pre – defined task when used in a computer program.
    13·1 answer
  • The system where the unit of measurement is centimeter
    15·1 answer
  • Primary memory consists of the various devices that are able to store data and programs even when the power is off. true or fals
    11·1 answer
  • How do we explain the difference between fake news and facts​
    8·1 answer
  • The set of coordinating colors applied to backgrounds, objects, and text in a presentation is called:
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!