I'm only posting my entire program so that you can try it and see my issue.
My problem lies with line 223 and onward. I've been trying to work out the sides of a right angled triangle using just side a and an angle (currently trying to work out the hypotenuse using side A and Angle B). However, the program wont allow me to use sine, tangent or cosine properly with failing to give me the correct answer.
If you follow the (badly structured) sections of the end of my program (after line 150), you can see that I'm still missing the sections that provide the user with values should they decide to enter a side and angle value at the beginning.
This is one of my first times I've been able to relatively understand C++ whilst trying it.
Please assist wherever you can, thank you.
also, ignore the goto functions, I'll never use them again.
#include <iostream>
#include <string>
#include <math.h>
#include <cmath>
usingnamespace std;
int main()
{
float Side_a;
float Side_b;
float Side_c;
float Angle_a;
float Angle_b;
char Response;
float End;
float Trig_Count;
End = 0;
Trig_Count = 0;
cout << "\n Trigonometry Calculator! \n\n Press ENTER to continue...";
cin.get();
cout << "\n /|\n";
cout << " /a| \n";
cout << " / | \n";
cout << " c / | \n";
cout << " / |a \n";
cout << " / | \n";
cout << " / | \n";
cout << " /b | \n";
cout << " ---------- \n";
cout << " b \n";
cout << "\n Using the diagram above as a guide... \n\n";
SideA :
cout << "\n Do you know the length of SIDE A? \n y or n ------> ";
cin >> Response;
if (Response == 'y')
{
cout << "Enter SIDE A length ";
End = End + 1;
Trig_Count = Trig_Count + 1;
cin >> Side_a;
goto SideB; //GOTO START OF SIDE B QUESTION
}
if (Response == 'n')
SideB : //SIDE B GOTO POINT
cout << "\n Do you know the length of SIDE B? \n y or n ------> ";
cin >> Response;
if (Response == 'y')
{
cout << "Enter SIDE B length ";
End = End + 1;
Trig_Count = Trig_Count + 10;
cin >> Side_b;
goto SideC; //GOTO START OF SIDE C QUESTION
}
if (Response == 'n')
SideC : //SIDE C GOTO POINT
if (End == 2)
goto EndingP; //GOTO EndingP
cout << "\n Do you know the length of SIDE C? (MUST be larger than sides A and B)\n y or n ------> ";
cin >> Response;
if (Response == 'y')
{
cout << "Enter SIDE C length ";
End = End + 1;
Trig_Count = Trig_Count + 100;
cin >> Side_c;
goto AngleA; //GOTO START OF ANGLE A QUESTION
}
if (Response == 'n')
AngleA : //ANGLE A GOTO POINT
if (End == 2)
goto EndingP; //GOTO EndingP
cout << "\n Do you know the angle of ANGLE A? \n y or n ------> ";
cin >> Response;
if (Response == 'y')
{
cout << "Enter ANGLE A value ";
End = End + 1;
Trig_Count = Trig_Count + 200;
cin >> Angle_a;
goto AngleB; //GOTO START OF ANGLE B QUESTION
}
if (Response == 'n')
AngleB : //ANGLE B GOTO POINT
if (End == 2)
goto EndingP; //GOTO EndingP
cout << "\n Do you know the angle of ANGLE B? \n y or n ------> ";
cin >> Response;
if (Response == 'y')
{
cout << "Enter ANGLE B value ";
End = End + 1;
Trig_Count = Trig_Count + 500;
cin >> Angle_b;
}
if (End == 2)
goto EndingP; //GOTO EndingP
else cout << "Error: At least 2 values must be known and entered\n"; //to inform user that more info is required
system ("PAUSE");
EndingP : //EndingP GOTO POINT
if (Trig_Count == 11) //Side A and Side B known
goto AB11;
if (Trig_Count == 101) //Side A and Side C known
goto AC101;
if (Trig_Count == 110) //Side B and Side C known
goto BC110;
if (Trig_Count == 201) //Side A and Angle a Known
goto Aa201;
if (Trig_Count == 210)
if (Trig_Count == 300)
if (Trig_Count == 501)
if (Trig_Count == 510)
if (Trig_Count == 600)
AB11 : //Side A and B known - 11
Side_c = Side_a * Side_a + Side_b * Side_b;
Side_c = sqrt(Side_c); //pythagoras theorem
Angle_a = Side_b / Side_c; //copy these sections for remaining 'if statements' as they are similar
Angle_a = asin (Angle_a); //sine required for angle. Others will be tangent and cosine
Angle_a = Angle_a * 57.2957795; //Times by 57.2957795 to convert to degrees. Not required for tangent
Angle_b = Side_a / Side_b;
Angle_b = atan (Angle_b);
Angle_b = Angle_b * 57.2957795;
cout << "\n For your triangle...\n Side A = ";
cout << Side_a;
cout << "\n Side B = ";
cout << Side_b;
cout << "\n Side C = ";
cout << Side_c;
cout << "\n Angle A = ";
cout << Angle_a;
cout << "\n Angle B = ";
cout << Angle_b;
AC101 : //Side A and Side C known - 101
Side_b = Side_c * Side_c - Side_a * Side_a;
Side_b = sqrt(Side_b);
Angle_a = Side_b / Side_c;
Angle_a = asin (Angle_a);
Angle_a = Angle_a * 57.2957795;
Angle_b = Side_a / Side_b;
Angle_b = atan (Angle_b);
Angle_b = Angle_b * 57.2957795;
cout << "\n For your triangle...\n Side A = ";
cout << Side_a;
cout << "\n Side B = ";
cout << Side_b;
cout << "\n Side C = ";
cout << Side_c;
cout << "\n Angle A = ";
cout << Angle_a;
cout << "\n Angle B = ";
cout << Angle_b;
BC110 : //Side B and Side C known - 110
Side_a = Side_c * Side_c - Side_b * Side_b;
Side_a = sqrt(Side_a);
Angle_a = Side_b / Side_c;
Angle_a = asin (Angle_a);
Angle_a = Angle_a * 57.2957795;
Angle_b = Side_a / Side_b;
Angle_b = atan (Angle_b);
Angle_b = Angle_b * 57.2957795;
cout << "\n For your triangle...\n Side A = ";
cout << Side_a;
cout << "\n Side B = ";
cout << Side_b;
cout << "\n Side C = ";
cout << Side_c;
cout << "\n Angle A = ";
cout << Angle_a;
cout << "\n Angle B = ";
cout << Angle_b;
Aa201 : //Side A and Angle A known - 201
Angle_b = 90 - Angle_a;
Side_c = Side_a / sin (Angle_b);
cout << "\n For your triangle...\n Side A = ";
cout << Side_a;
cout << "\n Side B = ";
cout << Side_b;
cout << "\n Side C = ";
cout << Side_c;
cout << "\n Angle A = ";
cout << Angle_a;
cout << "\n Angle B = ";
cout << Angle_b;
Ba210 : //Side B and Angle A known - 210
Ca300 : //Side C and Angle A known - 300
Ab501 : //Side A and Angle B known - 501
Bb510 : //Side B and Angle B known - 510
Cb600 : //Side C and Angle B known - 600
system ("PAUSE");
}
I think your problem come from the fact that the trig functions expects values in radians not degrees. ALthough your were using the value 57.2957795 earlier. You should make this a constdouble variable and refer to it in your code rather than use magic numbers like you have.
But the Elephant in the room is you r use of goto. Don't do it - it is really bad style. Investigate using functions and loops instead. I am not saying goto should be banned - there are situations where experts have a need to use them, but until you are an expert avoid using them at all costs.
Some other points to help you out:
Prefer double rather than float. The trig functions expect values that are double, and will convert them to double if they are not, and they return double as well. If you assign an answer to a float, you should get a loss of precision warning from the compiler. Make sure the warning level is set to it's highest, with g++, use -Wall -Wextra -pedantic
Choose the type of your variables carefully, IMO Trig_count and End should be unsignedint
If you do that, then this code:
Trig_Count = Trig_Count + 1;
can become
Trig_Count++;
and this one :
Trig_Count = Trig_Count + 500;
can be this:
Trig_Count += 500;
Instead of using Trig_Count the way you have, investigate using a switch statement instead. Each case can call the appropriate function. Put the whole thing in a while loop, call the function that displays the menu at the start of the loop, and have a default case to catch bad input. This will be a much cleaner overall solution.
Hope all is well at your end, and all are enjoying the festive season :+)
Be careful with the atan function - it returns angles less than 90 degrees, so this is a problem if you were expecting an answer in a different quadrant. Use atan2, which takes 2 arguments (x,y).