How do I call different variables from one function to another?

So, here's the issue.
I was told to make a basic calculator with Addition, Subtraction, Multiplication and Division functions. The program will first take the input of two numbers and a character, all in another function. The character will be used to determine which operation is to be used. Sort of like 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
#include <iostream>
using namespace std;

int Calculator();

int main()
{
	int S;

	S = Calculator();

	if (S == '+')
	{
		Addition();
	}
	else if (S == '-')
	{
		Subtraction();
	}
	else if (S == 'x')
	{
		Multiplication();
	}
	else if (S == '/')
	{
		Division();
	}

	
}

int Calculator()
{
	int Num1 = {}, Num2 = {};
	char Operation;

	cout << "Welcome!" << endl;
	cout << "Please enter First Number here: ";
	cin >> Num1;
	cout << "Please Enter Second Number here: ";
	cin >> Num2;
	cout << endl;
	cout << "Please select the operation from the following: " << endl;
	cout << "(Addition = +; Subtraction = -; Multiplication = x; Division = /)" << endl;
	cout << "Enter here: ";
	cin >> Operation;

	while (Operation != '+' && Operation != '-' && Operation != 'x' && Operation != '/')
	{
		cout << "Invalid Input. Please try again." << endl;
		cout << "Enter here: ";
		cin >> Operation;
	}

	return Operation;
}


(I know this doesn't work, this is the most I could get right)

Here's the problem, though. How do I get the two input values into the the other functions? The values are supposed to be taken in the Calculator function only.
Doesn't it make more sense to call Addition, Subtraction, Multiplication and Division from within Calculator? Then you could pass the values of Num1 and Num2 as arguments to these functions.
Last edited on
Based upon the OP design, then possibly something like 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
#include <iostream>

int Calculator(int&, int&);
int Addition(int, int);
int Subtraction(int, int);
int Multiplication(int, int);
int Division(int, int);

int main() {
	int v1 {}, v2 {}, result {};

	int S {Calculator(v1, v2)};

	if (S == '+')
		result = Addition(v1, v2);
	else if (S == '-')
		result = Subtraction(v1, v2);
	else if (S == 'x')
		result = Multiplication(v1, v2);
	else
		result = Division(v1, v2);

	std::cout << "Result is " << result << '\n';
}

int Addition(int n1, int n2) { return n1 + n2; }
int Subtraction(int n1, int n2) { return n1 - n2; }
int Multiplication(int n1, int n2) { return n1 * n2; }
int Division(int n1, int n2) { return n1 / n2; }

int Calculator(int& Num1, int& Num2) {
	char Operation {};

	std::cout << "Welcome!\n";
	std::cout << "Please enter First Number here: ";
	std::cin >> Num1;

	std::cout << "Please Enter Second Number here: ";
	std::cin >> Num2;

	std::cout << "\nPlease select the operation from the following:\n";
	std::cout << "(Addition = +; Subtraction = -; Multiplication = x; Division = /)\n";

	do {
		std::cout << "Enter here: ";
		std::cin >> Operation;

	} while (Operation != '+' && Operation != '-' && Operation != 'x' && Operation != '/' &&
		(std::cout << "Invalid Input. Please try again.\n"));

	return Operation;
}


Note that * is usually used for multiplication rather than x. Also note that division is integer - so 7 / 4 is 1.
Topic archived. No new replies allowed.