Question

In: Computer Science

Summary In this lab, you will make additions to a C++ program that is provided. The...

Summary

In this lab, you will make additions to a C++ program that is provided. The program is a guessing game. A random number between 1 and 10 is generated in the program. The user enters a number between 1 and 10, trying to guess the correct number.

If the user guesses correctly, the program congratulates the user, and then the loop that controls guessing numbers exits; otherwise, the program asks the user if he or she wants to guess again. If the user enters a "Y", he or she can guess again. If the user enters "N", the loop exits. You can see that the "Y" or an "N" is the sentinel value that controls the loop.

Note that the entire program has been written for you. You need to add code that validates correct input, which is "Y" or "N", when the user is asked if he or she wants to guess a number, and a number in the range of 1 through 10 when the user is asked to guess a number.

Instructions

  1. Ensure the source code file named GuessNumber.cpp is open in the code editor.

  2. Write loops that validate input at all places in the code where the user is asked to provide input. Comments have been included in the code to help you identify where these loops should be written.

  3. Execute the program by clicking the Run button. See if you can guess the randomly generated number. Execute the program several times to see if the random number changes. Also, test the program to verify that incorrect input is handled correctly. On your best attempt, how many guesses did you have to take to guess the correct number?

  4. // GuessNumber.cpp - This program allows a user to guess a number between 1 and 10.
    // Input: User guesses numbers until they get it right
    // Output: Tells users if they are right or wrong

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

    int main()
    {
          
    int number;    // Number to be guessed
    int userNumber;   // User's guess
    string keepGoing; // Contains a "Y" or "N" determining if the user wants to continue

    // This is the work done in the detailLoop() function
    srand((unsigned)time(NULL));
    number = (rand() % 10) + 1; // Generate random number
       
    // Prime the loop
    cout << "Do you want to guess a number? Enter Y or N: ";
    cin >> keepGoing;

    // Validate input


          
    // Enter loop if they want to play
    while(keepGoing == "Y")
    {
    // Get user's guess
    cout << "I'm thinking of a number. .\n Try to guess by entering a number between 1 and 10: ";
    cin >> userNumber;
    // Validate input
      


    // Test to see if the user guessed correctly
    if(userNumber == number)
    {
    keepGoing = "N";
    cout << "You are a genius. That's correct!";
    }
    else
    {
    cout << "That's not correct. Do you want to guess again? Enter Y or N: ";
    cin >> keepGoing;
    // Validate input


    }
    } // End of while loop
    return 0;
    } // End of main()
      

Solutions

Expert Solution

If you have any doubts, please give me comment...

// GuessNumber.cpp - This program allows a user to guess a number between 1 and 10.

// Input: User guesses numbers until they get it right

// Output: Tells users if they are right or wrong

#include <iostream>

#include <string>

#include <cstdlib>

#include <ctime>

using namespace std;

int main()

{

    int number;       // Number to be guessed

    int userNumber;   // User's guess

    string keepGoing; // Contains a "Y" or "N" determining if the user wants to continue

    // This is the work done in the detailLoop() function

    srand((unsigned)time(NULL));

    number = (rand() % 10) + 1; // Generate random number

    // Prime the loop

    cout << "Do you want to guess a number? Enter Y or N: ";

    cin >> keepGoing;

    // Validate input

    while (keepGoing != "Y" && keepGoing != "N")

    {

        cout << "Invalid! choose only Y or N! Re-enter: ";

        cin >> keepGoing;

    }

    // Enter loop if they want to play

    while (keepGoing == "Y")

    {

        // Get user's guess

        cout << "I'm thinking of a number. .\n Try to guess by entering a number between 1 and 10: ";

        cin >> userNumber;

        // Validate input

        while (userNumber < 1 || userNumber > 10)

        {

            cout << "Invalid number! Please enter between 1 and 10. Re-enter: ";

            cin >> userNumber;

        }

        // Test to see if the user guessed correctly

        if (userNumber == number)

        {

            keepGoing = "N";

            cout << "You are a genius. That's correct!";

        }

        else

        {

            cout << "That's not correct. Do you want to guess again? Enter Y or N: ";

            cin >> keepGoing;

            // Validate input

            while (keepGoing != "Y" && keepGoing != "N")

            {

                cout << "Invalid! choose only Y or N! Re-enter: ";

                cin >> keepGoing;

            }

        }

    } // End of while loop

    return 0;

} // End of main()


Related Solutions

