TRIGONOMETRY CALCULATOR PROBLEM

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.

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
 
#include <iostream>
#include <string>
#include <math.h>
#include <cmath>

using namespace 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");

}
Hi ,

Welcome to cplusplus :+D

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 const double 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 unsigned int

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).
> The trig functions expect values that are double
that's true, for C.
In C++ there are overloads.


You can use compound assignment operators with floating types.
You can use increment and decrement operators with floating types.
Topic archived. No new replies allowed.