My calculator doesnt run!

This is my first calculator and it´s the first time I use functions or switch so I need help becuse it doesnt run. May someone tell me why it doesnt work?

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

int addition (int x, int y)
{
	int a;
	a=x+y;
	return a;
}

int substraction (int x, int y)
{
	int s;
	s=x-y;
	return s;
}

int multiplication (int x, int y)
{
	int m;
	m=x*y;
	return m;
}

int division (int x, int y)
{
	int d;
	d=x/y;
	return d;
}

int main()
{
	int o, a, b;
	cout << "What operation do you want to do?" << endl;
	cout << "Write +, -, * or /." << endl;
	cin << o;
    switch (o)
	{
		case +:
			cout << "Now write the numbers.";
			cin << a << endl;
			cout << "+" << endl;
			cin << b << endl;
			o = addition (a,b);
			cout << "__________" << endl << o;
		case -:	
		    cout << "Now write the numbers.";
			cin << a << endl;
			cout << "-" << endl;
			cin << b << endl;
			o = substraction (x,y);
			cout << "__________" << endl << o;
		case *:	
		    cout << "Now write the numbers.";
			cin << a << endl;
			cout << "*" << endl;
			cin << b << endl;
			o = multiplication (x,y);
			cout << "__________" << endl << o;
		case /:	
		    cout << "Now write the numbers.";
			cin << a << endl;
			cout << "/" << endl;
			cin << b << endl;
			o = division (x,y);
			cout << "__________" << endl << o;		
	}	
}
Firstly switches only work on numerical variables (including char). You're switching on an integer however your cases aren't numerical. I'm not actually sure what those would be classified under haha. But they can't be treated as an int. Also cin << a << endl; should be
1
2
cin >> a;
cout << "\n";


You're using the << operator instead of the >> operator. << is only for cout.

And why are you assigning your functions to o? You can do this instead
std::cout << addition(a, b);

Lastly, x and y are undeclared variables in your function calls in your switch statements.

Edit:
Also just change int o; to a char and put your case values in single quotations like so:

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

using namespace std;

int addition(int x, int y)
{
	int a;
	a = x + y;
	return a;
}

int substraction(int x, int y)
{
	int s;
	s = x - y;
	return s;
}

int multiplication(int x, int y)
{
	int m;
	m = x*y;
	return m;
}

int division(int x, int y)
{
	int d;
	d = x / y;
	return d;
}

int main()
{
	char o;
	
	int a, b;
	cout << "What operation do you want to do?" << endl;
	cout << "Write +, -, * or /." << endl;
	cin >> o;
	switch(o)
	{
		case '+':
			cout << "Now write the numbers.";
			cin >> a;
			cout << "+" << endl;
			cin >> b;
			cout << addition(a, b);

		case '-':
			cout << "Now write the numbers.";
			cin >> a;
			cout << "-" << endl;
			cin >> b;
			cout << substraction(a, b);

		case '*':
			cout << "Now write the numbers.";
			cin >> a;
			cout << "*" << endl;
			cin >> b;
			cout << multiplication(a, b);

		case '/' :
			cout << "Now write the numbers.";
			cin >> a;
			cout << "/" << endl;
			cin >> b;
			cout << division(a, b);
	}
}


I also fixed all of the errors.
Last edited on
Thanks XD I´m just getting started
Topic archived. No new replies allowed.