Tax program

Having some problems getting this code to do what I want it to do. I can get it to perform some of it correctly. But the main problem is 2 fold. First I need to get the user information (fname, lname, ssn, marital status) and then enter their income and have it display the total taxes and then repeat until the sentinel value of 99 is entered at the last name prompt. Any ideas? I'm new so dont rip me one if I miss something blatantly obvious. But the problem I'm having is that it when entering the income it gets stuck asking the income until it crashes or I exit. Whats the issue I'm not seeing?

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

using namespace std;

int main()

{  

   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;

   float income;
   double tax = 0;
   string lastName;
   string firstName;
   string ssn;
   char marital_status;
   bool marital_status_ok;

   

	cout << "Please enter your last name: ";
	cin >> lastName;

		cout << "Please enter your first name: ";
		cin >> firstName;
		
		cout << "Please enter your Social Security Number (No spaces or dashes please): ";
		cin >> ssn;

		cout << "Please enter s for single, m for married: "; 
		cin >> marital_status;

		if (marital_status != 's', 'S', 'm','M')

			{		// start main marital status entry check
	
			marital_status_ok = false;
	
				if (marital_status_ok == true)

				{									// end outter if marital status true

					while (marital_status_ok = true)

					{								// start while for marital status
		
						while (marital_status == 's' || 'S')

						{							// start while S

						cout << "Please enter your income: ";		// single tax income set
						cin >> income;
	
							if (income <= SINGLE_LEVEL1)
							tax =  RATE1 * income;

							else if (income <= SINGLE_LEVEL2)
							tax = SINGLE_TAX1 
							+ RATE2 * (income - SINGLE_LEVEL1);

							else
							tax = SINGLE_TAX2 
							+ RATE3 * (income - SINGLE_LEVEL2);

						}							// end while S

						while (marital_status = 'm' || 'M');

						{							// start while M
			
						cout << "Please enter your income: ";		// married tax income set
						cin >> income;

							if (income <= MARRIED_LEVEL1)
							tax =  RATE1 * income;

							else if (income <= MARRIED_LEVEL2)
							tax = MARRIED_TAX1 
							+ RATE2 * (income - MARRIED_LEVEL1);

							else
							tax = MARRIED_TAX2 
							+ RATE3 * (income - MARRIED_LEVEL2);

						}		// end while M

						cout << "The tax is $" << tax << "\n";	// output tax amount

					}			// end while martial status true

				}				// end if marital status is true

			}					// end if marital status check != s,S,m,M

			else (marital_status_ok = false);

			{					// start error message

			cout <<"That is not a valid input. Please enter an S for single or an M for married.";

			}					// end error message

		system("PAUSE");			// pause screen

		return 1;

} // end main function


I havent set up the sentinel yet though. Just so you know
But the problem I'm having is that it when entering the income it gets stuck asking the income until it crashes or I exit


It doesn't appear that you're mixing formatted and unformatted input, so that isn't likely to be the problem. On the other hand, you never check or handle input operation failure, so that seems a likely source.

See http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.3 for an example of how to detect and handle input failure.
Ok thank you I will read up on that. There are a couple things I haven't got to yet. This is where I was at, thus far. Thank you though for the info though, I'll look into that.
Ok so I dumped the nested if/else and I'm setting it up as a switch statement. But I for some reason cant figure out how to get it to work. Also how would I set up the sentinel under last name, since last name is a string and the sentinel has to be an int 99 under last name. How do I tackle this. I think I am just thinking into it way too much.

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
//-----------------------------------------------------------------

#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
 
int main()
 
{  
 
double SINGLE_LEVEL1 = 21450.00;
double SINGLE_LEVEL2 = 51900.00;

double SINGLE_TAX1 = 3217.50;
double SINGLE_TAX2 = 11743.50;

double MARRIED_LEVEL1 = 35800.00;
double MARRIED_LEVEL2 = 86500.00;

double MARRIED_TAX1 = 5370.00;
double MARRIED_TAX2 = 19566.00;

double RATE1 = 0.15;
double RATE2 = 0.28;
double RATE3 = 0.31;
 
float income = 0;
double tax;
string lastName;
string firstName;
string ssn;
char marital_status;
bool marital_status_ok = true;
int sentinel = 99;

cout << "last name: ";
cin >> lastName;

cout << "first name: ";
cin >> firstName;
              
cout << "Social Security Number (No spaces or dashes please): ";
cin >> ssn;
 
cout << "s for single or m for married: "; 
cin >> marital_status;

while (marital_status == 's','S','m','M')
{

	if (marital_status_ok == true)
	{

		while (marital_status == 's' || 'S')
		{

		cout << "income: ";
		cin >> income;

		switch ('s')
			{

			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;
				
			default:
				cout << "invalid income amount";
			}

		}

		while (marital_status == 'm' || 'M')
		{

		cout << "income ";
		cin >> income;

			if (income <= MARRIED_LEVEL1)
				tax =  RATE1 * income;

			else if (income <= MARRIED_LEVEL2)
				tax = MARRIED_TAX1 + RATE2 * (income - MARRIED_LEVEL1);

			else
				tax = MARRIED_TAX2 + RATE3 * (income - MARRIED_LEVEL2);
		}

	}

else (marital_status_ok == false);
	{
	cout << "re-enter marital status";
	}

}

system("PAUSE");
 
return 0;
 
}
while (marital_status == 's','S','m','M')

Should be:

while (marital_status == 's' || marital_status == 'S' || marital_status == 'm' || marital_status == 'M')

You have the same issue at lines 56 and 83.


switch ('s')

's' is a constant whose value never changes, thus your switch will always execute the default case.




Ok so I fixed that issue. Its getting the income and calculating the tax but once the tax is done its asking for income again instead of going back to the beginning and starting over
Last edited on
Its like its stuck in the loop and Im not sure of how to get it out and keep it asking for another person to enter their info until the sentinel (99) found in the last name prompt exits
Ok got the 99 part. Now I need to figure out how to break out of the switch statement at the income prompt. :/ I can get the first input but it keeps looping
You never show updated code, so nobody really knows what you're talking about.
Topic archived. No new replies allowed.