Calculator

i'm doing a program, having some issues with it. I did a previous calculator program, and now i have to use it with functions.

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
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

void welcome(); // to print the welcome message
void promptUser(int&, char&, int&); // prompt user for expression to evaluate.
                                    // Function should return the 2 operands and the operator
                                   // in reference parameters
int calculate(int, char, int); // evaluate the expression and return the result
string numToString(int, int); // Returns the description of a digit
string opToString(char); // Returns the description of an operator

int main()
{
    int digit1,digit2;
    char op;
    
welcome();
promptUser();//need to return 2 operands and 1 operator
calculate();//use the 2 operands, and the operator from prompt User to calculate
numToString();//change the numbers from integers to string(1 to one)
opToString(); //change the operator from ( + to plus)
    
system("pause");
}


void welcome()
{
    // Welcome message
	cout << " ------------------------------------------------ \n\n"
              << " Welcome to The English Language Calculator\n"
              << " ------------------------------------------------- \n\n";
}

void promptUser(int digit1, char op, int digit2)
{
     
     cout<<"Please enter the simple expressions you would like "<<endl;
         cout<<"calculated in the following format : "<<endl;
         cout<<endl;
         cout<<"   ____________"<<endl;
         cout<<"  |  d1 op d2  |"<<endl;                     
         cout<<"  ~~~~~~~~~~~~~~"<<endl;
         cout<<endl;
         cout<<"Where d1 and d2 are single digits from 0 to 9, and "<<endl;
         cout<<"op is one the following operators: +, -, *, /, and ^ (power)."<<endl;
         cout<<endl;
         cout<<"Please enter your expression here: ";
         cin>>digit1>>op>>digit2;
         
         
         //Looking for digits bigger than 9, since this is a single digit calculator
    if (digit1>9||digit2>9)
             {
             cout<<"Invalid Input!!!"<<endl;
             cout<<"You entered a digit bigger than 9. This is a single digit "<<endl;
             cout<<"calculator."<<endl;
             }       
//First it looks for a division, then it looks if the 2nd digit is a 0
         else if (op=='/'&&digit2==0)
                  {   
                  cout<<"Invalid Input!!!"<<endl;
                  cout<<"A number cannot be divided by 0"<<endl;
                  }      
                  
                  else 
                  {    
                       cout<<digit1<<op<<digit2;
                  
                  }
                  
         	
}

int calculate(int digit1, char op, int digit2)
{ 
                       
                       double answer;
                      
                        switch (op)
                           {
                            case '+':answer=digit1+digit2;
                                     break;
                            case '-':answer=digit1-digit2;
                                     break;
                            case '*':answer=digit1*digit2;
                                     break;
                            case '/':answer=((float)digit1/(float)digit2);
                                     break;
                            case '^':answer=pow((double)digit1, (double)digit2);
                                     break;
                            default:cout<<"Invalid Operator!"<<endl;
                                     break;
                            }//END SWITCH OP
                            return answer;
}

string numToString(int digit1, int digit2)
{
 	                   string digEn1,digEn2;
 	                   
                        switch (digit1)
                                {
                                case 0:digEn1="Zero";
                                       break;         
                                case 1:digEn1="One";
                                       break;
                                case 2:digEn1="Two";
                                       break;
                                case 3:digEn1="Three";
                                       break;
                                case 4:digEn1="Four";
                                       break;
                                case 5:digEn1="Five";
                                       break;      
                                case 6:digEn1="Six";
                                       break;     
                                case 7:digEn1="Seven";
                                       break;        
                                case 8:digEn1="Eight";
                                       break;         
                                case 9:digEn1="Nine";
                                       break;       
                               default:cout<<"Invalid Input!!!"<<endl;     
                                }//End switch digit1
                                
                   //same as above, will translate each number to english
						 switch (digit2)
                                {
                                case 0:digEn2="Zero";
                                       break;         
                                case 1:digEn2="One";
                                       break;
                                case 2:digEn2="Two";
                                       break;
                                case 3:digEn2="Three";
                                       break;
                                case 4:digEn2="Four";
                                      break;
                                case 5:digEn2="Five";
                                      break;      
                                case 6:digEn2="Six";
                                      break;     
                                case 7:digEn2="Seven";
                                      break;        
                                case 8:digEn2="Eight";
                                      break;         
                                case 9:digEn2="Nine";
                                       break;       
                                default:
                                        cout<<"Invalid INPUT"<<endl;
                                }
} 
string opToString(char op)
{
       string opStr;
       switch(op)
			{
			  case '+': opStr = " plus ";
				        break; 
			  case '-': opStr = " minus ";
				        break; 
			  case '*': opStr = " multiplied by ";
				        break; 
			  case '/': opStr = " divided by ";
				        break; 
			  default: opStr = " to the power ";
			}
			
} 
having some problems calling the funstions.
Your functions take input parameters. You have to put things in the brackets when you call them.

