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).
#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;
}
Comments
Leave a comment