Summary In this lab, you use a counter-controlled while loop in a Java program provided for...
Summary In this lab, you use a counter-controlled while loop in a Java program provided for you. When completed, the program should print the numbers 0 through 10, along with their values multiplied by 2 and by 10. The data file contains the necessary variable declarations and some output statements. Instructions Ensure the file named Multiply.java is open. Write a counter-controlled while loop that uses the loop control variable to take on the values 0 through 10. Remember to initialize...
c++   In this lab you will create a program to make a list of conference sessions...
c++   In this lab you will create a program to make a list of conference sessions you want to attend. (List can be of anything...) You can hard code 10 Sessions in the beginning of your main program. For example, I have session UK1("Descaling agile",    "Gojko Adzic",       60);        session UK2("Theory of constraints", "Pawel Kaminski", 90); and then:        List l;        l.add(&UK1);        l.add(&UK2); Your Session struct should have the following member data: The Title The Speaker The session...
Summary In this lab, you complete a prewritten Python program that computes the largest and smallest...
Summary In this lab, you complete a prewritten Python program that computes the largest and smallest of three integer values. The three values are -50, 53, 78. Instructions Two variables named largestand smallest are assigned for you. Use these variables to store the largest and smallest of the three integer values. You must decide what other variables you will need and initialize them if appropriate. Write the rest of the program using assignment statements, if statements, or elifstatements as appropriate....
Understanding if-elseStatements Summary In this lab, you will complete a prewritten Java program that computes the...
Understanding if-elseStatements Summary In this lab, you will complete a prewritten Java program that computes the largest and smallest of three integer values. The three values are –50, 53, and 78. Instructions Two variables named largest and smallest are declared for you. Use these variables to store the largest and smallest of the three integer values. You must decide what other variables you will need and initialize them if appropriate. Write the rest of the program using assignment statements, if...
For this lab, you will write a C++ program that will calculate the matrix inverse of...
For this lab, you will write a C++ program that will calculate the matrix inverse of a matrix no bigger than 10x10. I will guarantee that the matrix will be invertible and that you will not have a divide by 0 problem. For this program, you are required to use the modified Gaussian elimination algorithm. Your program should ask for the size (number of rows only) of a matrix. It will then read the matrix, calculate the inverse, and print...
For this lab, you will write a C++ program that will calculate the matrix inverse of...
For this lab, you will write a C++ program that will calculate the matrix inverse of a matrix no bigger than 10x10. I will guarantee that the matrix will be invertible and that you will not have a divide by 0 problem. For this program, you are required to use the modified Gaussian elimination algorithm. Your program should ask for the size (number of rows only) of a matrix. It will then read the matrix, calculate the inverse, and print...
For this lab, you will write a C++ program that will calculate the matrix inverse of...
For this lab, you will write a C++ program that will calculate the matrix inverse of a matrix no bigger than 10x10. I will guarantee that the matrix will be invertible and that you will not have a divide by 0 problem. For this program, you are required to use the modified Gaussian elimination algorithm. Your program should ask for the size (number of rows only) of a matrix. It will then read the matrix, calculate the inverse, and print...
Summary In this lab, you complete a prewritten Java program that calculates an employee’s productivity bonus...
Summary In this lab, you complete a prewritten Java program that calculates an employee’s productivity bonus and prints the employee’s name and bonus. Bonuses are calculated based on an employee’s productivity score as shown below. A productivity score is calculated by first dividing an employee’s transactions dollar value by the number of transactions and then dividing the result by the number of shifts worked. Productivity Score Bonus <=30 $50 31–69 $75 70–199 $100 >= 200 $200 Instructions Ensure the file...
C++ Funcion For this lab you need to write a program that will read in two...
C++ Funcion For this lab you need to write a program that will read in two values from a user and output the greatest common divisor (using Euclidean algorithm) to a file. You will also need to demonstrate using ostream and ostringstream by creating 2 functions to output your print heading: one that uses ostream and the other uses ostringstream. Using ostream and ostringstream Write two PrintHeader functions that will allow you to output to the screen and to an...
Make a C program that simulates time-sharing in the operating system. Please follow the instructions provided:...
Make a C program that simulates time-sharing in the operating system. Please follow the instructions provided: a) Use a circular queue. b) It is required that the process be inputted by the user. The user must input the process name and the duration in seconds, and for this simulation let the user input 5 processes. c) As this requires process name and duration, use an array of structures. d) To simulate time-sharing, following the algorithm presented below: d.1) Use the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT