First program(Calculator) Please help!

Well, I'm trying to make my first program, in this case a calculator.
But when i run it, i enter the first number, and then (per example) - and then it closes... but i have no idea why.
could you please help me??

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
#include <iostream>
using namespace std;

int Plus() {
	int num1;
	int num2;
	cout << "The Answer is: " << num1 + num2 << endl;
	cout << "Press ENTER to continue";
	getchar();
	return 0;
}

int Min() {
	int num1;
	int num2;
	cout << "Press ENTER to continue";
	getchar()
	return 0;
}

int Divide() {
	int num1;
	int num2;
	cout << "Het antwoord is: " << num1 / num2 << endl;
	cout << "Press ENTER to continue";
	getchar();
	return 0;
}

int Multiply() {
	int num1;
	int num2;
	cout << "Het antwoord is: " << num1 * num2 << endl;
	cout << "Press ENTER to continue";
	getchar();
	return 0;
}

int main() {
	int num1, num2;
	int i;
	cout << "Enter the sum." << endl;
	cin >> num1;
	cin >> i;
	cin >> num2;
	if(i == '+')
		Plus();
		else if (i == '-')
			Min();
		else if (i == '/')
			Divide();
		else if (i == '*')
			Multiply();
	else {
		cout << "The sum is invalid." << endl;
		cout << "Press ENTER to continue..." << endl;
		getchar();
	}
}

thanks.
closed account (GADSLyTq)
I provided a link that I believe might help you with your problem.

http://www.cplusplus.com/forum/beginner/1988/

Are you using an IDE or terminal?
Last edited on
thanks for looking at my post, but it doesn't work. It still only lets me enter the first number and then the operator, and then the console shuts down(while i still have to enter the 3rd number).
Do you have another idea that could solve the problem?
Last edited on
I'm using visual C++ 2010 Express
First you need to add a semicolon on line 17.
Then read some tutorial on C++ function parameters.
The variables num1 & num2 you used in main() and all other functions are unrelated, except for having the same variable names. You might want to also read about function scope.
thanks, i'll try that now, i'll let you know if it worked.
closed account (GADSLyTq)
I see many problems with your current code. As wjee0910 stated read up on function in c++. If you have any further questions please feel free to ask.
And what about this? i read the tutorial at the documentation part( http://cplusplus.com/doc/tutorial/functions/) so this is what i got now, is it better?(it still does the same as before(doesn't let me enter the last number))
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>
using namespace std;

int Plus(int a, int b) 
{
	int r;
    r=a+b;
    return (r);
}

int Min(int a, int b) 
{
	int r;
    r=a-b;
    return (r);
}

int Divide(int a, int b) 
{
	int r;
    r=a/b;
    return(r);
}

int Multiply(int a, int b) 
{
	int r;
    r=a*b;
    return (r);
}

int main() 
{
   	int num1, num2;
	int i;
	cout << "Enter the sum." << endl;
	cin >> num1 >> i >> num2;
    switch (i) {
           case '+': cout << "Result: " << Plus(num1,num2);
                break;
           case '-': cout << "Result: " << Min(num1,num2);
                break;
           case '*': cout << "Result: " << Multiply(num1,num2);
                break;
           case '/': cout << "Result: " << Divide(num1,num2);
                break;
           default : cout << "The sum is invalid." << endl;
    system("PAUSE");  
    return 0;
	}
}
Last edited on
Glad that you now know the basics of functions.
Suggestions:
Line 37: Use "Enter the equation:" instead. It kinda confused me.
Use type char for i.
tyvm for ur help :D, it works perfect now.
Topic archived. No new replies allowed.