Whats wrong with my code???

Basically I've been coding for about 3 weeks, I am creating a calculator so I can do a lot more than I can on a normal calculator. I haven't been working on it long and have made a lot of progress. The standard Calculations are working fine when i run them. I have worked my way through part of Trigonometry but I thought I'd try it out to see if it works. So I thought I'd try to Cos(63) which returned an answer of 0.985897. I then tried to type it in on a calculator to see if it returned the same result, but to my dismay it showed on the calculator, the answer to be 0.453990. can anyone see why it is wrong???

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>
#include <cmath>
#include <math.h>

using namespace std;

int main()

{

         double firstNumber = 0.0;
         double secondNumber = 0.0;
         char operation =' ';
         double asin(double x);

do
{
    int M;
     cout << "welcome to Calculator++." << endl;
     cout << " " << endl;
     cout << "Select Operation" << endl;
     cout << "1. Standard calculator operations" << endl;
     cout << "2. Trigonometry" << endl;
     cout << "3. Statistics" << endl;

    cin >> M;

     if ( M == 1 ){
        cout << " " << endl;
        cout << "1. Addition" << endl;
        cout << "2. Subtraction" << endl;
        cout << "3. Multiplication" << endl;
        cout << "4. Division" << endl;
        cout << "5. Square a number" << endl;
        cout << "6. Cube a number" << endl;
        cout << "7. Exponent's ( X^n)" << endl;
        cout << "8. Squareroot" << endl;
        cout << "9. Cuberoot" << endl;
        cout << "10. Log [log10(x)]" << endl;
        cout << "11. Finding the length of a hypotenuse" << endl;


        {
            int Op;

                cout << " " << endl;
                cout << "Enter first Number" << endl;
                cin>>firstNumber;
                cout << " " << endl;
                cout << "Enter Operation number (1 to 11)" << endl;
                cin>>Op;
                cout << " " << endl;
                cout << "Enter second Number (enter zero for operation 5, 6, 8, 9 & 10)" << endl;
                cin>>secondNumber;
                cout << " " << endl;

                if ( Op == 1){
                    cout << "the answer is..."<<firstNumber<<"+"<<secondNumber<<"="<<(firstNumber+secondNumber) << endl;
                }
                if ( Op == 2){
                    cout << "the answer is..."<<firstNumber<<"-"<<secondNumber<<"="<<(firstNumber-secondNumber) << endl;
                }
                if ( Op == 3){
                    cout << "the answer is..."<<firstNumber<<"*"<<secondNumber<<"="<<(firstNumber*secondNumber) << endl;
                }
                if ( Op == 4){
                    cout << "the answer is..."<<firstNumber<<"/"<<secondNumber<<"="<<(firstNumber/secondNumber) << endl;
                }
                if ( Op == 5){
                    cout << "the answer is..."<<firstNumber<<"*"<<firstNumber<<"="<<(firstNumber*firstNumber) << endl;
                }
                if ( Op == 6){
                    cout << "the answer is..."<<firstNumber<<"*"<<firstNumber<<"*"<<firstNumber<<"="<<(firstNumber*firstNumber*firstNumber) << endl;
                }
                if ( Op == 7){
                    cout << "the answer is..."<<std::pow(firstNumber, secondNumber) << endl;
                    break;
                }
                if ( Op == 8){
                    cout << "the answer is..."<<std::sqrt(firstNumber) << endl;
                    break;
                }
                if ( Op == 9){
                    cout << "the answer is..."<<std::cbrt(firstNumber) << endl;
                    break;
                }
                if ( Op == 10){
                    cout << "the answer is..."<<std::log10(firstNumber) << endl;
                    break;
                }
                if ( Op == 11){
                    cout << "the answer is..."<<std::hypot(firstNumber,secondNumber) << endl;
                    break;
                }
        return 0;
        }
     }

     if ( M == 2 ){
        cout << "1. Cos(angle)" << endl;
        cout << "2. Sin(angle)" << endl;
        cout << "3. Tan(angle)" << endl;
        cout << "4. Sine (find an angle)" << endl;
        cout << "5. Sine (find a length)" << endl;
        cout << "6. Cosine (find an angle)" << endl;
        cout << "7. Cosine (find a length)" << endl;
        cout << "8. Degrees to Raidians" << endl;
        cout << "9. Radians to Degrees" << endl;
        cout << "10. Arc Length" << endl;
        cout << "11. Area of a Sector" << endl;
        cout << "12. Area of a segment in a circle" << endl;
     }
     {
         int Op1;
         double angle;
         double angleA;
         double angleB;
         double angleC;
         int lengtha;
         int lengthb;
         int lengthc;
         const double PI = 3.1415926535897932385;


            cout << " " << endl;
            cout << "Enter Operation number (1 to 12)" << endl;
            cin>>Op1;
            cout << " " << endl;

            if ( Op1 == 1){
                cout << "Enter angle" << endl;
                cin>>angle;
                cout << " " << endl;
                cout << "the answer is..." <<cos(angle) << endl;
                break;
            }
            if ( Op1 == 2){
                cout << "Enter angle" << endl;
                cin>>angle;
                cout << " " << endl;
                cout << "the answer is..." <<sin(angle) << endl;
                break;
            }
            if ( Op1 == 3){
                cout << "Enter angle" << endl;
                cin>>angle;
                cout << " " << endl;
                cout << "the answer is..." <<tan(angle) << endl;
                break;
            }
            if ( Op1 == 4){
                cout << "Enter angle A" << endl;
                cin>>angleA;
                cout << " " << endl;
                cout << "Enter length a" << endl;
                cin>>lengtha;
                cout << " " << endl;
                cout << "Enter length b" << endl;
                cin>>lengthb;
                cout << " " << endl;
                cout << "Angle B is " << (180.0/PI)*asin(sin(angleA*PI/180.0) * lengthb / lengtha) << endl;
                break;
            }



     }

     if ( M == 3 ){
        cout << "1. " << endl; //*

        cout << "2. " << endl;
        cout << "3. " << endl;
        cout << "4. " << endl;
        cout << "5. " << endl;
        cout << "6. " << endl;
        cout << "7. " << endl;

        return 0;
     }
} while (true);

}
ive put this in:

1
2
3
double cos (      double x );
float cos (       float x );
long double cos ( long double x );


but still no luck
By default the built-in trig functions work in radians. If you want to use degrees, first convert the user input from degrees to radians before calling the cos() function.

You should not need to declare the functions yourself, they are already declared in the <cmath> header.

Last edited on

By default the built-in trig functions work in radians. If you want to use degrees, first convert the user input from degrees to radians before calling the cos() function.

You should not need to declare the functions yourself, they are already declared in the <cmath> header.



Thank you so much, i've got it working now
Topic archived. No new replies allowed.