Something wrong with my formula for a right triangle...

I thought I had completed my program correctly, but when testing it, I see that two of these should be right triangles. Where in my code did I make the mistake? I've gone over it and over it, and I can't seem to find the problem. I'm assuming the problem lies in the if statement that calculates whether or not it is a right triangle (begins at line 48)? Any help would be greatly appreciated.

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
//This is a program that reads in 3 integers from an input file (triangle2.txt)
//that represent line lengths and determines whether or not the three segments
//form a right triangle.  If they don't, it checks whether they form a valid
//triangle at all.  Line lengths must be between 1 and 5000.


#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>

using namespace std;

ifstream infile;
ofstream outfile;

int main()
{
    int a, b, c, d, hyp, short1, short2 = 0;        //Decalre and initialize variables a. b, c

    infile.open("triangle2.txt");                           //Open infile triangles.txt
    if(!infile)                                             //If infile does not open
    {
        cout << "Error opening input file." << endl;        //Display error message
        return 1;                                           //Quit
    }

    outfile.open("answers.out");                            //Open outfile answers.out
    if(!outfile)                                            //If outfile does not open
    {
        cout << "Error opening output file." << endl;       //Display error message
        return 1;                                           //Quit
    }

    infile >> a >> b >> c;                                  //Read in a, b, c
    while( infile )                                         //While reading in info
    {
        outfile << setw(14) << "Three sides:" << a << " " << b << " " << c << endl;   //Print to outfile a, b, c
        cout    << setw(14) << "Three sides:" << a << " " << b << " " << c << endl;      //Display a, b, c

        if(a && b && c <= 5000)                              //If a, b, and c are less than or equal to 5000
        {
           d = max(a,b);                                     //Find the larger of the first 2 sides
           hyp = max(d,c);                                   //Determine longest side (hypotenuse)
           short1 = min(a,b);                                //Determine short sides
           short2 = min (short1, c);

           if ( hyp == (sqrt(pow(short1,2)+ pow(short2,2))))  //Check to see if it is a right triangle
           {
               outfile << " -Above dimensions indicate a right triangle." << endl;//Display that it's a right triangle
           }
           else
           {
                if((a + b > c) || (b + c > a) || (a + c > b)) //If not, check to see if it is a valid triangle
                {
                    outfile << " -Above dimensions indicate a valid triangle." << endl; //Display that it's a valid triangle
                    cout    << " -Above dimensions indicate a valid triangle." << endl; //Echo valid triangle
                }
                else                                          //If not valid triangle,
                {
                    outfile << " -Above dimensions indicate that this is NOT a valid triangle." << endl; //This is NOT a triangle
                    cout    << " -Above dimensions indicate that this is NOT a valid triangle." << endl; //Echo display
                }
           }
        }
        else                                                  //Or else, if it's not between 1 and 5000
        {
            outfile << " -This program only computes triangles with sides between 1 and 5000." << endl; //Distplay error
            cout    << " -This program only computes triangles with sides between 1 and 5000." << endl; //Echo error
        }
        infile >> a >> b >> c;      //Read in the next line of data from infile

    }
    return 0;                       //Quit

}
Last edited on
Let's say the three values entered are 3,4,5. So a=3, b=4, c=5.


short1 = min(a,b); //Determine short sides

So now, short1 = a

short2 = min (short1, c);

And short2 = a

Shouldn't short2 be b?
1
2
3
4
           d = max(a,b);                                     //Find the larger of the first 2 sides
           hyp = max(d,c);                                   //Determine longest side (hypotenuse)
           short1 = min(a,b);                                //Determine short sides
           short2 = min (short1, c);
Suppose that b<c<a, then
hyp = a
short1 = short2 = b


1
2
if(a && b && c <= 5000) //if c<=5000, 'a' and 'b' are not 0
if(a<=5000 and b<=5000 and c<=5000)//If a, b, and c are less than or equal to 5000 


if ( hyp == (sqrt(pow(short1,2)+ pow(short2,2)))) You may have precision issues. Don't take the square root, but compare the square of the hypotenuse.

if((a + b > c) || (b + c > a) || (a + c > b)) //If not, check to see if it is a valid triangle The condition is incorrect. It should be 'and'
Last edited on
Thanks for the replies. I'm working through them trying to understand where I went wrong...
Also, I see that I didn't include my input file, and that may help a bit. I'll include it below, in case it makes a difference...

For the first comments about short1 and short2. I see how that works for your examples, but I need it to work for every line in the input file...would your answer still do that?

And for the square root comment, I actually had started out that way, but I've since changed it, hoping that would help. It hasn't. Lol.

ne555, I have a question about your last comment. The validation for a valid triangle we were given is that the sum of any two sides is larger than the third. But the sides can be input in any order. So wouldn't that call for the use of 'or' and not 'and' ?
~ If not, can you explain why?

Input file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
10 8 4
4 8 10
90 200 125
5 12 6
90 181 90
1588 2300  4000
90 90 180
5 25 45
6000 7500 7000
1 1 1
1000 1 1000
50 20 5
1 1000 2500
0 0 0


Lines 10 and 11 from this input file should show up as valid right triangles. They show up only as valid triangles now, the way it's written.
Last edited on
The validation for a valid triangle we were given is that the sum of any two sides is larger than the third.
Considerer 1 2 4

By the way while( infile>>a>>b>>c )
Last edited on
So maybe I'm going about this wrong. When I hear "any" I think if a+b>c OR if a+c> b OR if b+c> a, but is that wrong? Because a+c here is >b, but a+b is not greater than c. So, does that make it a valid triangle, or not?
If any one side is greater in length that the sum of the other two sides, it cannot possibly be a triangle. Which triangle do you think you have found where c is greater than a+b? It's not.
Last edited on
Lines 10 and 11 from this input file should show up as valid right triangles.


These two lines are definitely not right angled triangles.
Line 14 would show up as one though.
1,1,1 is an equilateral triangle.

You also don't echo the right angle confirmation to cout so you need to be viewing the outfile to check whether this part of the code is working.
Last edited on
Thanks. I did catch the right angle confirmation, so I fixed that. But the online triangle 'calculator' that I was using to check the program was not helping. It was telling me that some of the triangles were right angles when they were not. I've fixed the code now, so that it actually works.
Thanks for your help. :)
Topic archived. No new replies allowed.