bool

Jun 17, 2012 at 11:35pm
Hello everybody. I have a simple question on how to properly use the bool. The last time I used the bool in a homework assignment I did used it wrong, but it worked. This is what I did.

bool lineOne;
string yesNo;

cin >> yesNo;

lineOne = (yesNo == "YES");

My facilator said, "A boolean variable was declared and an attempt to set its value is in the code, but the code does not set the boolean value for the variable correctly".

I don't know understand how to use the bool.

Jun 18, 2012 at 12:08am
closed account (zb0S216C)
Looks fine to me. The expression used to assign lineOne a value yields a Boolean value (it's a Boolean context). Though, "but the code does not set the boolean value for the variable correctly" leads me to believe that your tutor wanted you to get the value in some other way (possibly by initialising lineOne and not assigning it a value?).

Wazzak
Last edited on Jun 18, 2012 at 12:09am
Jun 18, 2012 at 1:12am
I agree with Framework. The result of (yesNo == "YES") is indeed a bool.

It could also be written as a ternary expression, but that is really unnecessary.
lineOne = (yesNo == "YES") ? true : false;
Jun 18, 2012 at 4:38am
Thank you so much for your help.
Topic archived. No new replies allowed.