hello

I have done this program that prompts the user to input the length of the sides of a triangle and outputs the shape of the triangle. Using an enumeration type, triangleType, that has the values scalene, isosceles, equilateral, and notTriangle. so the problem i ma having with the program is that when it is supposed to find which type of triangle it is it messes up the answer.

instead of outputting:

the shape with the sides of 9, 9, 9 is an equilateral triangle.

it outputs this:

the shape with the sides of 9, 9, 9 is an isosceles triangle instead


would really appreciate the help.

#include <iostream> 
using namespace std; 

enum triangleType{scalene, isosceles, equilateral, notTriangle}; 
triangleType shape; 

void inputTriangle(int& L1, int& L2, int& L3); 

void outputShape(int L1, int L2, int L3, triangleType shape); 

triangleType triangleShape(int L1, int L2, int L3, triangleType& shape); 


int main()
{ 
	int L1, L2, L3;   

	inputTriangle(L1, L2, L3); 
	triangleShape(L1, L2, L3, shape);
	outputShape(L1, L2, L3, shape); 

	return 0; 
} 

void inputTriangle(int& L1, int& L2, int& L3) 
{ 
	cout<<"Enter the lengths of each side of the triangle (seperate with spaces)."<<endl; 
	cin>>L1>>L2>>L3; 
}// end of inputTriangle.  
 
void outputShape(int L1, int L2, int L3, triangleType shape) 
{ 
	switch(shape) 
	{ 
		case scalene: 
			cout<<"The shape with the sides of "<<L1<<", "<<L2<<", "<<L3<<" is a scalene triangle."<<endl;
			break; 
		case isosceles: 
			cout<<"The shape with the sides of "<<L1<<", "<<L2<<", "<<L3<<" is an isosceles triangle."<<endl; 
			break; 
		case equilateral: 
			cout<<"The shape with the sides of "<<L1<<", "<<L2<<", "<<L3<<" is an eqilateral traiangle."<<endl; 
			break; 
		case notTriangle: 
			cout<<"The shape with the sides of "<<L1<<", "<<L2<<", "<<L3<<" is not a triangle."<<endl; 
	} // end of switch 
} //end of outputShape. 

triangleType triangleShape(int L1, int L2, int L3, triangleType& shape) 
{ 
	if (L1 + L2 > L3) 
		if (L1 != L2 && L2 != L3)
			shape = scalene; 
		else if (L1 == L2 || L2 == L3 || L1 == L3) 
			shape = isosceles; 
		else if (L1 == L2 && L2 == L3)
			shape = equilateral; 
		else 
			shape = notTriangle; 

		return shape; 
} //end of outputShape

Look at the condition on line 54. What happens when all of the sides are equal? Try testing for equilateral first.

Also, while you are restructuring the loop, what happens when L1+L2 <=L3 (I think you tried to account for it but be careful which if/else pairs are associated with each other)

Edited 6 Years Ago by jonsca: n/a

Look at the condition on line 54. What happens when all of the sides are equal? Try testing for equilateral first.

Also, while you are restructuring the loop, what happens when L1+L2 <=L3 (I think you tried to account for it but be careful which if/else pairs are associated with each other)

thanks for the help.
so something like this then right

triangleType triangleShape(int L1, int L2, int L3, triangleType& shape) 
{ 
	if (L1 + L2 > L3) 
		 if (L1 == L2 && L2 == L3)
			shape = equilateral; 
		else if (L1 == L2 || L2 == L3 || L1 == L3) 
			shape = isosceles; 
		else if (L1 != L2 && L2 != L3) 
			shape = scalene; 
		else 
			shape = notTriangle; 

		return shape; 
} //end of outputShape

however now it doesn't know the difference between scalene and notTriangle. Also i don't understand what you mean with L1 + L2 > L3, and the if/else pairs.

Your "else" is associate with the if(L1==L2 && L2==L3) you want it to be associated with the if(L1+L2 > L3) All I was saying is that if L1+L2 <=3 then it skips everything else and returns whatever is in the empty shape variable.

Your "else" is associate with the if(L1==L2 && L2==L3) you want it to be associated with the if(L1+L2 > L3) All I was saying is that if L1+L2 <=3 then it skips everything else and returns whatever is in the empty shape variable.

o ok thank you for your help.

This article has been dead for over six months. Start a new discussion instead.