Thread: How to convert angle degrees into radians

  1. #16
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    Mat...I tried this,but it returns wrong values -:
    Code:
    void Program::Trigonometry()
    {
                        double pi = 3.1416; //Again for trigo.
                        if (g[0] == 'M' || 'm')
                         {
                               setcolor(7);
                               cout << "Enter angle theta (degrees) -";
                               cin >> degrees;
                               a == degrees * (pi/180);
                               Sine = sin (a * (180/pi));
                               Cosine = cos (a * (180/pi)) ;
                               Tangent = tan (a * (180/pi));
                               Arctan = atan (a * (180/pi));
                               Arccos = acos (a * (180/pi));
                               Arcsine = asin (a * (180/pi));
                               
                         }
                         else if (g[0] == 'N' || 'n')
                         { 
                               setcolor(7);
                               cout << "Enter angle theta(radians) -";
                               cin >> radians;
                               cin.ignore(); 
                               a == radians;
                               Sine = sin (a);
                               Cosine = cos (a);
                               Tangent = tan (a);
                               Arctan = atan (a);
                               Arccos = acos (a);
                               Arcsine = asin (a);
                         }
                               cout << "\nThe trigonometric ratios are 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;
    }
    I try any angle,'cos' always comes 1,and the rest the same garbage.

  2. #17
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    a == degrees * (pi/180);
    Guess you want
    Code:
    a = degrees * (pi/180);
    Kurt
    EDIT:
    same here
    Code:
    a == radians;

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > else if (g[0] == 'N' || 'n')
    I thought we explained this one already in another thread.

    else if (g[0] == 'N' || g[0] == 'n' )

    Or maybe
    else if ( toupper(g[0]) == 'N' )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    The trig functions take arguments in radians. You've correctly converted a from degrees to radians, then incorrectly converted it right back to degrees inside the arguments to each of the trig functions.

  5. #20
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    I was thinking pretty much the same thing...it accepts arguements in radians. How do I make it accept args in degrees? Then I'd convert degrees to radians,feed it to the computer and get an output based on that. And Salem that if else works anyway :P

  6. #21
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > And Salem that if else works anyway :P
    Oh I'm sure it compiles, and perhaps through some random chance you occasionally see what you expect to see.
    But for sure it is NOT the way to compare a variable with an upper case or lower case letter.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #22
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    Hmm k I see,I'll implement what you said...but what about the accepting args in degrees part..some help on that please

  8. #23
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by SVXX View Post
    Hmm k I see,I'll implement what you said...but what about the accepting args in degrees part..some help on that please
    You don't give args in degrees, you give them in radians (why did you convert the angle from degrees to radians if you didn't think you were supposed to use it?). For example:
    Code:
    std::cin >> degrees;
    a = degrees * (pi/180); // a is angle in radians
    Sine = sin(a);  // this works properly now

  9. #24
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    if (g[0] == 'N' || 'n')
    If we split this into parts, we get if (g[0] == 'N') or ('n' != 0) - well, 'n' is not zero in any alphabet representation that I've even heard the name of, so this will ALWAYS be true.

    Yes, it compiles - the compiler may give you warnings if you enable -Wall to say "this is always true", but perhaps not.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #25
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    Well..I changed the stuff and added a separate Radians Degrees Conversion part instead
    Code:
    void Program::Case6()
    {
                               cout << "This is the radians-degrees/degrees-radians converter\n";
                               cout << "Choose - (M)Radians -> Degrees, (N)Degrees -> Radians\n";
                               cin >> g;
                               cin.ignore();
                               setcolor(13);
                               if (g[0] == 'M' ||g[0] == 'm')
                               {
                                       cout << "Enter radians - ";
                                       cin >> radians;
                                       degrees = radians * (180.0 / M_PI);
                                       cout << "Radians -> Degrees says " << degrees << " degrees" << endl;
                               }
                               else if (g[0] == 'N' ||g[0] == 'n')
                               {
                                       cout << "Enter degrees - ";
                                       cin >> degrees;
                                       radians = degrees * (M_PI / 180.0);
                                       cout << "Degrees -> Radians says " << radians << " radians" << endl;
                               }
                               setcolor(7);
    }
    But now I have problems with the program termination and case 7(trigonometry)
    This is the program restart or quit code
    Code:
    void Program::RorQ() //Gives user the option to restart or quit
    {
    	C.exitRorQ = 1;
    	while(C.exitRorQ == 1)
    	{
    	clrscr();
    	cout << "(r) restart or (q) quit" <<endl;
    	cout << ">";
    			cin  >> C.rq[1];
    
    			if (C.rq[1] == 'r')
    					C.exitRorQ = 0;
    			else if (C.rq[1] == 'q')
    			{
    				C.exitRorQ = 0;
    				EXIT();
    			}
    			else
    				{
                    cout << "Thats not a choice... Please rechoose" << endl;
                    }
    	}
    }
    And this is the Case7
    Code:
    void Program::Case7()
    {
                         setcolor(12);
                         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 << "\nSine theta is " << Sine;
                         cout << "\nCos theta is " << Cosine;
                         cout << "\nTangent theta is " << Tangent;
                         cout << "\nInverse Sine(Arcsine) theta is " << Arcsine;
                         cout << "\nInverse Cos(Arccos) theta is " << Arccos;
                         cout << "\nInverse Tan(Arctan) theta is " << Arctan;
                         cout << "\n";
                         setcolor(7);
                         Sleep(3000);
    }
    .....
    .....
    case 7: 
                          OMGDOIT.Case7();
                          OMGDOIT.TerminationPrevention();
                          OMGDOIT.RorQ();      
                          break;
    The restart or quit option works on all cases BUT this..If I try twice both r and q quit the program...

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What is the code after the break on
    Code:
    case 7: ...
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #27
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    Code:
    case 1:
                          OMGDOIT.Case1();
                          OMGDOIT.TerminationPrevention();
                          OMGDOIT.RorQ();
                          break;
                      case 2:
                          OMGDOIT.Case2();
                          OMGDOIT.TerminationPrevention();
                          OMGDOIT.RorQ();
                          break;
                      case 3:
                          OMGDOIT.Case3();
                          OMGDOIT.TerminationPrevention();
                          OMGDOIT.RorQ();
                          break;
                      case 4: 
                          OMGDOIT.Case4();
                          OMGDOIT.TerminationPrevention();
                          OMGDOIT.RorQ();
                          break;
                      case 5:
                          OMGDOIT.Case5(); 
                          OMGDOIT.TerminationPrevention();
                          OMGDOIT.RorQ();
                          break;  
                      case 6:
                          OMGDOIT.Case6();
                          OMGDOIT.TerminationPrevention();
                          OMGDOIT.RorQ();
                          break;
                      case 7: 
                          OMGDOIT.Case7();
                          OMGDOIT.TerminationPrevention();
                          OMGDOIT.RorQ();      
                      case 8:
                           C.exit = 0;  
                           break;  
                      default: 
                          cout << "Enter a number in the list\n";
                          break;

  13. #28
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And what happens after the end of the switch?

    [I'd also say that you don't need to have exitRorQ as a class member (or member of struct - not sure which it is from the context I can see) - it would make more sense to have that as a local variable - same applies to rq.]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #29
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    Code:
             default: 
                          cout << "Enter a number in the list\n";
                          break;
            }
            clrscr(); 
            }
                  
            return 0;
    }
    This is what happens

  15. #30
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And I take it that after clrscr, the brace is for some kind of while/for-loop - what is the condition that exits the loop?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert DegMinSec to Decimal Degrees
    By JacquesLeJock in forum C Programming
    Replies: 3
    Last Post: 11-21-2007, 11:59 PM
  2. Temperature conversion...
    By Onslaught in forum C Programming
    Replies: 3
    Last Post: 10-21-2005, 01:15 PM
  3. sin() and cos() that use degrees
    By dwks in forum C Programming
    Replies: 3
    Last Post: 05-14-2005, 04:29 PM
  4. Convert Char to Int Function
    By drdroid in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2003, 12:53 PM
  5. Is this right
    By Granger9 in forum C Programming
    Replies: 6
    Last Post: 08-14-2002, 02:21 AM