Program using the Pythagorean theorem

I have written a program using the Pythagorean theorem to figure out if the triangle is a right triangle or not. We are supposed to have it set up so that no matter which number is entered first the program knows which number is the hypotenuse. I have it written, and it is compiling, but, for some reason it is requesting that each number be entered twice. And, I can't get it to give a yes to whether it is a right triangle. Any suggestions or help would be greatly appreciated.

Crazy but I did figure some of it our. It is partially working. Just needs a little more refinement on the sorting and to figure out why you have to input the side lengths twice but, it works and does a little sorting.


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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
#include <iomanip>
#include <cmath>


using namespace std;

int main()
{

double num1,num2,num3;


                     //triangle side lengths request section;

  cout << "Please enter the first side of your triangle's length:" <<endl;
  cin >> num1;
  cout<<endl;
    {


        if(num1<0)
            cout<<"Please enter a positive number:"<< endl;
                cin>>num1;
                cout<<endl;

    }
  cout << "Please enter the second side of your triangle's length:"<< endl;
  cin >> num2;
  cout<<endl;
    {

        if(num2<0)
            cout<<"Please enter a positive number:"<< endl;
                cin>> num2;
                cout<<endl;

    }

  cout << "Please enter the third side of your triangle's length:"<< endl;
  cin >>num3;
  cout<<endl;

    {

        if(num3<0)
            cout<<"Please enter a positive number:"<< endl;
                cin>>num3;
                cout<<endl;

    }

   double a,b,c;                               //Side length sorting section;

    {


     if(num1<num2)
            a=num1;
            cout<<endl;
        if(num2<num1)
        a=num2;
        cout<<endl;
    }
    {


    if(num2<num3)
        b=num2;
        else
            b=num3;
        cout<<endl;
    }


    {



    if(num3>num2&&num3>num2)
        c=num3;
    else
        c=num1;

            cout<<endl;
    }


        {



        if((pow(a,2))+(pow(b,2))== (pow(c,2)))
            cout<< "Your triangle is a right triangle!"<<endl;
        else
            cout<< "I'm sorry, your triangle is not a right triangle."<<endl;
        }




  return 0;


}


Last edited on
Assuming that you have been introduced to functions:

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
#include <iostream>
#include <iomanip>
#include <cmath>

double read_side( const char* tag )
{
    std::cout << "Please enter the " << tag << " side: " ;
    double d ;
    if( std::cin >> d && d > 0 ) return d ;

    // handle error
    // http://www.cplusplus.com/reference/ios/ios/clear/
    std::cin.clear() ; // clear input error state

    // http://www.cplusplus.com/reference/istream/istream/ignore/
    std::cin.ignore( 1000, '\n' ) ; // throw away any non-numeric input

    std::cerr << "Please enter a positive number\n" ;

    // and try again
    return read_side(tag) ;
}

int main()
{
    // floating point computations are approximate
    // if the absolute difference between x and y is less than the tolerance,
    // we treat x and y as equal
    const double tolerance = 0.0001 ;

    // read the three sides
    double a = read_side( "first" ) ;
    double b = read_side( "second" ) ;
    double c = read_side( "third" ) ;

    // make a the largest of the three
    // http://www.cplusplus.com/reference/algorithm/swap/
    if( a < b ) std::swap(a,b) ;
    if( a < c ) std::swap(a,c) ;
    
    std::cout << std::fixed << "\nthe largest side is " << a 
              << " and the other two sides are " << b << " and " << c << '\n' ;

    // is it a triangle?
    if( a > (b+c) ) std::cerr << "These sides do not form a triangle\n" ;

    // is it right-=angled?
    // http://www.cplusplus.com/reference/cmath/abs/
    else if( std::abs( a*a - ( b*b + c*c ) ) < tolerance )
        std::cout << "Your triangle is a right triangle!\n" ;

    else
        std::cout << "I'm sorry, your triangle is not a right triangle.\n" ;
}

http://coliru.stacked-crooked.com/a/2ebdbd8497e9a52d
That is beautiful but, unfortunately we have not been introduced to functions yet. Through my own research I have seen them used but, the class is basically up to control loops. So, I am limited. But, thank you so much for your input. That is really nice. I will copy that so I have a reference to look at while I am learning to work with functions. Sometimes, for me anyway, a quick reference can make all the difference in my making the connection of how I can use the concept in my own program build.
Brackets
1
2
3
4
5
6
7
8
9
10
if(x == 0)
     cout << "x cannot be 0\n";  //executed if x == 0
     cout << "Enter x:  ";  //always executed
     cin >> x;  //always executed
if(x == 0)
{
     cout << "x cannot be 0\n";  //executed if x == 0
     cout << "Enter x:  ";  //executed if x == 0
     cin >> x;  //executed if x == 0
}
Last edited on
@GRex2595
Actually, only lines 3 and 4 will ever be executed (regardless of what x is), since
if (x = 0)
first assigns the value of 0 to x, and then compares whether x (which now has a value of 0) evaluates to true (nonzero), which of course it doesn't.

You probably meant
if (x == 0) // Two equals signs .
Last edited on
Yeah. I get lazy sometimes and don't recognize where I mess up until compile time or you point these out. Not really my point, though.
Topic archived. No new replies allowed.