&& not working as expected

For my assignment I have to take sets of 3 integers separated by spaces from the user input and determine whether or not they can form a triangle, and if so whether or not it's a right triangle. The user presses enter between each set of 3 integers. When the user is done entering sets, they'll enter the set "0 0 0" and the program will output the count of right triangle sets, non right triangle sets, and non triangular sets. The constrictions are as follows:

"the following libraries will not be accepted:

#include <bits/stdc++.h> //nonstandard
#include <sstream> //beyond chapter nine
#include <array> //beyond chapter nine
#include <stdlib.h> //a C library, not C++"

I am trying to test 3 values entered by the user using a while loop and the && operator. However the loop is exiting if only one condition is met. How can I make it so that all 3 conditions need to be met for the loop to exit?
Here is my loop, I can't figure out the code format on here ._.:



cout << "Type here: ";
cin >> x >> y >> z;
while (x != 0 && y != 0 && z != 0) {

result = getRightTriangleType(x, y, z);

switch (result) {

case rightTriangular:
rightTriangleCount = rightTriangleCount++;
cout << "Type here: ";
cin >> x >> y >> z;
continue;
case nonRightTriangular:
nonRightTriangleCount = nonRightTriangleCount++;
cout << "Type here: ";
cin >> x >> y >> z;
continue;
default:
nonRightTriangleCount = nonRightTriangleCount++;
cout << "Type here: ";
cin >> x >> y >> z;
continue;

}
}
https://walmartone.me/
Last edited on
Let's say x == 1, y == 2, x == 0

When you use "&&", that's the and operator. All 3 conditions must be true to continue.

Is x != 0? Yes, so check the next condition.
Is y != 0? Yes, so check the next condition.
Is z != 0? No. Z is 0, so the condition fails.

To exit the loop only when all are 0, do one of the following:

while (x != 0 || y != 0 || z != 0) )

while (! (x == 0 && y == 0 && z == 0) )

Topic archived. No new replies allowed.