[C++] Strange problem stacking multiple OR's
I'm writing a pretty simple c++ collision algorithm that first checks if the object is within bounds of a mesh grid. However, when I do a stacked OR to see if the values are within bounds, it always triggers as TRUE (even when the object is in bounds)
if (pos.x <= mesh->X(0, 0)) // None of these evaluate to TRUE
{
cout << "<X" << endl;
}
if (pos.x >= mesh->X(iw - 1, ih - 1))
{
cout << ">X" << endl;
}
if (pos.y <= mesh->Y(0, 0))
{
cout << "<Y" << endl;
}
if (pos.y >= mesh->Y(iw - 1, ih - 1))
{
cout << ">Y" << endl;
}
// But for some reason, this is:
if ( pos.x <= mesh->X(0, 0) || pos.x >= mesh->X(iw-1, ih-1) || pos.y <= mesh->Y(0, 0) || pos.y >= mesh->Y(iw-1, ih-1) );
{
cout << " Bound Error " << endl;
cData.outOfBounds = true;
;
}
Is there some nuance of stacking logical OR's that I am missing? If it helps all the values are GLfloat type.
Posted Jun 9, 2014, 3:36 AM UTC
26
Look at that line carefully. You have a semicolon at the end.
So the
ifstatement is being applied to the immediately following statement, which is an empty statement. The body of theifstatement is empty. The block that follows on the next line is not controlled by theifcondition.===
Make sure you have all warnings turned on in your compiler, and pay attention to what they say. Some compilers will warn you about empty bodies of
ifstatements:WOW, THANK YOU!
I always thought that would result in a compilation error. I can't believe I made a mistake that stupid... Sometimes it takes two sets of eyes.
No, it's a perfectly valid thing to write. Sometimes you want an empty body of a loop or an
ifstatement. But most of the time you don't, and that's why you should turn on all compiler warnings.Yeah... I should turn on all compiler warnings, but there are a LOT of warnings already in this code.
Then you should fix those things.