What's wrong with this code?

closed account (28ApDjzh)
This is code for determining the type of a triangle. I need help with it- what's wrong?

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
#include <iostream>
using namespace std;

int main()
{
    
    cout << "This program determines types of triangles." << endl
         << "Enter the lengths of each side of the triangle" << endl
         << "and the program will determine what type of triangle it is." << endl << endl;
        
    
    double a;
    double b;
    double c;
    
    
    cout << "Length of side a: ";
    cin >> a;
    cout << "Length of side b: ";
    cin >> b;
    cout << "Length of side c: ";
    cin >> c;
    if ((a == b && b == c && a == c)) {
        cout << "This is an equilateral triangle." << endl;
}
    
    else if((a != b && b != c) || (b != c && c != a) || (a != c && c != b))
    {
        cout << "This is an obtuse scalene triangle." << endl;
        }
    else {
        cout << "This is an acute scalene triangle." << endl;
         }
    else if (a + b == c) {
        cout << "This is a right triangle." << endl;
        }
        
        else if (a == b && b == c && c != a) {
        cout << "This is an isosceles triangle." << endl;
        }
    
    
    if ((a + b >= c) || (a + c >= b) || (b + c >= a))
        cout << "Those lengths do not form a triangle." << endl;
}
Last edited on
Dunno, but the hightlight checks are the same. Did you mean for check's 2 and 3 to be the same?
 
else if((a != b && b != c) || (b != c && c != a) || (a != c && c != b))
closed account (28ApDjzh)
Yeah, I did.
It's pretty pointless doing the same check twice.

You're going to have to work out at least one angle to get beyond equalateral and isosoles.
closed account (28ApDjzh)
How would I work out the angles? (to tell the truth, this is to debug)

EDIT: OK, so, I started from scratch. Here is the code below- whenever it should be printing that the legs do not from a triangle it also prints the code for obtuse scalene, and also, how do I do the acute scalene? (very important)

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

#include <iostream>
using namespace std;

int main()
{
    
    cout << "This program determines types of triangles." << endl
         << "Enter the lengths of each side of the triangle" << endl
         << "and the program will determine what type of triangle it is." << endl << endl;
        
    
    double a;
    double b;
    double c;
    
    
    cout << "Length of side a: ";
    cin >> a;
    cout << "Length of side b: ";
    cin >> b;
    cout << "Length of side c: ";
    cin >> c;
    
   
    if (a == b && b != c || a == c && b != c)
        cout << "This is an isosceles triangle." << endl;
    
   
    else if (a == b && b == c && a == c) {
        cout << "This is an equilateral triangle." << endl;
}
    
    if((a != b && c) && (b != c && a) && (a != c && b)) 
        cout << "This is an obtuse scalene triangle." << endl;
      
    if (a*a + b*b == c*c)
        cout << "This is a right triangle." << endl;
    
  
    else if ((a + b > c) || (a + c > b) || (b + c > a))
        cout << "Those lengths do not form a triangle." << endl;
}
Last edited on
You can do this without the angles by using the Pythagorean Theorem.

1. Check to see if the triangle is possible given the side-lengths (you currently have this as the last check)
2. Check to see if the triange is an equilateral triangle.
3. Check to see if the triangle is an isosceles triangle.
4. If the triangle is possible but not equilateral or isosceles, you have a scalene triangle.

To check whether an isosceles or scalene triangle is acute, right, or obtuse, find the longest side given to you and treat it as the hypotenuse c. Call the other sides a and b (or whatever variable names you prefer).
a2 + b2 = c2 : right triange
a2 + b2 < c2 : obtuse triange
a2 + b2 > c2 : acute triange
Last edited on
Topic archived. No new replies allowed.