Using the || -operator

for Section 2, chapter 24 (bools) I tried writing the following:

if (response[0] == (‘y’ || ‘Y’)) {
return true;
}

But the function doesn’t turn true. I’ve written in C# before, and I know it’s possible to write with this structure, so I’m wondering a) if I’m not getting errors, why does the program still not recognize the statement? and b) how would I write it differently in C++ ?

/Azul

I don’t know anything about C#, but that statement doesn’t do what you think it does in C++. First it’ll evaluate 'y' || 'Y' to true since they’re both non-zero values, then evaluate response[0] == true which will most likely be false. What you want is something like response[0] == 'y' || response[0] == 'Y', or a little less repetitive, tolower( response[0] ) == 'y'.

yeah, in the end i wrote " response[0] == ‘y’ || respons[0] == ‘Y’ ", though the thought behind the ||-operator (or what i thought) was like is the value of the first ([0]) character a ‘y’ OR a ‘Y’ in that case i wouldn’t have to double check for the value (in C# when writing ’ statement || otherStatement ’ the programme checks for the first statement and if it isn’t true it checks for the otherStatement)

Privacy & Terms