I'm havin trouble outputing different false statements in a boolean function... I'm currently working on a "secret number game" program which must generate a secret number and inform the user if his/her guess number is to high, to low or correct. I know boolean return true and false.. If the number is correct, the true statement will appear, if false... THAT'S where my problem starts cause now I have TWO statements to output..In a Function.. How do I make my program able to tell if the number guessed is "too high" or "too low" ? Please help
In that case its easy.Pity I don't have my work with me now. Does it mean I should use the Void function to show the output statements too..? Not the bool function?Cause I was typing only the validations inside the bool function and not the output statements.. I was trying to call the bool function in the main function, in the if statements and hey! I got confused. The other thing is..this assignment question include "..in both cases false is returned". The word "returned" doesn't it mean we may not use the Void function cause void does not return any statements?
You can still return a bool, but also maybe pass in a string reference. If it fails return false AND populate the string with your failure message.
Then outside your function you can still test for true or false and if it's a false, output the message.
You could do it the same way as they've done it with std::strcmp() - return a negative value if the guess is less than the secret number; 0 if they're equal and a positive value (besides 0) if it is larger than the secret number.
Although it won't work if it has to be a bool.
1 2 3 4 5
int compare(int secret, int guess)
if(guess < secret) return -1;
elseif(guess > secret) return 1;