How to find missing side on a right angle triangle. (NEED HELP WITH CODE, CODE NOT WORKING PROPERLY)

// Calculating the missing side of a triangle by Zachary Wilczak

#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;

int main ()
{
int x; //x is for sin, cos or tan
double s; //s is for side length
double a; //a is for the angle
int hyp; //hyp is for whether you are finding the hypotenuse or not
double ans; //ans is for the answer
int tang; //tang is only if the answer is going to have to use the tangent function
double pi = 3.14159265; //Just in case of need for some pi, maybe needing to turn in degrees
cout << "Sin = 1, Cos = 2, Tan = 3" << endl;
cin >> x;
cout << "Are you trying to find the hypotenuse? 0 = false 1 = true" << endl;
cin >> hyp;
cout << "Define the angle" << endl;
cin >> a;
cout << "Define the length of the side" << endl;
cin >> s;
if (x == 1) //sin function
{
if (hyp == 0)
{
ans = s*sin(a);

}
else
{
ans = s/sin(a);
}
}

else if (x == 2) //cos function
{
if (hyp == 0)
{
ans = s*cos(a);
}
else
{
ans = s/cos(a);
}
}

else if (x == 3) //tan function
{
cout << "For ADJ do 0, for OPP do 1 " << endl;
cin >> tang;
if (tang == 0)
{
ans = s/tan(a);
}
else
{
ans = s*tan(a);

}
}

else
{
cout << "You did not enter the right number" << endl;
}
cout << "The answer is " << ans << endl;

return 0;
}
closed account (48bpfSEw)
code compiles and runs on http://cpp.sh/. what is wrong with it?

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
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;

int main()
{
    int x; //x is for sin, cos or tan
    double s; //s is for side length
    double a; //a is for the angle
    int hyp; //hyp is for whether you are finding the hypotenuse or not
    double ans; //ans is for the answer
    int tang; //tang is only if the answer is going to have to use the tangent function
    double pi = 3.14159265; //Just in case of need for some pi, maybe needing to turn in degrees
    cout << "Sin = 1, Cos = 2, Tan = 3" << endl;
    cin >> x;
    cout << "Are you trying to find the hypotenuse? 0 = false 1 = true" << endl;
    cin >> hyp;
    cout << "Define the angle" << endl;
    cin >> a;
    cout << "Define the length of the side" << endl;
    cin >> s;
    if (x == 1) //sin function
    {
        if (hyp == 0) {
            ans = s * sin(a);
        }
        else {
            ans = s / sin(a);
        }
    }

    else if (x == 2) //cos function
    {
        if (hyp == 0) {
            ans = s * cos(a);
        }
        else {
            ans = s / cos(a);
        }
    }

    else if (x == 3) //tan function
    {
        cout << "For ADJ do 0, for OPP do 1 " << endl;
        cin >> tang;
        if (tang == 0) {
            ans = s / tan(a);
        }
        else {
            ans = s * tan(a);
        }
    }

    else {
        cout << "You did not enter the right number" << endl;
    }
    cout << "The answer is " << ans << endl;

    return 0;
}
It does not give me the right answer. I have tried with multiple questions and they all give me the wrong answer
closed account (48T7M4Gy)
1
2
3
cout << "Define the angle (degrees)" << endl;
    cin >> a;
    a = a * pi / 180;


Gives you correct answers for sin and cos at least. You must make sure your of the units for the angles. If you put in 30 degrees and don't make the conversion to degrees sin will be sin(30 radians) which is 30*180/pi degrees!

PS Looks good for tan too.
Last edited on
i dont understand, i tried the thing "kemort" explained but it didnt work, and plus i found a question and put in the radians instead of the degrees and the answer gave me the correct answer in degrees.? i tried to make the code convert the input into radians but it still doesnt work. It says the variable "ans" may be used unitialized when i try to convert the degrees to radian
closed account (48T7M4Gy)
All you have to do is replace Necip's lines 19 and 20 with my lines 1,2 and 3 and your program will behave properly. Just type an angles in degrees your revised program will convert to radians.
Last edited on
I tried what you did kemort, and thankfully it worked. thank you so much
closed account (48T7M4Gy)
:)
Topic archived. No new replies allowed.