Problem with switch/case statements

So I have to write a simple calculator program that performs a chosen operation and keeps a running total using switch/case statements. For some reason my program ignores the first and third case statements. It will subtract and divide, but not add or multiply. When I swap the order in the code, whatever operations I place in the first and third slots won't be performed. What gives?

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

using namespace std;
//Adds, subtracts, multiplies and divides 
//per the user's request and keeps a running total

int main()
{
	double total, number;
	char operand;
	total = 0;
	do
	{
		cout << "Current total is " << total << endl;
		cout << "Enter an operation: + - * / (or enter X to exit):";
		cin >> operand;
		if ((operand == '+') || (operand == '-') || (operand == '*') || (operand == '/'))
		{
			cout << "Enter a number: ";
			cin >> number;
			switch (operand)
			{

			case '+':
			
				total = total + number;
		
			case '-':
			
				total = total - number;
		
			case '*':

				total = number * total;
		
			case '/':

				if (number != 0)
				{
					total = total / number;
				}
				else
				{
					cout << "Can not divide by zero!" << endl;
				}
			};
		}
	} while (operand != 'X');
}


Oh, and is there a way to end the program when the user types X using switch/case and without using return()? My instructor gives these 'helpful' answers that are probably meant to make me think but really just make me want to hit my computer with a hammer. Help is appreciated.
Last edited on
As a beginner, I would recommed labeling the case values with numbers. Here is a sample calculator I worked on months ago.

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

using namespace std;

int main()
{
	int i;
	int result, num1, num2, choice;
	cout<<"Enter the numbers: "<<endl;
	cin>>num1>>num2;
	cout<<"\nPress 1 to add, 2 to subtract, 3 to multiply, 4 to divide: "<<endl;
	cin>>choice;
	switch(choice)
	{
	case 1:
		result = num1 + num2;
		cout<<num1<<" + "<<num2<<" = "<<result<<endl;
		break;
	case 2:
		result = num1 - num2;
		cout<<num1<<" - "<<num2<<" = "<<result<<endl;
		break;
	case 3:
		result = num1 * num2;
		cout<<num1<<" * "<<num2<<" = "<<result<<endl;
		break;
	case 4:
		if(num2 == 0)
			cout<<"Division by zero not allowed."<<endl;
		else
		{
			result = num1 / num2;
			cout<<num1<<" / "<<num2<<" = "<<result<<endl;
		}
		break;
	default:
		cout<<"Invalid input."<<endl;
	}
	cin>>i; // read output
	return 0;
}

Labeling the cases with numbers is a good idea, but it is forbidden by the instructor. Besides, the second and fourth case statements work; what is up with the first and third?
hmm.. try getting rid of the if statement before the switch statement
Here, I included a set of if/else if statements to convert operands to numbers, and have the same problem:

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

using namespace std;
//Adds, subtracts, multiplies and divides 
//per the user's request and keeps a running total

int main()
{
	double total, number;
	int casenum;
	char operand;
	total = 0;
	do
	{
		cout << "Current total is " << total << endl;
		cout << "Enter an operation: + - * / (or enter X to exit):";
		cin >> operand;
		if (operand == '+')
		{
			casenum = 1;
		}
		else if (operand == '-')
		{
			casenum = 2;
		}
		else if (operand == '*')
		{
			casenum = 3;
		}
		else if (operand == '/')
		{
			casenum = 4;
		}
		if ((operand == '+') || (operand == '-') || (operand == '*') || (operand == '/'))
		{
			cout << "Enter a number: ";
			cin >> number;
			switch (casenum)
			{

			case 1:
			
				total = total + number;
		
			case 2:
			
				total = total - number;
		
			case 3:

				total = number * total;
		
			case 4:

				if (number != 0)
				{
					total = total / number;
				}
				else
				{
					cout << "Can not divide by zero!" << endl;
				}
			}
		}
	} while (operand != 'X');
}
Tried removing the if statement; it's still ignoring the first and third case statements. I am seriously baffled. Textbook was no help here.
lol..that's telling me you may be losing data somewhere but I'm not entirely sure. I wish I could help.
Thanks for trying, yoked. If anyone else wants to try, I added in a simple cout to make it output the operand if it executed the first case statement and it did so. So the first and third case statements are being executed, but for some reason in those two statements the total is not being updated. Either the arithmetic is not being done or I don't know what. Help, anyone?
Nevermind, I figured it out. I forgot the breaks in my case statements. How dumb of me. If you start with 0 (or any number) and then add 2, subtract 2, multiply by 2, and divide by 2, you get back to zero. If you just subtract 2, multiply by 2, and divide by 2, you never cancel the subtraction and get negative 2.

As tempted as I am to delete this to cover my stupid tracks, I'll leave it up for future Nittany Lions also afflicted by the stupid who take this course.
heh..I never noticed the missing breaks. Good catch.
Topic archived. No new replies allowed.