# trig calculator

• 02-17-2005
Mr.Pink
trig calculator
just finished my first program which is actually usefull to me.
It just figures out right angle triangles

Code:

```#include <stdio.h> #include <string> #include <iostream> #include <cmath> #include <algorithm> #include <fstream> using namespace std; void welcome(); float decision(string); float sides(float &, float &, float &, float &, float &); int main(int argc, char *argv[]) { welcome();   return 0; } void welcome(){ int menu; float valuea; float valueb; float valuec; float valueao; float valuebo; float valueco; cout << "Trig Calculator" << endl << endl; cout << "Select problem type\n1. Right angle triangle" << endl; cin >> menu; switch(menu){ case 1:  cout << "If you do not know the value use 0.\n\n";  valuea = decision("Do you know side A?\n");  valueb = decision("Do you know side B?\n");  valuec = decision("Do you know side C?\n");  valueao = decision("Do you know angle A?\n");  valuebo = decision("Do you know angle B?\n");  sides(valuea, valueb, valuec, valueao, valuebo);  cout << "\n\nTriangle calculated:\n\n";  cout << "Side A: " << valuea << endl;  cout << "Side B: " << valueb << endl;  cout << "Side C: " << valuec << endl;  cout << "Angle A: " << valueao << endl;  cout << "Angle B: " << valuebo << endl << endl;  welcome();  } } float decision(string question){ float value; float radian = 3.1415/180; cout << question; cin >> value; return value; } float sides(float &a, float &b, float &c, float &ao, float &bo){ float radian = 3.1415/180; if(a !=0 && b!=0 && c==0){ c = sqrt(a * a + b * b); } if(a !=0 && c!=0 && b==0){ b = sqrt(c * c - a * a); } if(a ==0 && c!=0 && b!=0){ a = sqrt(c * c - b * b); } //TAN with side A if(a!= 0 && b==0 && c==0){ if(ao !=0){ b = a/tan(ao*radian); if(bo == 0){ bo = 90-ao; } } if(bo !=0){ b = a*tan(bo*radian); if(ao == 0){ ao = 90-bo; } } c = sqrt((a*a)+(b*b)); } //tan with sideB if(a == 0 && b!=0 && c==0){ if(ao !=0){ a = b*tan(bo*radian); if(bo == 0){ bo = 90-ao; } } if(bo !=0){ a = b/tan(bo*radian); if(ao == 0){ ao = 90-bo; } } c = sqrt((a*a)+(b*b)); } //sin with sideC if(a == 0 && b==0 && c!=0){ if(ao !=0){ a = c*sin(ao*radian); if(bo == 0){ bo = 90-ao; } } if(bo !=0){ a = c*cos(bo*radian); if(ao == 0){ ao = 90-bo; } } b = sqrt((c*c)+(a*a)); } if(ao == 0 && bo ==0){ ao = atan(a/b)*180/3.141592654; bo = 90 - ao; } }```
keep in mind I started using c++ only a few days ago. Any sugestions?
• 02-17-2005
7stud
Well, this is your main() function:

Code:

```int main(int argc, char *argv[]) { welcome();   return 0; }```
which should be the main hub of activity, but it looks like a ghost town. I would get rid of your welcome() function and move all that code into main().

It looks like your switch statement has one case--that means it's just an if statement.

The recursive welcome() function call in your switch statement is not a very elegant construct. You should consider using a while loop that terminates when you enter a certain number.

If you can get more than 10 lines to compile when you are first starting--much less produce correct output-- you are way ahead of the game. :)
• 02-18-2005
Salem
You could try learning some indentation techniques while you're at it.
• 02-18-2005
major_small
Quote:

Originally Posted by Salem
You could try learning some indentation techniques while you're at it.

I second that notion.

