My Program

Little help. My program works flawlessly except it wont perform the calculation when "m" for married is entered.
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main ()
{

	bool more_records = true;
	bool marital_status_ok = true;
	bool tax_income_ok = true;
	const double single_level1 = 21450.00;
	const double single_level2 = 51900.00;
	const double single_tax1 = 3217.50;
	const double single_tax2 = 11743.50;
	const double married_level1 = 35800.00;
	const double married_level2 = 86500.00;
	const double married_tax1 = 5370.00;
	const double married_tax2 = 19566.00;
	const double rate1 = 0.15;
	const double rate2 = 0.28;
	const double rate3 = 0.31;
	double income;
	double tax;
	int case_no=0;
	int married_case_no=0;
		
	
		cout << "Please enter the last name:";
		string last_name;
		cin >> last_name;
		if (last_name == "99")
			more_records=false;
	
	while (more_records == true) //if the data input is a real last name and not “99”
	{
		cout << "Please enter the first name:";
		string first_name;
		cin >> first_name;
	
		cout << "Please enter your social security number:";
		string social_security;
		cin >> social_security;
		
		marital_status_ok=false;
		cout << "Please enter s for single, m for married: ";
			string in_marital_status;
			cin >> in_marital_status;
		while (marital_status_ok==false)
			{	
			  if	(in_marital_status == "m" || in_marital_status == "s")
      					marital_status_ok = true ;
			  else
				{
					cout << "m or s only, please try again ";
					cout << "Please enter s for single, m for married: ";
					cin>> in_marital_status;
				}
			}	
		
			tax_income_ok= false;
				cout << "Please enter income: ";
				cin >> income;
		while (tax_income_ok==false) 
			{
				if (income=income)
				tax_income_ok = true ;
			else 
				{
				cout << "You have entered an invalid entry, please try again"<<endl;
				cout << "Please enter income: ";
				cin >> income;
				}
			}
			
	
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;
					}			
}				
	cout << "\nFull Name: " << first_name <<" " << last_name<< endl;  
	cout << "Your Social Security is: " << social_security<< endl;
	cout << "Your tax amount is: " << tax<< endl;
	
	cout << "Enter the next Last Name: ";
			cin >>last_name;
			if (last_name == "99")
			more_records==false;
	}	
	cout << "Goodbye!";
	cout << endl;

}//close program 


any suggestions??
Last edited on
Do you mean the calculation of tax? The way you calculate case_no looks suspicious. Are you sure that case_no is not 0?
Yes the calculation of tax. The single "s" calculation works flawlessly however when "m" is input the tax value is not determined. i am debating if and else statements.
http://cplusplus.com/doc/tutorial/control/
switch compares for equality.
1
2
case 1:
	(income <= single_level1);//statement has no effect 


PS: more_records == true ¿why do you do that? more_records is a boolean.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
if (in_marital_status == "s")
{	
		case_no=(income <= single_level1) || (income<= single_level2);	
		switch (case_no)
				{
					case 1:	tax = rate1 * income; break;
					case 2: tax = single_tax1 + rate2 * (income- single_level1);break;
					case 3: tax = single_tax2 + rate3 * (income-single_level2);break;
					
				}	
}		
else (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;
					}			
}				


perhaps i have figured it out. adding the else!
You're using switches incorrectly. Let me add some comments so you understand:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// If in_marital_status is equal to "s"
if (in_marital_status == "s")
{	
                // case_no is assigned either true/false (or 1/0) depending on
                // the result of the following expression
		case_no=(income <= single_level1) || (income<= single_level2);	
                // You're placing case_no into a switch
		switch (case_no)
				{
                                        // Same as writing
                                        // if (case_no == 1)
					case 1:	tax = rate1 * income; break;
                                        // else if (case_no == 2)
					case 2: tax = single_tax1 + rate2 * (income- single_level1);break;
                                        // else if (case_no == 3)
					case 3: tax = single_tax2 + rate3 * (income-single_level2);break;
					
				}	
}


The biggest issue seems to be I have no clue what you're trying to assign case_no as, and the computer is taking a guess. You never want the computer to guess at what your code means (actually, it's doing exactly what you told it to do, but it's not what you want it to do).

