# How to convert angle degrees into radians

Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last
• 10-01-2007
SVXX
How to convert angle degrees into radians
Guys...I was adding more parts to that equation solver program to make it a calculator of sorts...I need to know how to change degrees into radians..some help please?
• 10-01-2007
Salem
• 10-01-2007
matsp

This is the third or so on the list of MANY that come back if you enter "degrees to radians" into google.

http://www.teacherschoice.com.au/Mat...les/Angles.htm

--
Mats
• 10-01-2007
SVXX
Code:

``` cout << "Enter angle in (d)degrees or (r)radians? - ";                     if (r == 'd')                     {                           cout << "Enter angle theta (degrees) -";                           cin >> degrees;                           a = (degrees * 180)/pi;                     }                     else if (r == 'r')                     {                           cout << "Enter angle theta(radians) -";                           cin >> radians;                           a == radians;                     }```
This then? And yeah I just checked google before Salem replied lol sorry...I'll be careful in future to use the forum as a last resort. And the "r" variable is a char variable I already declared in the Class.(as are degrees,radians and pi..)
• 10-01-2007
matsp

--
Mats
• 10-01-2007
SVXX
Its the same one I used o.O damn I made a mistake
• 10-01-2007
SVXX
It isn't working...Let me post the problem -:

This is the first part.
Code:

```class Program {       private:               float a,x,b,c,D,d,f; //Teh math vars.               int n; //You guessed it....ANOTHER math variable.               long double radians, degrees, Sine, Tangent, Cosine, Arctan, Arcsine, Arccos; //Trigonometry.               char g[1]; //For the trigo section.                                                                 public:               void Case1(); //All of               void Case2(); //these               void Case3(); //are               void Case4(); //the               void Case5(); //mathematical               void Case6(); //operations.               void Trigonometry();// Trigonometrics.               void EXIT();  //The exit function.               void RorQ();  //The restart or quit function.               int exit;    //The exit value.               int exitRorQ; //The restart or quit value.               char rq[1];  // "r" or "q"?(char size 1)                             };```
The Trigonometrics Part
Code:

```void Program::Trigonometry() {                     double pi = 3.1416; //Again for trigo.                     if (g == "M")                     {                           setcolor(7);                           cout << "Enter angle theta (degrees) -";                           cin >> degrees;                           a = (degrees * pi)/180;                           Sine = sin (a);                           Cosine = cos (a);                           Tangent = tan (a);                           Arctan = atan (a);                           Arccos = acos (a);                           Arcsine = asin (a);                           cout << "\nThe trigonometric ratiosare as follows -:\n";                           setcolor(14);                           cout << "Sin theta = " << Sine << endl;                           cout << "Cos theta = " << Cosine << endl;                           cout << "Tan theta = " << Tangent << endl;                           cout << "Inverse Sin(Arcsine) theta = " << Arcsine << endl;                           cout << "Inverse Cos(Arccos) theta = " << Arccos << endl;                           cout << "Inverse Tan(Arctan) theta = " << Arctan << endl;                     }                     else if (g == "N")                     {                           setcolor(7);                           cout << "Enter angle theta(radians) -";                           cin >> radians;                           a == radians;                           Sine = sin (a);                           Cosine = cos (a);                           Tangent = tan (a);                           Arctan = atan (a);                           Arccos = acos (a);                           Arcsine = asin (a);                           cout << "\nThe trigonometric ratiosare as follows -:\n";                           setcolor(14);                           cout << "Sin theta = " << Sine << endl;                           cout << "Cos theta = " << Cosine << endl;                           cout << "Tan theta = " << Tangent << endl;                           cout << "Inverse Sin(Arcsine) theta = " << Arcsine << endl;                           cout << "Inverse Cos(Arccos) theta = " << Arccos << endl;                           cout << "Inverse Tan(Arctan) theta = " << Arctan << endl;                     } }```
And the Case6 part
Code:

```void Program::Case6() {                     cout << "Enter angle in (M)degrees or (N)radians? - ";                     cin >> g;                     Trigonometry();                     setcolor(7);                     Sleep(3000); }```
The output after cout << "Enter angle"..... isn't coming..it just skips the if else statements and goes straight to the getch(); in main..
• 10-01-2007
mike_g
You could declare the multiplier as a constant:
Code:

`const DEG_TO_RAD 0.0174532925;`
Then use call it in you program like:
Code:

`radians = degrees * DEG_TO_RAD;`
It saves some extra calculations.
• 10-01-2007
matsp
Quote:

Originally Posted by SVXX
It isn't working...Let me post the problem -:

