Simple calculator

Hi there,

I wrote this code for a simple calculator, but am sure that it is inefficient. Is there any other better way of causing it to loop back after the default statement to a user input, other than using this "pass by reference" function? Also, if the code is alright (it does work), what changes should be made? 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
// Program which performs addition, subtraction, multiplication and subtraction.

#include <iostream>
using namespace std;

// input function
void Input (float &x, float &y);

float a=1.0, b=1.0, result;
char operation;


int main ()
{
	cout << "Program which performs addition, subtraction, multiplication and subtraction. \n\n";
	
	cout << "Please input calculation operation (eg. 1 + 2): \n";
	cin >> a >> operation >> b;
	
	Input (a,b);

	cout << "The answer is: " << result << endl;
	system ("pause");
	return 0;
}


void Input (float &x, float &y)
{
	a = x;
	b = y;

	switch (operation)
	{
		case '+':
			result = x + y;
			break;

		case '-':
			result = x - y;
			break;

		case '*':
			result = x * y;
			break;

		case '/':
			result = x / y;
			break;

		default:
			cout << "Improper operation. Please input a correct calculation operation: \n";
			cin >> a >> operation >> b;
			Input (a, b);
	}
}
Last edited on
1. Why is a function that performs calculations called "Input"?
2. Why are a,b, operation, and result global?
3.
1
2
3
4
5
6
Input (a,b);
//...
void Input (float &x, float &y)
{
	a = x;
	b = y;

What? Do you understand what "passing by reference" means?
4. User input verification should be done before the call to the function that does all the work.
Thanks for the reply, Helios

1. Yep you're right - it should have been called something else.

2. Because I didn't only use them in the main () function. I used them also in the "Input" function. What should I have done?

3. I have a basic idea, which I gained from reading a Cambridge C++ tutorial. What I understand is that it is where "the function is told where in memory the data is stored" (http://www-h.eng.cam.ac.uk/help/languages/C++/c++_tutorial/functions.html) and this address is stored in pointers x and y.
Why is what I wrote not passing by reference?

4. Haven't I done this?
1
2
3
4
        cout << "Please input calculation operation (eg. 1 + 2): \n";
	cin >> a >> operation >> b;
	
	Input (a,b);


Have you any other suggestions? :)


EDIT: Oops I also wanted to add this question, relating to another program:

The following program runs, but stops before displaying "Integer number = ", thereby not allowing the user to input a number. I can't find my mistake... can you help please? 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
// Program to calculate the factorial of a number with a function

#include <iostream>
using namespace std;

// Function Prototype
int Factorial (int M);

int main ()
{
	int number=0, result;

	// User input must be an integer number between 1 and 10
	cout << "Program to calculate the factorial of a number with a function." << endl;
	
	while (number<1 || number>10);
	{
		cout << "Integer number = ";
		cin >> number;
	}

	// Function call and assignment of return value to result
	result = Factorial (number);

	cout << "The answer is: " << result << endl;

	system ("pause");
	return 0;
}

int Factorial (int M)
{
	int i, factorial=1;

	for (i=1; i<=M; i++)
	{
		factorial = factorial * i;
	}
	return factorial;
}
Last edited on
2. a, b and operation could be local in main, result could be the value returned by the function

3. Passing by reference means that the arguments aren't copies of the variables passed but they are using the same location in memory
http://www.cplusplus.com/doc/tutorial/functions2.html

4. You should check if the input is valid before calling the function

(Second program)
Because you have at line 16 a semicolon after the while condition, so it loops forever
Last edited on
2. That's what function parameters are for. The reasoning you're following will sooner or later give you nothing but problems.
3. If you understand that, then you should be able to figure out why the lines I highlighted make no sense.
4. That's not user input verification. That's just user input.

5. Are you sure it stops? It may be that "Integer number = " is still inside the output buffer and you can't see it. Try adding <<std::flush at the end of the call to std::cout.
Thanks! This has helped very much.

I see that I put

1
2
a = x;
b = y;


instead of

1
2
x = a;
y = b;
Sigh...

x is a reference to the first parameter that was passed to Input(). Input() was called from main() as Input(a,b). a is a global variable visible from both main() and Input(). Therefore, a=x; and x=a; are equivalent to a=a;.
Topic archived. No new replies allowed.