&& 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;

}
}
Last edited on
However the loop is exiting if only one condition is met.


Thats what operator&& is supposed to do. It only returns false if a single operand is false. It is only true if all of its operands are true. This is short-circuit logic.

1 0 0

makes the expression false

3 2 0

makes the expression false

0 0 0

makes the expression false

1 1 1

makes the expression true.

To fix it, you need to change && to ||
Topic archived. No new replies allowed.