# Stupid Logic Problem Need Outside Viewpoint

• 03-03-2005
RP319
Stupid Logic Problem Need Outside Viewpoint
Well basically I need to find a way to fix this problem I keep on having.

Here is my code.

Code:

```     Triangle calculateTriangleType(int &sOne,int &sTwo,int &sThree)   {     Triangle result;     if(sOne + sTwo < sThree || sOne + sThree < sTwo || sTwo + sThree < sOne)         result = NO_TRIANGLE;     if(sOne == sTwo && sOne == sThree)         result = EQUILATERAL;     if(sOne == sTwo && sOne != sThree || sTwo == sThree && sTwo != sOne || sThree == sOne && sThree != sTwo)         result = ISOCELES;     if(sOne != sTwo && sOne != sThree)         result = SCALENE;     return result;   }```

Here is my problem. At the way this is currently setup I have no issues with most things. However my problem arises when it should be identifying a No_Triangle as a Scalene. Also keep in mind for any triangle to be considered a triangle any two of its sides must be greater than the third.

Here is an example of my various outputs and what problem I keep having.

Code:

```Please enter a value for side 1 of your triangle:  1 Please enter a value for side 2 of your triangle:  1 Please enter a value for side 3 of your triangle:  1 The lengths of the sides that you entered were:  1, 1, and 1. Equilateral! Would you like to continue (Y or N)?  y Please enter a value for side 1 of your triangle:  12 Please enter a value for side 2 of your triangle:  12 Please enter a value for side 3 of your triangle:  11 The lengths of the sides that you entered were:  12, 12, and 11. isoceles! Would you like to continue (Y or N)?  y Please enter a value for side 1 of your triangle:  1 Please enter a value for side 2 of your triangle:  2 Please enter a value for side 3 of your triangle:  3 The lengths of the sides that you entered were:  1, 2, and 3. Scalene! Would you like to continue (Y or N)?  y Please enter a value for side 1 of your triangle:  900 Please enter a value for side 2 of your triangle:  1 Please enter a value for side 3 of your triangle:  3 The lengths of the sides that you entered were:  900, 1, and 3. Scalene! Would you like to continue (Y or N)?  y Please enter a value for side 1 of your triangle:  3 Please enter a value for side 2 of your triangle:  1 Please enter a value for side 3 of your triangle:  1 The lengths of the sides that you entered were:  3, 1, and 1. Scalene!```
Note that the bolded ones should not be Scalene, but in fact should be No_Triangle.

Not sure on the logic needed to make this happen.

Any help would be greatly appreciated.
• 03-03-2005
Kybo_Ren
EDIT: See below.
• 03-03-2005
Kybo_Ren
Except that if any of theose are true, then no triangle exists. So it should stand at ||, and not &&. You could switch the less than to a greater than and use &&, but this way is fine.
• 03-03-2005
Xipher
Umm, have you tried using an if elsif else structure, instead of just all if's, as that is quite possibly the problem.

Another posibility, just return what you determine, instead of putting it into result, that way once it finds the correct type, then it won't have a chance to check, and accedentily get changed.
• 03-03-2005
Kybo_Ren
By golly, you're right. How stupid of me.

Quote:

if(sOne + sTwo < sThree || sOne + sThree < sTwo || sTwo + sThree < sOne)
Code:

`if((sOne + sTwo <= sThree) || (sOne + sThree <= sTwo) || (sTwo + sThree <= sOne))`
EDIT: Take notice that the last one that isn't bolded is not a triangle either. The sum of the lengths of two sides of a triangle must be greater than the length of the third.

You could also place a return result; after every if statement if you wanted to.
• 03-03-2005
Xipher
Quote:

Originally Posted by Kybo_Ren
By golly, you're right. How stupid of me.

Code:

`if((sOne + sTwo <= sThree) || (sOne + sThree <= sTwo) || (sTwo + sThree <= sOne))`
EDIT: Take notice that the last one that isn't bolded is not a triangle either. The sum of the lengths of two sides of a triangle must be greater than the length of the third.

You could also place a return result; after every if statement if you wanted to.

If he doesn't either use returns after every if, or change it to if/elsif structure, its going to keep getting the SCALENE result. look

Code:

```if(sOne != sTwo && sOne != sThree)         result = SCALENE;```
sOne was never equal in either of the test cases, which means this if would be taken, and result would be assigned SCALENE.