This program, basically asks user for the lengths of 3 sides of a triangle, checks if it is actually a triangle, and if so it tells you what type of triangle it is:
Looking at line 31. Basically what it's supposed to do is - if the sum of the three angles is not 180, then it should say it's not a triangle. But it doesn't. However if I change != to == (which doesn't make sense to me) the program works perfectly. Do I misunderstand something?
Code:
#include <iostream.h>
#include <math.h>
int main()
{
double s1,s2,s3; //input variables (side lengths)
double longest,short1,short2; //sides arranged by their length
double angle1,angle2,angle3;
cin >> s1;
cin >> s2;
cin >> s3;
longest=s1;
short1=s2;
short2=s3;
if (longest<s2) //determining the longest and shorter sides
{
longest=s2;
short1=s1;
short2=s3;
}
if (longest<s3)
{
short1=s1;
short2=s2;
longest=s3;
}
angle1 = acos(((short1*short1)+(short2*short2)-(longest*longest))/(2*short1*short2))*(180/3.14159);
angle2 = acos(((longest*longest)+(short2*short2)-(short1*short1))/(2*longest*short2))*(180/3.14159);
angle3 = acos(((longest*longest)+(short1*short1)-(short2*short2))/(2*longest*short1))*(180/3.14159);
if ((angle3+angle2+angle1) != 180)
{
cout << "Not a triangle" << endl;
}
else
{
if (s1 == s2 && s2 == s3) //two of the sides are equal
{
cout << "Equilateral and Acute" << endl;
}
else if ((short1*short1 + short2*short2) == (longest*longest)) //pythagoras theorem
{
cout << "Right Angle" << endl;
}
else if (s1 == s2 || s2 == s3 || s1 == s3) //all sides are equal
{
if ((short1*short1 + short2*short2) < (longest*longest)) //an angle is just less than 90 degrees
{
cout << "Isosceles and obtuse" << endl;
}
else
{
cout << "Isosceles and acute" << endl;
}
}
else
{
if ((short1*short1 + short2*short2) < (longest*longest))
{
cout << "Scalene and obtuse" << endl;
}
else
{
cout << "Scalene and acute" << endl;
}
}
}
return 0;
}
Comment