Need help with C++ Program

I have a project due for my computer science class tomorrow. My program has to prompt a user to guess a random number 1-100. The guess is correct whenever the difference equals zero: diff = abs(num-guess). I have to give feedback after every guess saying weather the guess is very high, high, moderately high, somewhat high, very low, low, moderately low, somewhat low... I also have to limit it to only 5 guesses. I can not get my program to work properly.

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
int num, guess=0, diff;


srand(time(0));
num = rand() % 100;


do
{
cout << "Enter an integer greater than or equal to 0 and less than 100: ";
cin >> guess;
cout << endl;

diff = abs(num - guess);

if (diff >0 && diff <15)
cout << "Your guess is somewhat high. Guess again!" << endl;
else if (diff >= 15 && diff<30)
cout << "Your guess is moderately high. Guess again!" << endl;
else if (diff >= 30 && diff<50)
cout << "Your guess is high. Guess again!" << endl;
else if (diff >= 50)
cout << "Your guess is very high. Guess again!" << endl;
else if (diff <0 && diff >-15)
cout << "Your guess is somewhat low. Guess again!" << endl;
else if (diff <= -15 && diff>-30)
cout << "Your guess is moderately low. Guess again!" << endl;
else if (diff <= -30 && diff>-50)
cout << "Your guess is low. Guess again!" << endl;
else if (diff <= -50)
cout << "Your guess is very low. Guess again!" << endl;
else
cout << "Enter a valid number" << endl;


} while (diff != 0);

system("PAUSE");
return 0;
}
Last edited on
Why would the difference ever be negative if the difference = the absolute value of anything?
abs() give you the absolute value. so you never get a negative difference.

in that case some of your if statements never are true;
and you never know, if the guessed number is lower or higher than num
Yeah, why would the difference ever be negative??
Topic archived. No new replies allowed.