Two Switch Structures

If anyone can help me on switches. How can I differentiate the two if S is chosen or M is chosen.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
if (in_marital_status == "s")
		case_no= (income <= single_level1) || (income<= single_level2);	
		switch (case_no)
				{
					case 1:	(income <= single_level1); tax = rate1 * income; break;
					case 2: (income <= single_level2); tax = single_tax1 + rate2 * (income- single_level1);break;
					case 3: tax = single_tax2 + rate3 * (income-single_level2);break;
					
				}	
		
if (in_marital_status == "m")
		case_no= (income <= married_level1) || (income <= married_level2);	
		switch (case_no)
					{					
					 case 1: tax = rate1 * income; break;
					 case 2: tax = married_tax1 + rate2 * (income-married_level1);break;
					 case 3: tax = married_tax2 + rate3 * (income - married_level2);break;
					}			
				


Am I correctly using the switch? any feedback gladly appreciated.
are you sure that in_marital_status is a string??
if not. you need to use ' ' not " " .. because
" " << this one is for string
' ' <<this one is for char

oh.. and what's with the case_no
Last edited on
Your code is harder to read with multiple statements on 1 line.

The case_no assignment is the result of a conditional so will be 0 or 1.
To include more than one statement in the if statement you have to use curly brackets.

1
2
3
4
5
6
7
8
9
10
if (in_marital_status == "s")
{
	case_no= (income <= single_level1) || (income<= single_level2);	
	switch (case_no)
	{
		case 1:	(income <= single_level1); tax = rate1 * income; break;
		case 2: (income <= single_level2); tax = single_tax1 + rate2 * (income- single_level1);break;
		case 3: tax = single_tax2 + rate3 * (income-single_level2);break;
	}
}
Topic archived. No new replies allowed.