Calculator program

Hey Everyone,

I need some help with a calculator program. I am not too good with programming so I am having so many difficulties with this calculator program. I have started it and solved a few minor problems on my own, but I still couldn't solve the following:

1-some operations were not functioning right or sometimes the compiler won't recognize them and give me an error.
2- when I tried multiplying a big number by another big one - both positive numbers- it gave me back a negative value.
3- I am trying to make the program quit by typing 'q' at anytime, but that does not seem to be working.

here is my code:

// Oct 22, 2011

// Calculator Program

#include <iostream> // for cout, endl;

using namespace std;

int main()

{
char op;
do
{
int x, y;
cout << ("Welcome to Calculator")<< endl;

cout << "Enter an operand: " << endl;

cin>> x;

cout << "Enter an operation: " << endl;

cin >> op;

//Select the operation ('Q' to quit);
//+, -, *, /, !,^, L, > !;
cout << "Enter an operand: " << endl;

cin >> y;

cout << "Enter Q to quit" << endl;

switch (op)
{
case '+':
cout << " = :" << x + y << endl;
break;
case '-':
cout << " = :" << x - y << endl;
break;
case '*':
cout << " = :" << x * y << endl;
break;
case '/':
cout << " = :" << x / y << endl;
break;
case '%':
cout << " = :" << x % y << endl;
break;
default: cout << " Invalid operator! " << endl;
}

cout << " Thank you for using calculator program ";
cin >> op;
}
while (op == 'y');
return 0;


system ("pause");
return EXIT_SUCCESS;

}

I hope someone can help me with this code to make it better.
Thank you-
The issue of multiplying two numbers together is purely based on binary calculations, If you are using a int plainly it assumes the integer is signed where the first bit in the number is a sign flag. so if you do two multiplications or additions when you roll past say 31^2 to 32^2, assuming a 32 bit integer, the sign flag is set and the number results in an negative. If you want to avert this problem you make the integer unsigned by adding the keyword unsigned before the int.

One thing I note about your program is it doesn't check for 0 for y when doing a divide. Divide by 0 is undefined.

I would structure program as followed:
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

// Oct 22, 2011

// Calculator Program

#include <iostream> // for cout, endl;

using namespace std;

int main()
{
      char op;
      int x = 0;
      int y = 0;
      int answer =0;
      bool done = false;
      bool error = false;

     do
     { 
           cout << "Welcome to Calculator" << endl; 

           cout << "enter the left side of the equasion: " << endl;
           cin >> x;

           cout >> "Enter the right side of the equasion:  " << endl;
           cin >> y;

           cout << "Enter an operand: (Q - to Quit)" << endl;
           cin>> op;

           //Select the operation ('Q' to quit);
          //+, -, *, /, !,^, L, > !;

          switch (op)
          {
              case '+':
                     answer  =x + y;
                     break; 
              case '-':
                     answer =  x - y; 
                     break;
              case '*':
                     answer = x * y;
                     break;
              case '/':
                     answer = x / y;
                     break;
              case '%':
                      answer = x % y;
                     break;
              case 'q':
              case 'Q':
                     done = true;
                     error = true;
                     break;
              default: 
                     cout << " Invalid operator! " << endl;
                     error = true;
          }
  
          if(error ==  false)
          {
                cout << "The answer was: " << answer << endl;
          }

     } while (done == false;);

        

     system ("pause");
     return EXIT_SUCCESS;

}


hope that points you in a better direction
Last edited on
Thanks a lot for your help. I still got a few more issues. I need to add the following functions to my code:

Exponentiation '^'
Factorial '!'
Log10 'L'

I tried adding them but it did not work. What step am I missing?
And can I enter multiple operands?

here's my last version of my code:

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

// Nawaf Alqahtani
// Oct 22, 2011

// Calculator Program

#include <iostream> // for cout, endl;

using namespace std;

int main()

{
char op;
int x=0;
int y=0;
int answer=0;
bool done = false;
bool error = false;


do
  { 
  int x, y; 	    
  cout << ("Welcome to Calculator")<< endl; 
  
  cout << "Enter an operand: " << endl;
  
  cin>> x;
  
  cout << "Enter an operation: " << endl;
  
  cin >> op;
  
  //Select the operation ('Q' to quit);
  //+, -, *, /, !,^, L, > !;
  cout << "Enter an operand: " << endl;
  
  cin >> y;
  
  cout << "Enter Q to quit" << endl;
  
switch (op)
{
  case '+':
  cout << " The answer is " << x + y << endl;
  break;
  case '-':
  cout << " The answer is " << x - y << endl;
  break;
  case '*':
  cout << " The answer is " << x * y << endl;
  break;
  case '/':
  cout << " The answer is " << x / y << endl;
  break;
  case '%':
  cout << " The answer is " << x % y << endl;
  break;		 
	case 'Q' :
	case 'q' :
			 done = true;
			 error = true;
			 break;
			 		 
	default: cout << " Invalid operator! " << endl;
	error = true;
}
 
cout << " Thank you for using calculator program ";
cin >> op;
}
while (op == 'y');
return 0;
	   	   	   				    
    
    system ("pause");
    return EXIT_SUCCESS;
    
}


Thank you guys,
The operator ^ is pow((double), (double)) found in this reference:
http://www.cplusplus.com/reference/clibrary/cmath/pow/

for Log it is in the same library as pow function which is here:
http://www.cplusplus.com/reference/clibrary/cmath/

for the factorial, you will have to write the function for that. There are many ways described out on the web, and I don't know what you have covered in your c++ class to get to any of them, sometimes it uses Recursion and other versions don't. Not all of them out there are written in c++.

