954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

triangles

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
superchica08
Newbie Poster
9 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

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)

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

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.

superchica08
Newbie Poster
9 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

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.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 
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.

superchica08
Newbie Poster
9 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: