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
By limiting the number of times a person can use a type of software before registering as the authorized owner of that software,
alexira [117]

Answer:

Software piracy.

Explanation:

A software can be defined as a set of executable instructions (codes) or collection of data that is used typically to instruct a computer on how to perform a specific task and solve a particular problem.

Simply stated, it's a computer program or application that comprises of sets of code for performing specific tasks on the system.

Basically, softwares are categorized into two (2) main categories and these are;

I. Open-source software.

II. Proprietary software.

A proprietary software is also known as a closed-source software and it can be defined as any software application or program that has its source code copyrighted and as such cannot be used, modified or distributed without authorization from the software developer. Thus, it is typically published as a commercial software that may be sold, licensed or leased by the software developer (vendor) to the end users with terms and conditions.

Some examples of proprietary software are Microsoft Windows, macOS, Adobe photoshop etc.

Furthermore, a proprietary software license avail end users the opportunity to install and use the software after agreeing to the terms of its license.

Software piracy can be defined as an act which typically involves the unauthorized use, duplications, or distribution of a software that is legally copyrighted or protected, without an express permission from the software manufacturer (owner).

Generally, software manufacturers (owners) deal with problems associated with software piracy by placing a limit on the number of times or durations that an end user is allowed to use a particular software before registering (subscribing) as the authorized owner of that software.

8 0
2 years ago
As part of their extensive kitchen remodel, the Lees told their electrical contractors that they would need plenty of outlets fo
Vanyuwa [196]

The kind of wire and circuit breaker are:  grounding wire such as fairly large bare copper wire.

<h3>What is the best wire for the above?</h3>

A 20A, 120V small-appliance branch circuit is known to be  used in the case above.

Note that Electric range circuits needs about 50-amp, 240-volt made for circuit that is said to supplies the power to the range or oven via  a 6-3 electrical wire.

Learn more about circuit breaker from

brainly.com/question/8976395

#SPJ1

5 0
2 years ago
Which of the following is most likely to be considered plagiarism? a. Intentionally reporting the results of inaccurate statisti
Svetlanka [38]

Answer:

c.

Explanation:

Plagiarism refers to the act of stealing another person's thought, ideas, or expressions. Therefore it can be said that out of all the answers provided the one that is most likely to be considered plagiarism would be using materials from a source without proper citation. This is because you are using another individual's work and not giving them credit for having created that work, instead you are taking credit for it by not citing the actual author.

4 0
3 years ago
Explain why the fundamental software engineering principles of process, dependability, requirements management, and reuse are re
Georgia [21]

These principles are not explicit to one kind of program and are increasingly broad "best practice" rules that assist designers with composing code that is easier to maintain.

<u>Explanation:</u>

A set of programming guidelines that are executed to play out a particular undertaking according to the prerequisites of the client is known as programming. Every product has some essential standards to follow. In light of all product frameworks have basic quality traits, including accessibility, modifiability, execution, security and wellbeing, testability and ease of use, the key programming thoughts give basic arrangements or strategies to help those characteristics.

It is generally less expensive, over the long haul, to utilize programming designing strategies and methods for programming frameworks instead of simply compose the projects as though it was an individual programming venture.

8 0
3 years ago
Which is a possible benefit of having a good credit history?
Solnce55 [7]

I believe low interest rate is the correct answer

6 0
3 years ago
Read 2 more answers
Other questions:
  • List and the deference between MS access objects​
    8·1 answer
  • _____ consists of the instructions that direct the operation of the computer system and enable users to perform specific tasks,
    15·1 answer
  • Identify a factor that is unlikely to influence the length of time people wait in the lunch line.
    8·2 answers
  • The first few lines of a script that say "using" ... that point the code to the right groups are called
    11·1 answer
  • What is the use of a piano​
    10·2 answers
  • You can use a minus sign to make a negative numberlike -2. What happens to each of the following 2++2
    8·1 answer
  • This is a tableware use to serve the main dish
    11·1 answer
  • Write a program that accepts a whole number as input, multiplies that number by 12, and then outputs the product.
    6·2 answers
  • What are the 3 dimension of an information system?
    13·1 answer
  • The arrangement of keys on a keyboard, QWERTY reflects the keyboard layout by ________. Group of answer choices using the letter
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!