promptUser(have to put parameters in here);
Last edited on
when i do, i get errors
thats what i do:

welcome();
promptUser(digit1, op, digit2);//need to return 2 operands and 1 operator
calculate(digit1,op,digit2);//use the 2 operands, and the operator from prompt User to calculate
numToString(digit1,digit2);//change the numbers from integers to string(1 to one)
opToString(op); //change the operator from ( + to plus)
when i do, i get errors

Then you must be doing it wrongly.

promptUser(digit1, op, digit2);
Ah, see here. digit1 is a string, op is a floating point number, and digit2 is an fstream object. You have to pass in an int, a char, and an int.

What's that you say? You are passing in an int, a char, and an int? I'm sorry, I had to guess what you were passing in because you didn't make it clear. If you showed us the whole code, we wouldn't have to guess :)
Last edited on
the initial code (w/o function) is:
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

#include <iostream>
#include <cmath> 
#include <math.h>
#include <string>
using namespace std; 
 
int main(){ 
 // PRESENTATION
    int digit1,digit2;
    char op;
    
    double answerplus, answerminus,answermulti,answerdiv,answerpower;
    string digEn1, digEn2;
         
         cout<<" _______________________________________________"<<endl;
         cout<<"|   Welcome to the English Language Calculator   |"<<endl; 
         cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
         
          
         cout<<"Please enter the simple expressions you would like "<<endl;
         cout<<"calculated in the following format : "<<endl;
         cout<<endl;
         cout<<"   ____________"<<endl;
         cout<<"  |  d1 op d2  |"<<endl;                     
         cout<<"  ~~~~~~~~~~~~~~"<<endl;
         cout<<endl;
         cout<<"Where d1 and d2 are single digits from 0 to 9, and "<<endl;
         cout<<"op is one the following operators: +, -, *, /, and ^ (power)."<<endl;
         cout<<endl;
         cout<<"Please enter your expression here: ";
         cin>>digit1>>op>>digit2;
         
        
//Looking for digits bigger than 9, since this is a single digit calculator
    if (digit1>9||digit2>9)
             {
             cout<<"Invalid Input!!!"<<endl;
             cout<<"You entered a digit bigger than 9. This is a single digit "<<endl;
             cout<<"calculator."<<endl;
             }       
//First it looks for a division, then it looks if the 2nd digit is a 0
         else if (op=='/'&&digit2==0)
                  {   
                  cout<<"Invalid Input!!!"<<endl;
                  cout<<"A number cannot be divided by 0"<<endl;
                  }        
                  else 
                     {  
                     //This switch/case statement will take each number entered and translate it in english
						 switch (digit1)
                                {
                                case 0:digEn1="Zero";
                                       break;         
                                case 1:digEn1="One";
                                       break;
                                case 2:digEn1="Two";
                                       break;
                                case 3:digEn1="Three";
                                       break;
                                case 4:digEn1="Four";
                                       break;
                                case 5:digEn1="Five";
                                       break;      
                                case 6:digEn1="Six";
                                       break;     
                                case 7:digEn1="Seven";
                                       break;        
                                case 8:digEn1="Eight";
                                       break;         
                                case 9:digEn1="Nine";
                                       break;       
                               default:cout<<"Invalid Input!!!"<<endl;     
                                }//End switch digit1
                                
                   //same as above, will translate each number to english
						 switch (digit2)
                                {
                                case 0:digEn2="Zero";
                                       break;         
                                case 1:digEn2="One";
                                       break;
                                case 2:digEn2="Two";
                                       break;
                                case 3:digEn2="Three";
                                       break;
                                case 4:digEn2="Four";
                                      break;
                                case 5:digEn2="Five";
                                      break;      
                                case 6:digEn2="Six";
                                      break;     
                                case 7:digEn2="Seven";
                                      break;        
                                case 8:digEn2="Eight";
                                      break;         
                                case 9:digEn2="Nine";
                                       break;       
                                default:
                                        cout<<"Invalid Input!!!"<<endl;     
                                }//End switch digit2
                      
					//this swtich  statement will look for the operation, when done it will do the calculation and output the results.            
                        switch (op)
                           {
                            case '+':answerplus=digit1+digit2;
                                     cout<<digEn1<<" plus "<<digEn2<<" is equal to "<<answerplus<<endl;
                                     break;
                            case '-':answerminus=digit1-digit2;
                                     cout<<digEn1<<" minus "<<digEn2<<" is equal to "<<answerminus<<endl;
                                     break;
                            case '*':answermulti=digit1*digit2;
                                     cout<<digEn1<<" multiplied by "<<digEn2<<" is equal to "<<answermulti<<endl;
                                     break;
                            case '/':answerdiv=((float)digit1/(float)digit2);
                                     cout<<digEn1<<" divide by "<<digEn2<<" is equal to "<<answerdiv<<endl;
                                     break;
                            case '^':answerpower=pow((double)digit1, (double)digit2);
                                     cout<<digEn1<<" to the power of "<<digEn2<<" is equal to "<<answerpower<<endl;
                                     break;
                            default:cout<<"Invalid Operator!"<<endl;
                                    break;
                            }//END SWITCH OP
                            }//END IF -ELSE STATEMENT     
                            
    
            system ("pause");
            
            }//END MAIN() 
