Problem With Calculator

in my C++ class we have to write a code for a calculator which will do all of the math functions on this page here: http://www.cplusplus.com/reference/clibrary/cmath/
im having problems getting my program to do sine and cosine. I dan't get it to fit into the code right.. any suggestions?
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
#include <iostream>
#include <math.h>
#include <stdio.h>
#define PI 3.14159265

using namespace std;

int main()
{
	int x;
	char y;
	int z;
	int iAnswer;
    double param, result;

	cout << "Welcome to Cal-cu-la-tor! The devil of all calculators!" << endl;
	cout << "Maths Symbols" << endl;
	cout << "Add : +" << endl;
	cout << "Subtract : -" << endl;
	cout << "Multiply : *" << endl;
	cout << "Divide : /" << endl;
	cout << "Cos : c" << endl;
	cout << "Sin : s" << endl;

	cout << "Please enter you equation: \n" << endl;
	cin >> x;
	cin >> y;
	if( y = 'c','s')
	{
	   switch (y)
	   {
          case 'c':
            iAnswer = cos (param*PI/180);
            printf ("The cosine of %lf degrees is %lf.\n", param, result );
            break;
          case 's':
            cout << "there is no code written for this yet" << endl;
            break;
	   }
	}
	else
	{
	   	cin >> z;
	}

	switch (y)
    {
        case '+':
            iAnswer = x+z;
            break;
        case '-':
            iAnswer = x-z;
            break;
        case '*':
            iAnswer = x*z;
            break;
        case '/':
            iAnswer = x/z;
            break;





    }


    cout << "The answer to your equation is: " << iAnswer << endl;
    cout << "This ends you use of Cal-cu-la-tor! he is displeased!" << endl;

return 0;

}

Did you mean,

if( (y == 'c') || (y=='s'))

Also, you should initialize iAnswer or make sure you put something in the 's' case to assign a value to it. Since it is possible that the user will call sin or cos, why not make iAnswer a double?

Last edited on
what do you mean a double?
As cppmatt suggested you should make iAnswer a double (the variable type which param is set to). If you do not iAnswer gets truncated (rounded down to nearest integer). Thats why your calculator always produces o.oooo as an answer.

Your case 'c': should look like this

case 'c':
param = x;
iAnswer = cos (param*PI/180);
printf ("The cosine of %lf degrees is %lf.\n", param, iAnswer );
break;

1. You have to set param equal to the input (param = x) otherwise you are multiplying
(undefined param)*PI/180 which equals no useful value.
2. You have to print out iAnswer since your result variable is never set a value.
i now have a new problem. my teacher reviewed my code and pointed out some things i needed to fix so i had to rewrite my code (new code as follows) though when i try to find the cosine of a number, it says its 0.0000. how do i fix 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
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
#include <iostream>
#include <math.h>
#include <stdio.h>
#define PI 3.14159265

using namespace std;

int main()
{
	int iOne;
	char cSym;
	int iTwo;
	int iCalc;
	int iAnswer;
    double param, result;

	cout << "Welcome to Cal-cu-la-tor! The devil of all calculators!" << endl;
	cout << "\n" << "If your equation you need solved is just using basic functions '1'. \n " << endl;
	cout << "If you need to do Scientific calculations, enter '2'. \n" << endl;
	cin >> iCalc;
	if (iCalc == 1)
	{
	    cout << "To enter you basic math equation using additon, subtraction, division, or" << endl;
	    cout << " multiplication, enter the number, the symbol, and then the final number." << endl;
	    cin >> iOne;
        cin >> cSym;
        cin >> iTwo;

        switch (cSym)
        {
            case '+':
                iAnswer = iOne+iTwo;
                break;
            case '-':
                iAnswer = iOne-iTwo;
                break;
            case '*':
                iAnswer = iOne*iTwo;
                break;
            case '/':
                iAnswer = iOne/iTwo;
                break;
        }
	}
    else ;
    {
        cout << "You have chosen the Scientific calculator." << endl;
        cout << "To find the cosine of a number, press 'c' then your number, then enter" << endl;
        cout << "To find the sine of a number, press 's' then your number, then enter" << endl;
        cin >> cSym;
        cin >> iOne;
        switch (cSym)
        {
            case 'c':
                param = iOne;
                iAnswer = cos (param*PI/180);
                printf ("The cosine of %lf degrees is %lf.\n", param, iAnswer );
                break;
            case 's':
            cout << "there is nothing we can do about this" << endl;
            break;
        }
    }


   /* cout << "The answer to your equation is: " << iAnswer << endl;
    cout << "This ends your use of Cal-cu-la-tor! He is displeased!" << endl;*/

return 0;

}
You are doing the exact same thing that you were told about in two previous posts!

You are using iAnswer as an int, when it should be a double.
Topic archived. No new replies allowed.