triangle classification help plz

Hi everybody, I'm just looking for some help as I have seem to of hit a brick wall :(. Here are my instructions for my program. "You are to write a C++ program that accepts as input from the console three integer line segment lengths in cm. Your
program is to determine whether or not the three line segments could be used to form a triangle. If a triangle can be
formed your program should classify the triangle as scalene, isosceles or equilateral. It should also determine whether
or not the triangle is a right triangle and should calculate the perimeter of the triangle and the area of the triangle using
Heron’s formula Your program should validate the input to make sure 0 and
negative line segment lengths are not processed."

Sorry for so much text. Just didn't know how it could have been worded better than the actual instructions.

Well my problem is, is that I don't know how to add into the script that I have written now to address the part to check if the triangle is a right triangle. And then how do I display that it is a right triangle. Here is my program as is right now. Any help is appreciated. Keep in mind I am a complete noob at this stuff. Thanks for any tips and advice.


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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <cmath>
#include <fstream>

using namespace std;

int main()

{

	ofstream DataOut ("Output.txt");

	int SideA, SideB, SideC, SP, Area, SideCS;
	int Sum, RightT;

	cout<< "Triangle Classification Program"<<endl;

	cout<<"Please enter three sides of a triangle(in cm) with a space inbetween each side.";
	cin >> SideA >>SideB >>SideC;
	SP=(SideA+SideB+SideC)/2; 						 //part of heron's formula
	Area= sqrt(SP*(SP-SideA)*(SP-SideB)*(SP-SideC)); //forumula for area
	Sum=(SideA^2)+(SideB^2);
	SideCS=SideC^2;





if(SideA<0 || SideB<0 || SideC<0)
	{
				DataOut<<"The line segments are: ";
				DataOut<<SideA <<" cm, "<<SideB<<" cm, "<<SideC<<" cm."<<endl;
				DataOut<<"The data entered is invalid.";
	return 1;

	}
	else if (SideA==SideB && SideB==SideC)
	{
				DataOut<<"The line segments are: ";
				DataOut<<SideA <<" cm, "<<SideB<<" cm, "<<SideC<<" cm."<<endl;
				DataOut<<"The triangle is equilateral."<<endl;
				DataOut<<"The area of the triangle is "<<Area<<" cm.";

	}

	else if (SideA == SideB || SideB==SideC || SideA==SideC)
	{
				DataOut<<"The line segments are: ";
				DataOut<<SideA <<" cm, "<<SideB<<" cm, "<<SideC<<" cm."<<endl;
				DataOut<<"The triangle is Isosceles."<<endl;
				DataOut<<"The area of the triangle is "<<Area<<" cm.";
	}

	else if(SideA != SideB && SideB != SideC && SideA !=SideC)
	{
				DataOut<<"The line segments are: ";
				DataOut<<SideA <<" cm, "<<SideB<<" cm, "<<SideC<<" cm."<<endl;
				DataOut<<"The triangle is Scalene."<<endl;
				DataOut<<"The area of the triangle is "<<Area<<" cm.";

	}
	else if(Sum==SideCS)
	{
			DataOut<<"This triangle is a right triangle.";
	}














return 0;
}
If the sum of any 2 sides is greater than the third side, the triangle can be formed. By using some math, you can modify the above condition to "if the sum of the smalest 2 sides is greater than the semi perimeter, the triangle can be formed".

You should check whether a triangle can be formed or not (i.e. above condition and no sides -ve) before calculating area.

You program will stop before reaching the right angle part because an if condition will be met.

^ is not the exponential operator.

You do not need to check if all sides are unequal in the case of a scalene triangle. You are checking if the triangle is isosceles (or equilateral) before that. So if it is isosceles, it will not reach the scalene else-if part.

You are displaying sides, perimeter and area in each else-if block. Shift these out.

Try reducing the number of variables and the no of lines of code (obviously without changing program output behavior). Less variables and less code more the program simpler to understand and debug.

Also, use double instead of int and the area can be in decimals. You shouldn't use int even if you want the answer rounded off to an int, as the semi perimeter will be calculated wrongly using an int.

If you don't get it right, I can post source code. You'll have to ask for it. Don't ask to misutilize (i.e. copy and paste for homework), I'm trusting your conscience.
Last edited on
I don't know how to add into the script that I have written now to address the part to check if the triangle is a right triangle.


Identify which side is the longest. Then use Pythagoras' Theorem to test whether the square of this side equals the sum of the squares of the other two sides.

However, the else-if structure of your existing code may not fit in with this. For example, an isosceles or scalene triangle may also have a right angle. Thus you may have to re-arrange the existing code slightly.

Topic archived. No new replies allowed.