If statement question

Feb 19, 2015 at 3:38am
Use three separate if() statements for the tests (one for each possible hypotenuse). Remember if you don't find a right triangle in any of the three if() statements for a fourth if() statement to print a message saying the sides don't form a right triangle. Do not use if-else() for this, just four if() statements.
Your fourth if() statement may not test the sides entered by the user. You must
REMEMBER if you have seen a right triangle or not.

Could anyone explain what does the fourth statement mean and give me a hint?

I have no idea what should I do now because of the restriction for the fourth if statement.

Another question is when I declared the variables as integer, the calculation for the pow(x,y) was incorrect. For example, the result for 5 square became 24 instead of 25. Does this mean only float is working for the pow() function?

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

int main()
{
    float side1, side2, side3, hy1, hy2, hy3;

    cout << "Enter the length of the three sides: " << flush;
    cin  >> side1;
    cin  >> side2;
    cin  >> side3;
    cout << endl;

    side1 = pow (side1, 2);
    side2 = pow (side2, 2);
    side3 = pow (side3, 2);

    hy1 = side1 + side2;
    hy2 = side2 + side3;
    hy3 = side1 + side3;

    if (side1 == hy2)
    {
        cout << "It is a right triangle.";
    }
    if (side2 == hy3)
    {
        cout << "It is a right triangle.";
    }
    if (side3 == hy1)
    {
        cout << "It is a right triangle.";
    }
    if ( )
    // I'm stuck here since I can't use the user input as the test condition.
    {
Last edited on Feb 19, 2015 at 7:12am
Feb 19, 2015 at 4:14am
Is this all the code you have if not you will need to show more for help
Feb 19, 2015 at 4:52am
you can actually use the user input as the test condition.
Feb 19, 2015 at 4:53am
What is the different between these statements?

1
2
3
4
5
6
7
8
9
10
11
12
    if (side1 == hy2)
    {
        cout << "It is a right triangle.";
    }
    if (side2 == hy3)
    {
        cout << "It is a right triangle.";
    }
    if (side3 == hy1)
    {
        cout << "It is a right triangle.";
    }


1
2
3
4
5
6
7
8
9
10
11
12
    if (side1 == hy2)
    {
        cout << "It is a right triangle.";
        if (side2 == hy3)
        {
            cout << "It is a right triangle.";
            if (side3 == hy1)
            {
                cout << "It is a right triangle.";
            }
        }
    }
Feb 19, 2015 at 4:56am
Unfortunately, I not allowed to use the user input as test condition.
Feb 19, 2015 at 5:04am
closed account (D80DSL3A)
Can you use a bool variable to "remember" that a rt. triangle has been found?
1
2
3
4
5
6
7
bool isRtTri = false;
if (side1 == hy2)
    {
        isRtTri = true;
    }
// same with others
if ( isRtTri ) cout << "It is a right triangle.";
Feb 19, 2015 at 7:15am
Yeah, it works.
I learned a new function today.
Thank you very much! Appreciated!
Last edited on Feb 19, 2015 at 7:17am
Feb 19, 2015 at 7:19am
What is the different between these statements?


in the first one.

only one will be executed if you run the program.


in the second

there is a possibilty that all statements will be executed if you run the program
Last edited on Feb 19, 2015 at 7:20am
Topic archived. No new replies allowed.