Need Help on Simple calculator [Question Answered]

Hello I am creating a simple calculator program using functions. The current problem that I am having is that I can't figure out a way to call the function to perform the calculation depending on what operator has been entered.

How would I make a display function to print out the equation and then the answer after the calculation function returns a value?

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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
  #include<stdlib.h>
#include<cmath>
#include <iostream>

using namespace std;

// my prototypes

char menu ();
double getOperand ();
char getOperator ();
bool numOperandsNeeded (char);
double plus (double, char, double);
double sub (double, char, double);
double mult (double, char, double);
double div (double, char, double);
double rem (double, char, double);
double exp (double, char, double);
double log (double, char);
int factorial (int n);
double log (double, char);
void display2 (double, char, double);
void display1 (double, char);


int main ()
{
	double op1 =0;
	char optor = 0;
	double op2 =0;
	double ans =0;
   
    menu ();
	op1 = getOperand ();  
	optor = getOperator ();
	if((numOperandsNeeded (optor)) == 1)
	{ op2 = getOperand ();};
	
    /*switch (optor)
    {
           case '+':
                ans = plus(op1, op2);
           case '-':
                min ();
           case '*':
                mult ();
           case '/':
                div ();
           case '%':
                rem ();
           case '^':
                exp ();
           case 'L':
                log ();
           case '^':
                exp ();
           case '!':
                factorial ();
    }*/
    
	system ("pause");
	return EXIT_SUCCESS;
}

//functions
char menu()
{
  cout <<"Welcome to simple calculator!" << endl;
} 

double getOperand() 
{
  double op = 0;
  cout <<"Please enter a number"<< endl;
  cin >> op ;
  
  if (op >= 10000 || op <= -10000)
  {
    do 
  { 
    cout <<"Your Number is out of Range" ;
    cout << " Please Enter Another" << endl;
      
    cin >> op;
       

  }
    while (op >= 10000 || op <= -10000);
  
} 
 return op;
}

char getOperator ()
{
  char Optor = 0;
  cout << "Select an operator: " << endl;
  cout << " +, -, *, /, %, ^, !, L " << endl;
  cout << "Or 'q' to quit" << endl;
  cin >> Optor;
  
  while ( Optor == 'q')
  {
     return 0; 
  }
   
  if ( Optor == '+' || Optor == '-' || Optor == '*' || Optor == '/' || Optor == '%' || Optor == '^' || Optor == '!' || Optor == 'L')
  {
    return Optor;
  }
  else
  { 
    do 
    {
      cout << "The operator is not valid, please select another. " << endl;
      cin >> Optor; break;
    } while (Optor != '+' || Optor != '-' || Optor != '*' || Optor != '/' || Optor != '%' || Optor != '^' || Optor != '!' || Optor != 'L');
    
  }
  return Optor;
}

bool numOperandsNeeded (char Optor)
{
	switch (Optor)
    {
    	case '+':;
    	case '-':;
    	case '*':;
    	case '/':;
    	case '%':;
    	case '^': Optor = 1; break;
    
    	case '!':;
    	case 'L': Optor = 0; break;
    }
    return Optor;
}

double plus (double Op, char Optor, double Op2)
{
	double ans = 0;
	ans = Op + Op2;
	return ans;
}

double sub (double Op, char Optor, double Op2)
{
	double ans = 0;
	ans = Op - Op2;
	return ans;	
}

double mult (double Op, char Optor, double Op2)
{
	double ans = 0;
	ans = Op * Op2;
	return ans;	
}

double div (double Op, char Optor, double Op2)
{
	double ans = 0;
	ans = Op / Op2;
	return ans;
}

int rem (int Op, char Optor, int Op2)
{
	int ans = 0;
	ans = Op % Op2;
	return ans;
}

double exp (double Op, char Optor, double Op2)
{
	double ans = 0;
	ans = pow( Op, Op2);
	return ans;
}


int factorial(int Op) 
{ int i=0,fact=1; 
if (Op<=1) 
	{
	return(1); 
	} 
else 
	{
		for(i=1;i<=Op;i++) 
		{ 
			fact=fact*i; 
		} 
		return(fact); 
	}
}

double log (double Op, char Optor)
{
    
    double ans = 0;
		
	ans =(log (Op) / log (10));
	return ans;
}
Last edited on
closed account (o3hC5Di1)
Hi there,

You have a commented out switch-statement here that should do the trick with some minor alterations:

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
switch (optor)
    {
           case '+':
                ans = plus(op1, op2);
                break;
           case '-':
                min (op1,op2);
                break;
           case '*':
                mult (); //add arguments here
                break;
           case '/':
                div (); //add arguments here
                break;
           case '%':
                rem (); //add arguments here
                break;
           case '^':
                exp (); //add arguments here
                break;
           case 'L':
                log (); //add arguments here
                break;
           case '^':
                exp (); //add arguments here
                break;
           case '!':
                factorial (); //add arguments here
                break;
    }


"break" stops the switch-statement evaluating any further. Also, you need to pass the actual operands to the functions:

1
2
3
4
double plus (double Op, double Op2) //slightly optimized your plus function
{
	return (Op+Op2);
}


So in your switch, you pass the arguments to it:

1
2
3
4
5
switch (optor)
    {
           case '+':
                ans = plus(op1, op2);
                break;



As for a display function, you could declare it like this:

void display_equation(double left_operand , double right_operand, char operator, double answer);

You would only need to actually print out the operands, the operator and the answer in the correct order.

Hope that helps, please do let us know if you have any further questions.

All the best,
NwN
Last edited on
Awesome, thank you!
Topic archived. No new replies allowed.