hypotenuse

can someone show me how to set this out, user inputs the angle and the opposite

sin x=opposite/hyp
=>hyp=opp/sin x

Note that functions from the cmath library, such std::sin() expect an angle expressed in radians instead of degrees.

So if you're getting bad results, this is most likely why: you give x in degrees.

You transform degrees in radians by the formula:
radians = degrees * Pi / 180
where Pi = 3.14159265

http://www.cplusplus.com/reference/cmath/sin/

Otherwise I can't tell what you're having problems with... not having posted any code at all. Please don't tell me you need someone to write the program for you. Write it yourself, and if it doesn't work post it here so we can correct it.
thank you for that i put it in to my code but it never worked could you help please

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
// Maths Helper
#include <iostream>
// define identifier PI with a constant
#define PI 3.14159
// define identifier TWO with a constant
#define TWO 2.0
// define identifier cmath with a constant
#define cmath
using namespace std;

char menu()
{
    char choice;
    cout<< "\nMaths Helper";
    cout<< "\n\n**************************************\n";
    cout<< "\n\n Please choose one of the following:\n";
    cout<< "\n\n 1 - Area of a circle ";
    cout<< "\n 2 - Circumference of a circle ";
    cout<< "\n 3 - Factorial of a number ";
    cout<< "\n 4 - Hypotenuse of a triangle ";
    cout<< "\n 5 - Exit";
    cout<< "\n\n\n**************************************\n";
    cout<< "\n\n Enter you choice and press return: ";
    cin >> choice;
    return choice;
}

int main()
{
    int num,factorial=1,Angle,Opposite,sin;
    float area, radius, circumference;
    
    char choice;
    
    do
    {
        choice = menu();
        
        switch (choice)
        {
            case '1':
                cout << "\n\nArea of a circle"
                     << "\n\nEnter the radius:";
                cin >> radius;
                // area = PI*radius*radius
                area = PI * radius * radius;
                // circle area
                cout << "\nCircle area = " << area << endl;
                // Pause for user
                system("pause");
            break;             
            case '2':
                cout<< "\n\nCircumference of a circle"
                    <<"\n\nEnter a circumference of a circle:";
                cin>>radius;
                // circumference = 2*PI*radius
                circumference = TWO * PI * radius;
                // circle Circumference
                cout<<"\nCircumference = "<<circumference<<endl;
                // Pause for user
                system("pause");
            break;
            case '3':
                cout<< "\n\nFactorial of a number "
                    <<"\n\nEnter a number to find its factorial:";
                    cin>>num;
                    // a = 1, number less then 1 equals number, increase the number by 1 
                    for(int a=1;a<=num;a++)
                    {
                    // factorial = 1 * 
                    factorial=factorial*a;
                    }
                    //Factorial number
                    cout<<"\n\nFactorial number is ="<<factorial<<endl;
                    // Pause for user
                    system("pause");
                break;
            case '4':
                cout<< "\n\nHypotenuse of a triangle "
                    << "\n\n Enter a Angle:";
                cin>>Angle;
                cout<< "\n\n Enter the Opposite:";
                cin>>Opposite;
                radians = degrees * Pi / 180

                
                system("pause");
                break; 
            case '5':
                cout<< "Exit"; 
                break;
            default:
                cout<< "\nNot a valid choice.";
        }
    } while (choice != '5');

    return 0;
} 
I see you have copy-pasted the formula. That's not what you were supposed to do.

Instead you must use the formula and adapt it to your program, like so:

78
79
80
81
82
83
84
85
86
            case '4':
                cout<< "\n\nHypotenuse of a triangle "
                    << "\n\n Enter a Angle:";
                cin>>Angle;
                cout<< "\n\n Enter the Opposite:";
                cin>>Opposite;
                float radians = Angle * PI / 180;
                cout << "Hypotenuse equals: " << Opposite / sin(radians) << '\n';
                


Edit: also, what is this?

7
8
9
10
11
// define identifier cmath with a constant
#define cmath

// perhaps you meant
#include <cmath> 
Last edited on
i did as you said and these errors came up

84 `degrees' undeclared (first use this function)
85 `sin' cannot be used as a function

what have done 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
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
// Maths Helper
#include <iostream>
// define identifier PI with a constant
#define PI 3.14159
// define identifier TWO with a constant
#define TWO 2.0
// define identifier cmath with a constant
#define cmath
using namespace std;

char menu()
{
    char choice;
    cout<< "\nMaths Helper";
    cout<< "\n\n**************************************\n";
    cout<< "\n\n Please choose one of the following:\n";
    cout<< "\n\n 1 - Area of a circle ";
    cout<< "\n 2 - Circumference of a circle ";
    cout<< "\n 3 - Factorial of a number ";
    cout<< "\n 4 - Hypotenuse of a triangle ";
    cout<< "\n 5 - Exit";
    cout<< "\n\n\n**************************************\n";
    cout<< "\n\n Enter you choice and press return: ";
    cin >> choice;
    return choice;
}

int main()
{
    int num,factorial=1,Angle,Opposite,sin;
    float area, radius, circumference, radians;
    
    char choice;
    
    do
    {
        choice = menu();
        
        switch (choice)
        {
            case '1':
                cout << "\n\nArea of a circle"
                     << "\n\nEnter the radius:";
                cin >> radius;
                // area = PI*radius*radius
                area = PI * radius * radius;
                // circle area
                cout << "\nCircle area = " << area << endl;
                // Pause for user
                system("pause");
            break;             
            case '2':
                cout<< "\n\nCircumference of a circle"
                    <<"\n\nEnter a circumference of a circle:";
                cin>>radius;
                // circumference = 2*PI*radius
                circumference = TWO * PI * radius;
                // circle Circumference
                cout<<"\nCircumference = "<<circumference<<endl;
                // Pause for user
                system("pause");
            break;
            case '3':
                cout<< "\n\nFactorial of a number "
                    <<"\n\nEnter a number to find its factorial:";
                    cin>>num;
                    // a = 1, number less then 1 equals number, increase the number by 1 
                    for(int a=1;a<=num;a++)
                    {
                    // factorial = 1 * 
                    factorial=factorial*a;
                    }
                    //Factorial number
                    cout<<"\n\nFactorial number is ="<<factorial<<endl;
                    // Pause for user
                    system("pause");
                break;
            case '4':
                cout<< "\n\nHypotenuse of a triangle "
                    << "\n\n Enter a Angle:";
                cin>>Angle;
                cout<< "\n\n Enter the Opposite:";
                cin>>Opposite;
                radians = degrees * PI / 180;
                cout << "Hypotenuse = " << Opposite / sin(radians) << '\n';

                
                system("pause");
                break; 
            case '5':
                cout<< "Exit"; 
                break;
            default:
                cout<< "\nNot a valid choice.";
        }
    } while (choice != '5');

    return 0;
} 

Line 30, delete the last variable, named sin.
It has the same name as the std::sin() function, which confuses the compiler.

Line 84, change "degrees" to "Angle" as in:

radians = Angle * PI / 180;

Edit: also, line 8, correct:

#include <cmath>
Last edited on
thank you very much
Topic archived. No new replies allowed.