So, I suspect you need a "cin.ignore()" after each cin >> somedouble, because cin >> somechar will otherwise get your "newline" that is left in the buffer after you entered the double number.

--
Mats
• 10-01-2007
SVXX
Code:

```setcolor(7);                           cout << "Enter angle theta (degrees) -";                           cin >> degrees;                           cin.ignore();```
Code:

```setcolor(7);                           cout << "Enter angle theta(radians) -";                           cin >> radians;                           cin.ignore();```
These?
• 10-01-2007
matsp
Yes, in all places where you read in numbers.

Edit: Of course, if you do two reads in the same function, you can put just one ignore after the second "cin >> number;", because the newline left in the buffer will be "eaten" by the second "cin >> number;" - but there will be a newline left in the buffer.

You can also use the "flush the input buffer" method(s) described in the FAQ section (top left of this page).

--
Mats
• 10-01-2007
SVXX
All the other functions are working...EXCEPT Trigonometry..Trigonometry(Case6) wasn't working even before I entered the cin.ignore()s...and the others were working anyway.
• 10-01-2007
SVXX
where exactly must I implement that code? In the main()?
Code:

```int main() //Muhuhahahhahaha {     Program OMGDOIT;     int y;     C.exit = 1;     int ch;          //Clearing input buffers.     char buf[BUFSIZ];//Same.     while(C.exit == 1)     {             cout << "\nWelcome to the Equation Solver!\n";         cout << "\nChoose one of the following -:\n";                cout << "\n1. Find a quadratic equation's nature of roots\n";         cout << "\n2. Find a quadratic equation's roots\n";         cout << "\n3. Establish the Vičte relation of the roots and coefficients\n";         cout << "\n4. Find a term of an arithmetic progression(AP)\n";         cout << "\n5. Find the sum of the terms of an AP\n";         cout << "\n6. Find the trigonometric values of an angle\n";         cout << "\n7. Quit,noobs >_>\n";         cin >> y;         cin.ignore();                 switch(y)         {                          case 1:                       OMGDOIT.Case1();                       getch();                       OMGDOIT.RorQ();                       break;                   case 2:                       OMGDOIT.Case2();                       getch();                       OMGDOIT.RorQ();                       break;                   case 3:                       OMGDOIT.Case3();                       getch();                       OMGDOIT.RorQ();                       break;                   case 4:                       OMGDOIT.Case4();                       getch();                       OMGDOIT.RorQ();                       break;                   case 5:                       OMGDOIT.Case5();                       getch();                       OMGDOIT.RorQ();                       break;                    case 6:                       cout.flush();                       OMGDOIT.Case6();                       getch();                       OMGDOIT.RorQ();                       break;                   case 7:                       C.exit = 0;                        break;                    default:                       cout << "Enter a number in the list\n";                       break;         }         clrscr(); //Clears screen maybe?         }                       return 0; }```
• 10-01-2007
matsp
Ehm, perhaps the problem is something else:
Code:

`g == "M"`
This compares the address of g with the address of the literal "M". You probably don't want that (as it's always false with the current piece of code).

Code:

`g[0] == 'M';`
would compare the first letter in g with the letter 'M', which is more likely to be what you actually want to compare.

Also:
Code:

```                          Sine = sin (a);                           Cosine = cos (a);                           Tangent = tan (a);                           Arctan = atan (a);                           Arccos = acos (a);                           Arcsine = asin (a);                           cout << "\nThe trigonometric ratiosare as follows -:\n";                           setcolor(14);                           cout << "Sin theta = " << Sine << endl;                           cout << "Cos theta = " << Cosine << endl;                           cout << "Tan theta = " << Tangent << endl;                           cout << "Inverse Sin(Arcsine) theta = " << Arcsine << endl;                           cout << "Inverse Cos(Arccos) theta = " << Arccos << endl;                           cout << "Inverse Tan(Arctan) theta = " << Arctan << endl;```
is duplicated. You only need it once.

However, Isn't atan etc supposed to be multiplied by the conversion back to degrees as well?

You still only need one piece of code to display things, and to do most of the caclulations.

Duplicating code is a sure way to "miss" changing something on one side of the else that needs to be changed on both sides. It is a good habit to get into only writing as little as possible inside if/else statements, and keeping any code that is common outside the if/else.

You could even do the whole calculation of angle from radians to degrees or keep it radians by having a variable "conv_factor" that is set to either 1.0 or the conversion factor, and always multiply by this factor when producing the result. Multiplying by 1.0 is of course a "no change".

--
Mats
• 10-01-2007
SVXX
Hmm I see...let me try this...
Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last