What is supposed to happen during your switch statements? What is case_no supposed to represent? It might be easier for you to write it out as if statements first and then try to convert it to a switch. Also, switches are designed for constant values only, IIRC, so you can't quite use it as efficiently as an if condition.
Last edited on
further more looking at it. I did do it wrong. However I am no stuck on what the variable is. for the switch i understand a expression is needed. income is incorrect.
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
if (in_marital_status == "s")
{	
			
		switch (income)
				{
					case 1:	(income <= single_level1);
								tax = rate1 * income; break;
					case 2: (income <= single_level2);
								tax = single_tax1 + rate2 * (income- single_level1);break;
					default: tax = single_tax2 + rate3 * (income-single_level2);break;
					
				}	
}		
else (in_marital_status == "m");
{		
			
		switch (income)
					{					
					 case 1: (income <= married_level1);
								tax = rate1 * income;
								break;
					 case 2: (income <= married_level2);
								tax = married_tax1 + rate2 * (income-married_level1);
								break;
					 default: tax = married_tax2 + rate3 * (income - married_level2);
								break;
					}			
}				
You're missing the concept of switches. You're trying to use it like an if statement, and, to an extent, they are. Using the above code, you're doing this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if (in_marital_status == "s")
{	
			
		switch (income)
				{
                                        // if (income == 1)
					case 1:
                                                                // This statement does nothing
                                                                (income <= single_level1);
								tax = rate1 * income; break;
					case 2: (income <= single_level2);
								tax = single_tax1 + rate2 * (income- single_level1);break;
					default: tax = single_tax2 + rate3 * (income-single_level2);break;
					
				}	
}		


You're trying to check if the income is less than equal to single_level1, but switches don't work like that. In a case like that, you would want to use an if condition. Rewriting your above code to use switches, you want something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
switch(in_marital_status) {
   case "s":
      if (income <= single_level1)
         tax = rate * income;
      else if (income <= single_level2)
         tax = single_tax1 + rate2 * (income - single_level1);
      else
         tax = single_tax2 + rate3 * (income - single_level2);
      break;
   case "m":
      // ...
      break;
}
Last edited on
Thank you for this! Now i understand how to use switches.
Modified my code a bit but still wont give the proper output. Any ideas?
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
if (in_marital_status== "s")	//Single Marital Status		
	{	
		if (income <= single_level1)
				tax_bracket=1;
		else if (income <= single_level2)
					tax_bracket=2;
		else tax_bracket=3;
					
				switch(tax_bracket)
					{
						case 1:	tax= rate1 * income;break;
						case 2:	tax= single_tax1 + rate2*(income-single_level1);break;
						case 3: tax= single_tax2 + rate3 * (income-single_level2);break;
						
					}	
	}
