Jun 12, 2013 at 10:29pm Jun 12, 2013 at 10:29pm UTC
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 Jun 12, 2013 at 11:08pm Jun 12, 2013 at 11:08pm UTC
Jun 12, 2013 at 10:53pm Jun 12, 2013 at 10:53pm UTC
The code you showed shall not be compiled. So iat least try to write code that is compiled without errors.
Jun 12, 2013 at 11:05pm Jun 12, 2013 at 11:05pm UTC
do not use do-while loops, you need a normal while loop for this problem.
Jun 12, 2013 at 11:08pm Jun 12, 2013 at 11:08pm UTC
sorry, i forgot one of the do{
Jun 12, 2013 at 11:14pm Jun 12, 2013 at 11:14pm UTC
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 );
Jun 12, 2013 at 11:16pm Jun 12, 2013 at 11:16pm UTC
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 Jun 12, 2013 at 11:19pm Jun 12, 2013 at 11:19pm UTC
Jun 12, 2013 at 11:26pm Jun 12, 2013 at 11:26pm UTC
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 Jun 12, 2013 at 11:27pm Jun 12, 2013 at 11:27pm UTC
Jun 12, 2013 at 11:47pm Jun 12, 2013 at 11:47pm UTC
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.