### how do I Differentiate false statements in a function

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
It's hard to know the best way to do this without seeing your code.

But it sounds like a use for a basic if-elseif-else test. Here's some pseudo-code:

 ``12345678910111213141516171819202122`` ``````int secretNumber; int userGuess; // Initialise the secret number secretNumber = ... // Get the user guess userGuess = ... if (userGuess > secretNumber ) { std::cout << "You guessed too high" << std::endl; } else if (userGuess < secretNumber ) { std::cout << "You guessed too low" << std::endl; } else { std::cout << "Congratulations! You guessed correctly." << std::endl; } ``````
Last edited on
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.

Which is a bit like MikeyBoy's suggestion really.
Oh alright I'd definately try that too
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.

 ``12345`` ``````// pseudo int compare(int secret, int guess) if(guess < secret) return -1; else if(guess > secret) return 1; else return 0;``````

Useage:
 ``1234567`` ``````int result = compare(secretNumber, guess); if(result == 0) print "secretNumber == guess" else if(result > 0) print "secretNumber > guess" else print "secretNumber < guess"``````

But, as I said; if it has to be a bool it won't work - but if you're free to do however you want to do, then this might be something for you :)
Now that's another way of doing it..wow there really are different ways of tackling this.Thank you very much guys, you'v helped me a lot! :)
Topic archived. No new replies allowed.