now i have to put that into functions, by following those instruction:

void welcome(); // to print the welcome message

void promptUser(int&, char&, int&); // prompt user for expression to evaluate.
// Function should return the 2 operands and the operator
// in reference parameters

int calculate(int, char, int); // evaluate the expression and return the result

string numToString(int); // Returns the description of a digit

string opToString(char); // Returns the description of an operator
any idea on what i should do?
or how to call the functions?

was able to compile 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
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
#include<iostream>
#include <string>
#include <cmath>

using namespace std;

void welcome(); // to print the welcome message
void promptUser(int&, char&, int&); // prompt user for expression to evaluate.
                                    // Function should return the 2 operands and the operator
                                   // in reference parameters
int calculate(int, char, int); // evaluate the expression and return the result
string numToString(int, int); // Returns the description of a digit
string opToString(char); // Returns the description of an operator

int main()
{
    int digit1,digit2;
    char op;
    
welcome();
promptUser(digit1,op, digit2);//need to return 2 operands and 1 operator
calculate(digit1,op,digit2);//use the 2 operands, and the operator from prompt User to calculate
numToString(digit1,digit2);//change the numbers from integers to string(1 to one)
opToString(op); //change the operator from ( + to plus)
    
system("pause");
}


void welcome()
{
    // Welcome message
	cout << " ------------------------------------------------ \n\n"
		 << " Welcome to Ali's English Language Calculator\n"
		 << " ------------------------------------------------- \n\n"; 
}

void promptUser(int& digit1, char& op, int& digit2)
{
     
     cout<<"Please enter the simple expressions you would like "<<endl;
         cout<<"calculated in the following format : "<<endl;
         cout<<endl;
         cout<<"   ____________"<<endl;
         cout<<"  |  d1 op d2  |"<<endl;                     
         cout<<"  ~~~~~~~~~~~~~~"<<endl;
         cout<<endl;
         cout<<"Where d1 and d2 are single digits from 0 to 9, and "<<endl;
         cout<<"op is one the following operators: +, -, *, /, and ^ (power)."<<endl;
         cout<<endl;
         cout<<"Please enter your expression here: ";
         cin>>digit1>>op>>digit2;
         
         
         //Looking for digits bigger than 9, since this is a single digit calculator
    if (digit1>9||digit2>9)
             {
             cout<<"Invalid Input!!!"<<endl;
             cout<<"You entered a digit bigger than 9. This is a single digit "<<endl;
             cout<<"calculator."<<endl;
             }       
//First it looks for a division, then it looks if the 2nd digit is a 0
         else if (op=='/'&&digit2==0)
                  {   
                  cout<<"Invalid Input!!!"<<endl;
                  cout<<"A number cannot be divided by 0"<<endl;
                  }      
                  
                  else 
                  {    
                       cout<<digit1<<op<<digit2;
                  
                  }
                  
         	
}

int calculate(int digit1, char op, int digit2)
{ 
                       
                       double answer;
                      
                        switch (op)
                           {
                            case '+':answer=digit1+digit2;
                                     break;
                            case '-':answer=digit1-digit2;
                                     break;
                            case '*':answer=digit1*digit2;
                                     break;
                            case '/':answer=((float)digit1/(float)digit2);
                                     break;
                            case '^':answer=pow((double)digit1, (double)digit2);
                                     break;
                            default:cout<<"Invalid Operator!"<<endl;
                                     break;
                            }//END SWITCH OP
                            return answer;
}

