Ok the requirements include a program that asks for three positive number and checks if they are a triangle. I did that ok, but now I have to figure out what kind of triangle that I entered is.

The choices include:

right angle triangle
acute triangle (all three angles < 90 degrees)
obtuse triangle (one of the three angles is greater than 90 degrees),
isosceles triangle (two sides are equal)
equilateral triangle (all three sides are equal)

The program is supposed to work with numbers entered like 3 4 5, 1 1 2, and 1 1 1

Here is what I have but the bottom portion where it checks for what triangle is wrong I know.

#include <iostream>
using namespace std;

int main()
double a, b, c;
   cout << "Enter 3 positive numbers \n";
   cin >> a >> b >> c;

   bool isTri = false;

   if (a > 0 && b > 0 && c > 0 &&
	   a + b > c && b + c > a && c + a > B)
   {isTri = true;}
  if (isTri && c==sqrt(a*a + b*b) || a==sqrt(b*b + c*c) || b==sqrt(a*a + c*c))
   {cout<<"This is a right angle triangle\n";}
   if(isTri && a<90 && b<90 && c<90 && a!=b!=c)
   {cout<<"\nThis is an acute triangle\n";}
   if (isTri && a>90 || b>90 || c>90)
   {cout<<"\nThis is an obtuse angle\n";}
   if (isTri && a==b || b==c || a==c)
   {cout<<"\nThis is an isosceles triangle\n";}
   if (isTri && a==b==c)
   {cout<<"\nThis is an equilateral triangle\n";}
   {cout << "The three nubmers "<<a<<", "<<b<<" and "<<c<<" do not form a triangle\n";}
   return 0;


Edited by johnb08: n/a

8 Years
Discussion Span
Last Post by johnb08

Law of cosines (http://en.wikipedia.org/wiki/Law_of_cosines) will help you get the angles between all of them.

I would test for the right triangle that way rather than your method (especially since your values a,b,and c are doubles and comparing those for equality is tricky (since a number close enough for practical purposes will not be equal, 1.0001 != 1.0).

If you are going to use that Pythagorean approach some of your formulas are off if a^2+b^2 = c^2 then a^2 = c^2-b^2, etc.

Votes + Comments
rep. for the double comparison educational outreach

Can I just comment (a) Jonsca is correct, you are going to have to define a tolerance and (b) it is going to be lot easier to calculate the cos(angles) of the triangle and go from there..

Also you cannot do this a==b==c It compiles but DOES NOT do what you think. It is the equivelent to this: if ((a==0 && b!=c) || (a==1 && b==c)) That is because b==c is converted to a boolean , i.e. 0 or 1 and then compared with a. So it will only be true if a==0 or a==1 and then if will depend of the result of b==c. If a=2;b=2;c=2 it will be false.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.