// 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;
#include <iostream>
#include <math.h>
#include <iomanip>
usingnamespace 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);
}
}
elseif (x == 2) //cos function
{
if (hyp == 0) {
ans = s * cos(a);
}
else {
ans = s / cos(a);
}
}
elseif (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;
}
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!
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
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.