I've looked and looked with the debugger and cannot seem to figure out why the IF statement always prints the message.
The IF statement checks to see if yesno != 'Y' ||(or) 'N' but regardless if i type y or Y or n N or H B it will show... . Im not sure what move to make anymore! I cant seem to find where its going wrong?
if(yesno != 'Y' || 'N') { ...
Thanks guys.
The
||
doesn't quite mean what you think it means. The correct approach is:This evaluates each side of the
&&
independently, and the result is true if both sides are true.Note that
will always be true because any given character is either not Y or it is not N. This is probably not what you want.
You need to put
You always need to put the full expression twice.
you cannot OR an expression (yesno!='Y') and a statement ("N") which is also an expression. its only two or more expressions that can be combined using || and && to get the required output.
is actually
which is always true because:
regardless of the expression.
should be:
You are chaining them which is incorrect. I am fairly sure you know that you should not and that is a typo, because you used
&&
correctly on line no44
.Take the conditional
yesno != 'Y' || 'N'
apart. It has two clauses:yesno != 'Y'
. This is probably a comparison you intended; it checks to see if the charyesno
is inequal to the value'Y'
.'N'
. This clause evaluates simply to the value'N'
. As a bool,'N'
, which is not zero, is considered "true."So you have "(stuff)
||
(always true)". Here "stuff" isyesno != 'Y'
and "always true" is'N'
. The result will always be true.