Calculator script help

So I'm making a "calculator" in c++ for class.
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
#include <iostream>
#include <cmath>


using namespace std;

#define PI 3.14159265

int main()
{

    
    double X, Y;
    double resultPow, resultSin, resultCos;
    char operation; 
    
       cout << "**********************************" << endl;
   cout << "*   1 - Add                      *" << endl;
   cout << "*   2 - Subtract                 *" << endl;
   cout << "*   3 - Multiply                 *" << endl;
   cout << "*   4 - Divide                   *" << endl;
   cout << "*   5 - Raise X to the power Y   *" << endl;
   cout << "*   6 - Sine ( X )               *" << endl;
   cout << "*   7 - Cosine ( X )             *" << endl;
   cout << "*                                *" << endl;
   cout << "*   0 - Quit                     *" << endl;
   cout << "**********************************" << endl;
	     
    cout << "\n ENTER FIRST NUMBER  : ";
    cin  >> X;

    cout << "\n ENTER Option Number : ";
    cin  >> operation;
   
    
    
    resultSin = sin (X*PI/180);
    
    resultCos = cos (X*PI/180);
   


      
         switch(operation)
         {
                          case '1':
                          cout << "\n ENTER SECOND NUMBER : ";
                          cin  >> Y;
                          cout << "\n The Result is " << "#### " << (X + Y) << " ####" << endl;
                          break;
                          
                          case '2':
                          cout << "\n ENTER SECOND NUMBER : ";
                          cin  >> Y;
                          cout << "\n The Result is " << "#### " << (X - Y) << " ####" << endl;
                          break;
                          
                          case '3':
                          cout << "\n ENTER SECOND NUMBER : ";
                          cin  >> Y;
                          cout << "\n The Result is " << "#### " << (X * Y) << " ####" << endl;
                          break;
                          
                          case '4':
                          cout << "\n ENTER SECOND NUMBER : ";
                          cin  >> Y;
                          cout << "\n The Result is " << "#### " << (X / Y) << " ####" << endl;
                          break;
                          
                          case '5':
								  cout << "\n ENTER SECOND NUMBER : ";
                          cin  >> Y;
         resultPow = pow (X,Y);                
								  cout << "\n ANSWER = " << "#### " << resultPow << " ####" << endl;
                          break;
                          
                          case '6':
                          cout << "\n ANSWER = " << "#### " << resultSin << " ####" << endl;
                          break;
                          
                          case '7':
                          cout << "\n ANSWER = " << "#### " << resultCos << " ####" << endl;
                          break;
                                                    
                          case '0':
								  cout<< " are you sure you want to quit?" << 
								  cin.get();
                          }
                          return 0;
}                        


1. when i try the exponent option (5), it always gives me an answer of infinty.
2. I gotta do a while loop to keep it from closing without the user choosing the option to
3. Those "####"'s are supposed to be there. :)
Last edited on
1. it is because resuly pow is calculated after you input 'X' and 'Y' is a garbage memory address and then 'Y' is changed after and the calculation is not done again in the case 5.

you need to move the resultPow = Pow ( x,y) before the cout << ANSWER.... etc

2. on case 0;
add a
1
2
3
4
5
cout<< " are you sure you want to quit?" << 
cin.get();
}
return 0;
}


this should wait for user to input enter
now the power works, but it still exits after the operation.

I updated the code at the top.
Last edited on
What is this?

 
#define PI 3.14159265 

Macros?...MMM......MMMMAAAACCRROOOSSSSS?????


Please, use global variables for things like that:

 
const double PI = 3.14159265


Also, at the end of your program, you can do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
char play_again = 'y';
do
{
[code here]
cout << "Would you like to play again? (y or n)\n";
cin >> play_again;
if(play again = 'y')
{
continue;
}
else
{
break;
}
}while(play_again = 'y')

I don't see why you'd need the ####'s.

Happy coding.
Topic archived. No new replies allowed.