( MindTap - Cenage )Example 5-6 implements the Number Guessing Game program. If the guessed number is not correct, the program outputs a message indicating whether the guess is low or high. Modify the...



( MindTap - Cenage )Example 5-6 implements theNumber Guessing Game program. If the guessed number is not correct, the program outputs a message indicating whether the guess is low or high.


Modify the program as follows: Suppose that the variables num and guess are as declared inExample 5-6 and diff is an int variable. Let diff = the absolute value of (num - guess).


If diff is0, then guess is correct and the program outputs a message indicating that the user guessed the correct number.


Suppose diff is not0. Then the program outputs the message as follows:



  • If diff is greater than or equal to50, the program outputs the message indicating that the guess isvery high (if guess is greater than num) orvery low (if guess is less than num).

  • If diff is greater than or equal to30 and less than50, the program outputs the message indicating that the guess ishigh (if guess is greater than num) orlow (if guess is less than num).

  • If diff is greater than or equal to15 and less than30, the program outputs the message indicating that the guess ismoderately high (if guess is greater than num) ormoderately low (if guess is less than num).

  • If diff is greater than0 and less than15, the program outputs the message indicating that the guess issomewhat high (if guess is greater than num) orsomewhat low (if guess is less than num).


 give the user no more than five tries to guess the number.


(To find the absolute value of num - guess, use the expression abs(num - guess). The function abs is from the header file cstdlib.)




//Flag-controlled while loop.

//Number guessing game.



#include 

#include 

#include 



using namespace std;

int main()

{

    int num; //variable to store the random number

    int guess; //variable to store the number guessed by the user

    bool isGuessed; //boolean variable to control the loop


    srand(time(0));

    num = rand() % 100;


    isGuessed = false;


    while (!isGuessed)

    {

        cout <>

 <>

 <>


        cin >> guess;

        cout <>



        if (guess == num)

        {

            cout <>

 <><>

            isGuessed = true;

        }

        else if (guess <>

            cout <>

 <>

 <>

        else

            cout <>

 <>

 <>

    } //end while

    return 0;

}

Jun 05, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here