edit: I indented your code for you, and all I have to say is, find a better way... use less variables, clean up your conditional statements, and get rid of the case in void welcome() (which should just be the code in main():
Code:

```void welcome() {     int menu;     float valuea;     float valueb;     float valuec;     float valueao;     float valuebo;     float valueco;         for(;;)         cout << "Trig Calculator" << endl << endl;         cout << "Select problem type\n1. Right angle triangle" << endl;         cin >> menu;                 if(menu==1)             break;                 cout << "If you do not know the value use 0.\n\n";         valuea = decision("Do you know side A?\n");         valueb = decision("Do you know side B?\n");         valuec = decision("Do you know side C?\n");         valueao = decision("Do you know angle A?\n");         valuebo = decision("Do you know angle B?\n");                 sides(valuea, valueb, valuec, valueao, valuebo);                 cout << "\n\nTriangle calculated:\n\n";         cout << "Side A: " << valuea << endl;         cout << "Side B: " << valueb << endl;         cout << "Side C: " << valuec << endl;         cout << "Angle A: " << valueao << endl;         cout << "Angle B: " << valuebo << endl << endl;     } }```
Code:

```#include <stdio.h> #include <string> #include <iostream> #include <cmath> #include <algorithm> #include <fstream> using namespace std; void welcome(); float decision(string); float sides(float &, float &, float &, float &, float &); int main(int argc, char *argv[]) {         welcome();         return 0; } void welcome() {     int menu;     float valuea;     float valueb;     float valuec;     float valueao;     float valuebo;     float valueco;         cout << "Trig Calculator" << endl << endl;     cout << "Select problem type\n1. Right angle triangle" << endl;     cin >> menu;         switch(menu)     {         case 1:             cout << "If you do not know the value use 0.\n\n";             valuea = decision("Do you know side A?\n");             valueb = decision("Do you know side B?\n");             valuec = decision("Do you know side C?\n");             valueao = decision("Do you know angle A?\n");             valuebo = decision("Do you know angle B?\n");             sides(valuea, valueb, valuec, valueao, valuebo);             cout << "\n\nTriangle calculated:\n\n";             cout << "Side A: " << valuea << endl;             cout << "Side B: " << valueb << endl;             cout << "Side C: " << valuec << endl;             cout << "Angle A: " << valueao << endl;             cout << "Angle B: " << valuebo << endl << endl;             welcome();     } } float decision(string question) {     float value;     float radian = 3.1415/180;         cout << question;     cin >> value;         return value; } float sides(float &a, float &b, float &c, float &ao, float &bo) {     float radian = 3.1415/180;     if(a !=0 && b!=0 && c==0)     {         c = sqrt(a * a + b * b);     }     if(a !=0 && c!=0 && b==0)     {         b = sqrt(c * c - a * a);     }     if(a ==0 && c!=0 && b!=0)     {     a = sqrt(c * c - b * b);     }     //TAN with side A     if(a!= 0 && b==0 && c==0)     {         if(ao !=0)         {             b = a/tan(ao*radian);             if(bo == 0)             {                 bo = 90-ao;             }         }         if(bo !=0)         {             b = a*tan(bo*radian);                 if(ao == 0)                 {                     ao = 90-bo;                 }         }         c = sqrt((a*a)+(b*b));     }     //tan with sideB     if(a == 0 && b!=0 && c==0)     {         if(ao !=0)         {             a = b*tan(bo*radian);             if(bo == 0)             {                 bo = 90-ao;             }         }         if(bo !=0)         {             a = b/tan(bo*radian);             if(ao == 0)             {                 ao = 90-bo;             }         }         c = sqrt((a*a)+(b*b));     }     //sin with sideC     if(a == 0 && b==0 && c!=0)     {         if(ao !=0)         {             a = c*sin(ao*radian);             if(bo == 0)             {                 bo = 90-ao;             }         }         if(bo !=0)         {             a = c*cos(bo*radian);             if(ao == 0)             {                 ao = 90-bo;             }         }         b = sqrt((c*c)+(a*a));     }     if(ao == 0 && bo ==0)     {         ao = atan(a/b)*180/3.141592654;         bo = 90 - ao;     } }```
I just noticed this:
Code:

`float radian = 3.1415/180;`
Code:

`ao = atan(a/b)*180/3.141592654;`
please, for the sake of sanity, consider using a constant here, a simple line of code would save you a world of confusion:
Code:

`const double pi=31.141592654;`
• 02-18-2005
Zach L.
Pi ~ 31, eh? ;)
• 02-18-2005
lol, i think he might've meant:

const double pi=3.141592654;

but if 31 is what he wanted.....
• 02-18-2005
major_small
yeah, I actually meant 31,415,926.54
• 02-19-2005
MipZhaP
Hmm... I see a problem if Side C == 0, and either Angle A or B == 0. Then it will never calculate Angle A or B!..
Or am I totally wrong?
• 02-19-2005
Mr.Pink
yea, but that's an unlikely situation. I wrote it mostly for when I'm doing physics stuff on the computer and don't want to use windows calc
• 02-20-2005
MipZhaP
Yes, it's very unlikely, but I just fought of it.
If you had to publish your work some day, and a person switched between the letters, so that the 90degree angle would be B instead of C. Then you would have a problem. But if it's for home use only, then it would do with the 3 if-sentences alone :)