I hope that points you in a direction.
Hi,
Im still having problems with exponents. I caouldnt even proceed to the next function because the program wont run. I changed my code, and you can see it differs from the previous version but I still couldnt figure it out. I hope you can help me. Thanks,

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
// Nawaf Alqahtani
// Oct 22, 2011

// Calculator Program

#include <iostream> // for cout, endl;
#include <cmath>
using namespace std;

int main()

{
char op;
int x=0;
int y=0;
int answer=0;
bool done = false;
bool error = false;


do
  { 
  int x, y; 	    
  cout << ("Welcome to Calculator")<< endl; 
  
  cout << "Enter an operand: " << endl;
  
  cin>> x;
  
  cout << "Enter an operation: " << endl;
  
  cin >> op;
  
  //Select the operation ('Q' to quit);
  //+, -, *, /, !,^, L, > !;
  cout << "Enter an operand: " << endl;
  
  cin >> y;
  
  cout << "Enter Q to quit" << endl;
  
switch (op)
{
  case '+':
  cout << " The answer is " << x + y << endl;
  break;
  case '-':
  cout << " The answer is " << x - y << endl;
  break;
  case '*':
  cout << " The answer is " << x * y << endl;
  break;
  case '/':
  cout << " The answer is " << x / y << endl;
  break;
  case '%':
  cout << " The answer is " << x % y << endl;
  break;		 
	case '^':
	cout << " The answer is " << ( pow( x, y)) << endl;
	break;
	case '!':
	cout << "The answer is " << x! << endl;
	break;
	case 'L':
	cout << " The answer is " << Log10(x) << endl;
	break;		 		 		 
	case 'Q' :
	case 'q' :
			 done = true;
			 error = true;
			 break;
			 		 
	default: cout << " Invalid operator! " << endl;
	error = true;
}
 
cout << " Thank you for using calculator program ";
cin >> op;
}
while (op == 'y');
return 0;
	   	   	   				    
    
    system ("pause");
    return EXIT_SUCCESS;
    
}

I figured out the exponentiation part but I still could not figure out the other 2 parts (! Factorial, L log10(x))

Please help!
Think about the logic of mine verses yours. Where do you state that op will ever equal 'y' from the user inputs? Q is for quit, and your operators and nothing is mentioned for y. Scope is minor issue and you have a lot of redundant code.

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
// Oct 22, 2011

// Calculator Program

#include <iostream> // for cout, endl;
#include <cmath>

using namespace std;

double getYvalue()
{
        // the goal here is to get the second value if it is needed or not
        double y = 0.0;
        
        cout << "Enter the Y value :" << endl;
        cin >> y;
 
        return y;
}

int main()
{
      // the character for the operand 
      char op;
      // the x value...
      double x = 0.0;
      // the y value...
      double y = 0.0;
      // the answer is stored here.
      double answer =0;
      // am I done with my loop?
      bool done = false;
      // did I report an Error?
      bool error = false;

     do
     { 
           cout << "Welcome to Calculator" << endl; 

           cout << "Enter a value for X: " << endl;
           cin >> x;

           cout << "Enter an operand: (Q - to Quit)" << endl;
           cin>> op;

           //Select the operation ('Q' to quit);
          //+, -, *, /, !,^, L, > !;

          switch (op)
          {
              case '+':
                     // we add two values
                     y = getYvalue();
                     answer  =x + y;
                     break; 
              case '-':
                     // we subtract two values
                     y = getYvalue();
                     answer =  x - y; 
                     break;
              case '*':
                     // we multiply two values.
                     y = getYvalue();
                     answer = x * y;
                     break;
              case '/':
                     // we divid two values.
                     y = getYvalue();
                    // make sure y isn't 0.
                     if(y != 0)
                     {
                          answer = x / y;
                     }
                     else
                     {
                           cout << "sorry cannot divide by zero!!" << endl;
                           error = true;
                     }
                     break;
              case '%':
                     // we get the % of two values 
                     y = getYvalue();
                     // again I need to make sure Y isn't 0.
                     if(y != 0)
                     {
                            answer = x % y;
                     }
                     else
                     {
                           cout << "sorry cannot divide by zero!!" << endl;
                           error = true;
                     }
                     break;
              case '!': // factorial....
                     // this function I am not writing for you.
                      answer = factorial(x);  // calculate x factorial.
                      break;
              case 'L': // log 10...
                      // find the log base 10 of x
                      answer = log10(x);
                      break;
              case '^':
                     y = getYvalue();
                     answer = pow(x, y);
                     break;
              case 'q':
              case 'Q':
                     // q- was hit and now I need to exit my loop so I set done to true and error to true, I don't need to print an answer.
                     done = true;
                     error = true;
                     break;
              default: 
                     cout << " Invalid operator! " << endl;
                     error = true;
          }
          
          // did I report an error? If true I don't print an answer...
          if(error ==  false)
          {
                cout << "The answer was: " << answer << endl;
          }

        // was Q hit at some point, if so done becomes true and I will exit the loop.
     } while (done == false );  // note the difference here to yours...

        

     system ("pause");
     return EXIT_SUCCESS;

}


I could write this in a completely different way to get the logic to flow a little cleaner, but I am following your logic to get the code I just posted. For example: the first thing I would ask is operator, then I could decide in the switch which numbers to ask the user for or quit out. All of your math structures need the x and not all need the y. Quitting doesn't need any of them.

I figure you are not reading my code differences to yours because the error you encountering are in the differences to the two code.
Last edited on
Thank you for your help. I finally noticed the differences. It took me a while because I am a beginner. Its my first time taking this class. I will try working on the factorial hopefully I find an answer. Thanks,
Topic archived. No new replies allowed.