else 	(in_marital_status== "m"); //Married Marital Status
	{					
				if (income <= married_level1)
				tax_bracket=1;
				else if (income <= married_level2)
				tax_bracket=2;
				else tax_bracket=3;		
					
				switch (tax_bracket)
						{
						case 1:	tax= rate1*income;
						case 2:	tax= married_tax1+rate2*(income-married_level1);
						case 3: tax= married_tax2 + rate3*(income-married_level2);
						break;	
						}
Last edited on
Because line 17 isn't doing what you expect it to do. Let me show you what I see (and similarly, what the compiler recognizes it as):
1
2
3
4
5
6
7
8
9
else
   // This line won't do anything
   (in_marital_status== "m"); //Married Marital Status
// Everything down will be run every time
{					
   if (income <= married_level1)
   tax_bracket=1;
   // ...
}


I believe, instead, what you wanted was to change the else to an else if:
1
2
3
4
5
else if (in_marital_status== "m"); //Married Marital Status
	{					
				if (income <= married_level1)
				tax_bracket=1;
	}


Hope this helps.
Volatile Pulse again thanks! that helps although it still wont give me the correct output. Here is my whole code.
The things I have trouble with are:

the while loop on income wont work if a letter is inputted.
the calculation wont work
if "99" is entered in last name it wont correctly exit.

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main ()
{

	bool more_records = true;
	bool marital_status_ok = true;
	bool tax_income_ok = true;
	const double single_level1 = 21450.00;
	const double single_level2 = 51900.00;
	const double single_tax1 = 3217.50;
	const double single_tax2 = 11743.50;
	const double married_level1 = 35800.00;
	const double married_level2 = 86500.00;
	const double married_tax1 = 5370.00;
	const double married_tax2 = 19566.00;
	const double rate1 = 0.15;
	const double rate2 = 0.28;
	const double rate3 = 0.31;
	double income;
	int case_no=0;
	double tax;
	int married_case_no=0;
	int tax_bracket=0;	
	
		cout << "Please enter the last name:";
		string last_name;
		cin >> last_name;
		if (last_name == "99")
			more_records=false;
	
while (more_records = true) //if the data input is a real last name and not “99”
	{
		cout << "Please enter the first name:";
		string first_name;
		cin >> first_name;
	
		cout << "Please enter your social security number:";
		string social_security;
		cin >> social_security;
		
		marital_status_ok=false;
		cout << "Please enter s for single, m for married: ";
			string in_marital_status;
			cin >> in_marital_status;
		while (marital_status_ok==false)
			{	
			  if	(in_marital_status == "m" || in_marital_status == "s")
      					marital_status_ok = true ;
			  else
				{
					cout << "m or s only, please try again ";
					cout << "Please enter s for single, m for married: ";
					cin>> in_marital_status;
				}
				
			}
			tax_income_ok=false;
				cout << "Please enter income: ";
				cin >> income;
		while (tax_income_ok==false) 
			{
			if		(income >= 0)
					tax_income_ok = true ;
			else 
				{
					cout << "You have entered an invalid entry, please try again";
					cout << "Please enter income: ";
					cin >> income;
				}
			}		

		
if (in_marital_status== "s")	//Single Marital Status		
	{	
		if (income <= single_level1)
				tax_bracket=1;
		else if (income <= single_level2)
					tax_bracket=2;
		else tax_bracket=3;
					
				switch(tax_bracket)
					{
						case 1:	tax= rate1 * income;break;
						case 2:	tax= single_tax1 + rate2*(income-single_level1);break;
						case 3: tax= single_tax2 + rate3 * (income-single_level2);break;
						
					}	
	}
else if	(in_marital_status== "m"); //Married Marital Status
	{					
				if (income <= married_level1)
				tax_bracket=1;
				else if (income <= married_level2)
				tax_bracket=2;
				else tax_bracket=3;		
					
				switch (tax_bracket)
						{
						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;
						
						}

	}	
			
	cout << "\nFull Name: " << first_name <<" " << last_name<< endl;  
	cout << "Your Social Security is: " << social_security<< endl;
	cout << "Your tax amount is: " << tax<< endl;
	
	cout << "Enter the next Last Name: ";
			cin >>last_name;
			if (last_name == "99")
			more_records=false;
			cout<< "Goodbye!";
		
}// close BIG LOOP!
	
cout << "Goodbye!";
cout << endl;
	return 0;

		
}//close program

Hate being stuck on this code and its kicking my butt.
^ You've got a semicolon at the end.

@OP: It seems that you are using switch just because you want to use it.

Edit
while (more_records = true) you are assigning there.
I'm not sure what you were trying to do there.
Last edited on
edited
(more_records == true)

although which semicolon at the end is wrong
return 0;?
Last edited on
Good catch ne555, I missed that twice.

@rynzor6
You need to look into a code beautifier. It's hard to read the indentations. Otherwise, I'd suggest switching all your spaces to tabs or tabs to spaces. MSVC++ (I believe that's what you're using) should have a replace feature that should make going from what you have to one of the aforementioned indentation styles very quick.

Otherwise, I hope that solves your issues.

Edit: Also line 117's if statement only applies to line 118. You will see "Goodbye!" every time, even if you don't type 99 in for the last name.

To make it not do this, you need to use brackets to signify a code block:
1
2
3
			if (last_name == "99") {
			more_records=false;
			cout<< "Goodbye!";}


Edit2: The semicolon at line 93 in your above code, the else if that I told you to fix. I missed the semi colon at the end, that was my fault.
Last edited on
Many Thanks to @ne555 and @volatile pulse! A single semi colon has cost me headaches. I appreciate the assist.
Topic archived. No new replies allowed.