I was on working something and && is not working properly

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://applinked.me
Last edited on
The while loop will only enter the block when all of x y z are not 0. Hence if any are 0 the while loop will execute.

To process when all of x y z are not 0, then:

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


which simplified gives:

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


So you need logical or rather than logical and in your original code.

Note that instead of != 0, consider > 0. What's a triangle with a negative side?
The check is incomplete anyway. There are all-positive triples that are not the sides of any possible triangle.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
   int rightTriangle = 0, notRightTriangle = 0, notTriangle = 0;

   cout << "Enter triplets of integers (or 0 0 0 to stop):\n";
   for ( int x, y, z; cin >> x >> y >> z && !( x == 0 && y == 0 && z == 0 ); )
   {
      if ( min( { x, y, z } ) > 0 && 2 * max( { x, y, z } ) < x + y + z )
      {
         int xsq = x * x, ysq = y * y, zsq = z * z;
         if ( xsq == ysq + zsq || ysq == zsq + xsq || zsq == xsq + ysq ) rightTriangle++;
         else                                                            notRightTriangle++;
      }
      else
      {
         notTriangle++;
      }
   }

   cout << "Right triangles: " << rightTriangle << '\n'
        << "Not-right triangles: " << notRightTriangle << '\n'
        << "Not triangles: " << notTriangle << '\n';
}


Enter triplets of integers (or 0 0 0 to stop):
3 4 8
3 4 5
5 12 13
10 10 10
2 3 4
0 0 0
Right triangles: 2
Not-right triangles: 2
Not triangles: 1
I can't figure out the code format on here ._.:
While writing or editing a post, highlight the code and then click the "<>" button to the right of the edit window.

nonRightTriangleCount = nonRightTriangleCount++;
The behavior of this is undefined. Basically, you can't modify a variable more than once in an expression. There are exceptions, but in general, don't do it unless you're certain it's allowed. In this case, you can just say:
++nonRightTriangleCount; or
nonRightTriangleCount++;

This code appears 4 times:
1
2
cout << "Type here: ";
cin >> x >> y >> z;


You can get rid of 2 of them by having one copy after the switch statement instead of one copy in each case, but a better way involves putting the loop condition in the middle of the loop. Don't be afraid to do this when the problem calls for it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    while (true) {
	int x,y,z;
	cout << "Type here: ";
	cin >> x >> y >> z;

	if (x == 0 && y == 0 && z == 0) break;

	TriangleType result = getRightTriangleType(x, y, z);

	switch (result) {
	case rightTriangular:
	    ++rightTriangleCount;
	    break;
	case nonRightTriangular:
	    ++nonRightTriangleCount;
	    break;
	default:
	    ++nonTriangleCount;
	    break;
	}
    }

> nonRightTriangleCount = nonRightTriangleCount++;
> The behavior of this is undefined.

The behaviour of this used to be undefined prior to 2017; since then it is not undefined.

20) In every simple assignment expression E1=E2 and every compound assignment expression E1@=E2, every value computation and side-effect of E2 is sequenced before every value computation and side effect of E1 (since C++17)
https://en.cppreference.com/w/cpp/language/eval_order
Thanks JLBorges for the correction. :)
Topic archived. No new replies allowed.