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?
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?
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?
#include <iostream>
#include <math.h>
#include <stdio.h>
#define PI 3.14159265
usingnamespace 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;
}