code functions that will perform the various arithmetic operations and then call the functions in main()

The calling statements that are placed in main() should replace the calculations that are currently in the switch statement.

I need to write and use the following functions:
int addition( int value1, int value2 )
int subtraction( int value1, int value2 )
int multiplication( int value1, int value2 )
int quotient( int value1, int value2 )
int remainder( int value1, int value2 )
int power( int value1, int value2 )
int factorial( int 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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <cmath>

using namespace std;

int main()
{

char operation;
int num1, num2, result, remain;


//Display the menu to the user and get their first choice

	cout << "What operation would you like to perform:" << endl 
	<< " + addition\n - subtraction\n * multiplication\n / division\n ^ number to power\n ! factorial" 
	<< "\n q quit" << endl << endl << "Operation? ";
	cin >> operation;
while (operation != 'q')
{

//Switch - the user does not want to quit

switch (operation)
{

case '+': 
	//read input first
	cout << "Enter the first number to add: " << endl;
	cin >> num1;
	cout << "Enter the second number to add: " << endl;
	cin >> num2;
	//compute the results
	result=num1+num2;
	cout << endl << num1 << " + " << num2 << " = " << result;
	break;

case '-': 
	//read input first
	cout << "Enter the first number to subtract: " << endl;
	cin >> num1;
	cout << "Enter the second number to subtract: " << endl;
	cin >> num2;
	//compute the results
	result=num1-num2;
	cout << endl << num1 << " - " << num2 << " = " << result;
	break;

case '*': 
	//read input first
	cout << "Enter the first number to multiply: " << endl;
	cin >> num1;
	cout << "Enter the second number to multiply: " << endl;
	cin >> num2;
	//compute the results
	result=num1*num2;
	cout << endl << num1 << " * " << num2 << " = " << result;
	break;

case '/': 
	//read input first
	cout << "Enter the dividend: " << endl;
	cin >> num1;
	cout << "Enter the divisor: " << endl;
	cin >> num2;
	//compute the results
	result=num1/num2;
	cout << endl << num1 << " / " << num2 << " = " << result;
	result=num1%num2;
	cout << endl << num1 << " % " << num2 << " = " << result;
	break;
 
case '^':
	//read input first
	cout << "Enter the base number " << endl;
	cin >> num1;
	cout << "Enter the power: " << endl;
	cin >> num2;
	//compute the results
	result=num1^num2;
	cout << endl << num1 << " ^ " << num2 << " = " << result;
	break;

case '!': 
	//read input first
	cout << "Enter a number: " << endl;
	cin >> num1;
	//compute the results
	num1!=result;
	cout << endl << num1 << " ! " << " = " << result;
	break;

default: 
	cout << "That is an invalid operation!" << endl; 	
	break;

} // switch statement closed

cout << endl << "What operation would you like to perform:" << endl
<< " + addition\n - subtraction\n * multiplication\n / division\n ^ number to power\n ! factorial"
<< "\n q quit"
<< endl << endl << "Operation? ";
cin >> operation; 

}
return 0;
} //main statement closed 
Here's an example of the first one:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int addition( int value1, int value2 ){

   return value1+value2;
}


int main(){
 
int num1, num2, add;
cout << "enter the first number" << '\n';
cin >> num1;
cout << "enter the second number" << '\n';
cin >> num2;
add = addition(num1, num2);
cout << "The sum of " << num1 << " and " << num2 << " is " << add<< '\n';
return 0;
}
Last edited on
Thank you. Does this replace

case '+':
//read input first
cout << "Enter the first number to add: " << endl;
cin >> num1;
cout << "Enter the second number to add: " << endl;
cin >> num2;
//compute the results
result=num1+num2;
cout << endl << num1 << " + " << num2 << " = " << result;
break;

or do I still need the case '+': and that follows under?
Last edited on
I believe he got rid of your switch statement entirely and instead created an addition function (lines 1-4, notice it is outside of main) which accepts two integer values which will be added together and returned.

So you have to follow lines 1-4 but change it into multiplication, division and etc. and similarly call them in main
I still need to switch statement in it. My professor's directions are: Part 2 of the assignment, code functions that will perform the various arithmetic operations and then call the functions in main(). The calling statements that are placed in main() should replace the calculations that are currently in the switch statement.

She doesn't answer questions and just tells us to figure it out, that's why I am here. Did I misunderstand the directions? Is there a way to make that into a switch with what he did?
1. define all the functions before main as arbwok did with addition()
2. in main set up a switch : case 1 : addition(), case 2: subtraction(), ...
as side-notes:
(a) you may wish to const qualify the functions' argument(s)
(b) note signatures of all functions except factorial() are same, so without the factorial() function you might also have been able to use function pointers within the switch
Topic archived. No new replies allowed.