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
Declare a structure whose tag name is Server and that contains the following fields: manufacturer, a string, model and serialnum
NemiM [27]

Answer:

The answer to this question as follows :

Structure definition:

struct Server  //define structure server.

{

string manufacturer;

 //define string variable.

string model, serialnum;  //define string variable.

int year;

 //define integer variable.

double clockSpeed;

 //define double variable.

int cores;   //define integer variable.

int ram;

    //define integer variable.

int storage;

  //define integer variable.

};

Explanation:

Structure is a collection of heterogeneous(different type) elements. It is a user-define datatype which is available on the C/C++ programming language. To define any structure we use the struct keyword. The syntax of defining structure can be given as:

Syntax:

struct structure name

{

//define variables

//statements and code.

};

OR

struct structure name

{

//define variables

//statements and code.

}create structure variable;  

In the above structure code, we define a structure that is "Server". In this structure, we define different types of variables that is " manufacturer, model, serialnum, year, clockSpeed, cores, ram, and storage". In this structure variable manufacturer, model and serialnum datatype are a string and clockSpeed datatype is double and variable year, cores, ram, and storage data type is an integer.

3 0
3 years ago
How to call a void method with no parameters IN JAVA?
natka813 [3]

Explanation:

SanfoundryMenu

Java Program to Illustrate a Method without Parameters and Without Return Type

« PrevNext »

This is a Java Program to Illustrate a Method without Parameters and Without Return Type.

We have made the method to calculate area. We call this method from main method and then enter the radius of the circle as input. Now we calculate area and print the output.

Here is the source code of the Java Program to Illustrate a Method without Parameters and Without Return Type. The Java program is successfully compiled and run on a Windows system. The program output is also shown below

6 0
3 years ago
How can a user begin working with a new, blank workbook in Excel?
inn [45]
Go to file. Then click new.
5 0
3 years ago
Identify the examples of goals.
Gemiola [76]
Complete your first marathon.
Create and commit to a fitness routine.
Learn a foreign language.
Cut junk food out of your diet.
Start volunteering regularly.
Increase your emotional intelligence.
Earn a college degree.
3 0
2 years ago
Read 2 more answers
If you’re storing some personal information like Debit/Credit card numbers or Passwords etc, on different sites for running you’
Lynna [10]

Answer:

No, hackers can steal information from cookies. This many times depend on the type of cookies.

Cookies, by general definition, are programs embedded onto browsers which collect non-invasive information about our activities such so that it can adapt to our needs.

The type of information that a cookie collects depends on what type of cookie it is.

Session Cookies: These types of cookies only collect information about the movement of the user on the website such as pages visited, products that were examined (for e-commerce websites) and products/services that the user put in their cart.

These type of cookies are temporary and are deleted as soon as the browser is shut down.

Persistent Cookies:

These do the same thing that the Session cookies do only that they are more permanent and are stored in a text file even after the browser and the computer has been shut down.

It is these types of cookies that pose a threat. These type of cookies many store codes and passwords and log-in details which may be exploited by a hacker.

Cheers!

7 0
3 years ago
Other questions:
  • The main differences between laptops and desktop computers other than size and portability.
    14·1 answer
  • What does %d, , %c, %s mean and what's their difference?
    12·1 answer
  • The Integers 10, 30, 20 are inserted in that order, into an AVL Tree. Which sequence of commands will make the correct method ca
    7·1 answer
  • ____ documents consist of the text to be displayed on a Web page, together with a number of special characters: tags that achiev
    14·1 answer
  • What is inputted into a computer system?
    7·1 answer
  • Which task can be completed with the Template Organizer?
    12·2 answers
  • 45 points!
    10·2 answers
  • Which is the fastest memory in computer​
    9·2 answers
  • Please explain what steps you will take to prevent hackers from getting access to your personal information.
    7·1 answer
  • Define technical writing. The proposal introduces the ……. (5 Mark)
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!