string numToString(int digit1, int digit2)
{
 	                   string digEn1,digEn2;
 	                  
                        switch (digit1)
                                {
                                case 0:digEn1="Zero";
                                       break;         
                                case 1:digEn1="One";
                                       break;
                                case 2:digEn1="Two";
                                       break;
                                case 3:digEn1="Three";
                                       break;
                                case 4:digEn1="Four";
                                       break;
                                case 5:digEn1="Five";
                                       break;      
                                case 6:digEn1="Six";
                                       break;     
                                case 7:digEn1="Seven";
                                       break;        
                                case 8:digEn1="Eight";
                                       break;         
                                case 9:digEn1="Nine";
                                       break;       
                               default:cout<<"Invalid Input!!!"<<endl;     
                                }//End switch digit1
                                
                   //same as above, will translate each number to english
						 switch (digit2)
                                {
                                case 0:digEn2="Zero";
                                       break;         
                                case 1:digEn2="One";
                                       break;
                                case 2:digEn2="Two";
                                       break;
                                case 3:digEn2="Three";
                                       break;
                                case 4:digEn2="Four";
                                      break;
                                case 5:digEn2="Five";
                                      break;      
                                case 6:digEn2="Six";
                                      break;     
                                case 7:digEn2="Seven";
                                      break;        
                                case 8:digEn2="Eight";
                                      break;         
                                case 9:digEn2="Nine";
                                       break;       
                                default:
                                        cout<<"Invalid INPUT"<<endl;
										
                                }return 0;
} 
string opToString(char op)
{
       string opStr;
       switch(op)
			{
			  case '+': opStr = " plus ";
				        break; 
			  case '-': opStr = " minus ";
				        break; 
			  case '*': opStr = " multiplied by ";
				        break; 
			  case '/': opStr = " divided by ";
				        break; 
			  default: opStr = " to the power ";
			}
			return 0;
} 
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
string numToString(int digit1, int digit2)
{
 	                   string digEn1,digEn2;
 	                  
                        switch (digit1)
                                {
                                case 0:digEn1="Zero";
                                       break;         
                                case 1:digEn1="One";
                                       break;
                                case 2:digEn1="Two";
                                       break;
                                case 3:digEn1="Three";
                                       break;
                                case 4:digEn1="Four";
                                       break;
                                case 5:digEn1="Five";
                                       break;      
                                case 6:digEn1="Six";
                                       break;     
                                case 7:digEn1="Seven";
                                       break;        
                                case 8:digEn1="Eight";
                                       break;         
                                case 9:digEn1="Nine";
                                       break;       
                               default:cout<<"Invalid Input!!!"<<endl;     
                                }//End switch digit1
                                
                   //same as above, will translate each number to english
						 switch (digit2)
                                {
                                case 0:digEn2="Zero";
                                       break;         
                                case 1:digEn2="One";
                                       break;
                                case 2:digEn2="Two";
                                       break;
                                case 3:digEn2="Three";
                                       break;
                                case 4:digEn2="Four";
                                      break;
                                case 5:digEn2="Five";
                                      break;      
                                case 6:digEn2="Six";
                                      break;     
                                case 7:digEn2="Seven";
                                      break;        
                                case 8:digEn2="Eight";
                                      break;         
                                case 9:digEn2="Nine";
                                       break;       
                                default:
                                        cout<<"Invalid INPUT"<<endl;
										
                                }class="quote">
class="qd">return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
string opToString(char op)
{
       string opStr;
       switch(op)
			{
			  case '+': opStr = " plus ";
				        break; 
			  case '-': opStr = " minus ";
				        break; 
			  case '*': opStr = " multiplied by ";
				        break; 
			  case '/': opStr = " divided by ";
				        break; 
			  default: opStr = " to the power ";
			}
			class="quote">
class="qd">return 0;
}

Why do you return 0 .
Last edited on
A void function cannot return anything so you wanna change the variables to reference
in the first part because the numbers are not getting back into main.

EDIT: ohh you did change the function prototype variables to reference but i think you need to change the function header too to make it look like this.

 
void promptUser(int & digit1, char op, int & digit2)


and also in your call function in main you should be sending variables into your void function so the reference will actually store what you referenced back into the variables in main like this.

 
promptUser(digit1, op, digit2);


So it should send digit1, op, digit2 into the function, then in that function they will be changed in scope and since they are in being referenced they will also change in the function main.
Last edited on
Topic archived. No new replies allowed.