Trigonometry

Hi, I am trying to make a trig calculator.
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
#include<iostream>
#include<cmath>
using namespace std;
int main (){
cout<<"Hello, welcome to xxxx trig. calculator"<<endl;
cout<<"enter 1 for tan, enter 2 for sin, enter 3 for cos"<<endl;
	int select;
	cin>>select;
	
	do{	double a;
		cout<<"length of one side"<<endl;
		cin>>a;
		double b;
		cout<<"enter angle"<<endl;
		cin>>b;
		double radian=(b*3.14159/180);
		double unknownS=tan(radian)*a;
		cout<<"unknown length"<<unknownS<<endl;
	}
	while(select=='1');
	 
	do{
		double a;
		cout<<"length of one side"<<endl;
		cin>>a;
		double b;
		cout<<"enter angle"<<endl;
		cin>>b;
		double radian=(b*3.14159/180);
		double unknownS=sin(radian)*a;
		cout<<"unknown length"<<unknownS<<endl;
	}

	while(select=='2');

	do{
		double a;
		cout<<"length of one side"<<endl;
		cin>>a;
		double b;
		cout<<"enter angle"<<endl;
		cin>>b;
		double radian=(b*3.14159/180);
		double unknownS=cos(radian)*a;
		cout<<"unknown length"<<unknownS<<endl;
	}
	while(select=='3');
return 0;
}

My problem is that I want the user to choose sin, tan or cos by entering 1,2, or 3, but my code isn't allowing me to do that. It is outputting everything. I am wondering if there is a way to fix this problem. Thanks very much for your time.
Last edited on
The code you showed shall not be compiled. So iat least try to write code that is compiled without errors.
do not use do-while loops, you need a normal while loop for this problem.
sorry, i forgot one of the do{
The condition in the do-while loop is incorrect

while(select=='1');

You are entering an integer number form 1 to 3 inclusively but trying to compare it with character '1'.

I think you meant

while( select < 1 || select > 3 );
Going by the instuctions you display

enter 1 for tan, enter 2 for sin, enter 3 for cos

it looks like you to calculate a tangent value, if the user enters 1; calculate a sine value, if the user enters 2; and calculate a cosine value, if the user enters 3.

Also, as select is an int, this

select=='1'

is comparing select against the ascii code value for the character '1', which is 49 in decimal.

Andy

Last edited on
try this:
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
#include<iostream>
#include<cmath>
using namespace std;
int main (){
cout<<"Hello, welcome to xxxx trig. calculator"<<endl;
cout<<"enter 1 for tan, enter 2 for sin, enter 3 for cos"<<endl;
	char select;
	cin>>select;
	
	while(select=='1'){
		double a;
		cout<<"length of one side"<<endl;
		cin>>a;
		double b;
		cout<<"enter angle"<<endl;
		cin>>b;
		double radian=(b*3.14159/180);
		double unknownS=tan(radian)*a;
		cout<<"unknown length"<<unknownS<<endl;
	}
		 
	while(select=='2'){
		double a;
		cout<<"length of one side"<<endl;
		cin>>a;
		double b;
		cout<<"enter angle"<<endl;
		cin>>b;
		double radian=(b*3.14159/180);
		double unknownS=sin(radian)*a;
		cout<<"unknown length"<<unknownS<<endl;
	}


	while(select=='3'){
		double a;
		cout<<"length of one side"<<endl;
		cin>>a;
		double b;
		cout<<"enter angle"<<endl;
		cin>>b;
		double radian=(b*3.14159/180);
		double unknownS=cos(radian)*a;
		cout<<"unknown length"<<unknownS<<endl;
	}
return 0;
}


anyway, this still is a lame way to keep the choice repeating.
i didn't check your maths in this program, that's your job.

edit: i changed select from int to char
Last edited on
@Rechard3


Your proposed code is invalid. Please before posting read what others already wrote.
@vlad from moscow:
Please before posting read what others already wrote.


i did read the other posts, i still don't know which one you're talking about !!!
ok vlad from moscow, i'm now looking at the date you posted your comment, and the date i edited mine (the one right before it).

looks like you commented right before i submitted my edit.
i think you didn't read the edit.
anyway, after i read yours, i went to my C++ compiler and pasted that code in it, and tried the program myself.
it works like charm.

don't take it hard on me, i felt like my first contribution was discarded, that's why i posted the second.
Topic archived. No new replies allowed.