Answer to Question #270675 in C++ for DANISH

Question #270675

LOOPS


If diff is greater than or equal to 50, the program outputs the message indicating that the guess is very high (if guess is greater than num) or very low (if guess is less than num).


If diff is greater than or equal to 30 and less than 50, the program outputs the message indicating that the guess is high (if guess is greater than num) or low (if guess is less than num).


If diff is greater than or equal to 15 and less than 30, the program outputs the message indicating that the guess is moderately high (if guess is greater than num) or moderately low (if guess is less than num).


If diff is greater than 0 and less than 15, the program outputs the message indicating that the guess is somewhat high (if guess is greater than num) or somewhat low (if guess is less than num).


1
Expert's answer
2021-11-23T12:31:29-0500
#include <iostream>
#include <cstdlib>
#include <ctime>


using namespace std;
int main()
{
    srand(time(0));
    
    int num;        
    int guess;      
    bool found; 


    num = rand() % 100;


    found = false;


    // Game loop
    while (!found)
    {
        cout << "Enter an integer greater"
             << " than or equal to 0 and "
             << "less than 100: ";


        cin >> guess;
        cout << endl;


        if (guess == num)
        {
            cout << "You guessed the correct "
                 << "num." << endl;
            found = true;
        }
        else if (guess < num)
            cout << "Your guess is lower than the "
                 << "num.\n Try again!"
                 << endl;
        else
            cout << "Your guess is higher than "
                 << "the num.\n Try again!"
                 << endl;
    }
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS