-
Boolean Help
Im having a real hard time understanding how the Boolean operators work and how to figure them out.
A. !( 1 || 0 ) ANSWER: 0
B. !( 1 || 1 && 0 ) ANSWER: 0 (AND is evaluated before OR)
C. !( ( 1 || 0 ) && 0 ) ANSWER: 1 (Parenthesis are useful)
like how did u arrive at these answers any help in understanding would b gr8.
Thanks
-
-
Thanks I think this might help! :D
-
My question is how to implement Boolean in an IF statement with YES or NO?
("Do you want to re-start? *YES*=restart(for loop?) *NO*=exit")
-
Well, you can't compare strings using the boolean operators. You can only compare numbers.
In your simple yes / no example, the best option is to check to see if the first character is "Y' or "N". You can use boolean for this, because each ASCII character is represented by a number... you are just comparing numbers.
You need to understand strings (C-style strings or C++ string objects) and you need to understand the ASCII code.
Some hints:
- Check for both upper and lower case.
- If there are two conditions, you only need to check one... If "yes" is the default answer, just check for "N" or "n". If the user enters anything other than that, assume "yes".
Even if there are more than 2 choices, I'll usually have a default value so I can just hit ENTER. If there is no default, you need to repeat the question if the user enters something invalid.
-
>My question is how to implement Boolean in an IF statement with YES or NO?
Code:
do {
//Do stuff
cout << "Do you want to re-start? *YES*=restart *NO*=exit" << flush;
string answer;
cin >> answer;
} while (answer != "no");