Federal Tax Calculator Program

I am doing an assignment that is to calculate the federal tax based on the users input. I am not entirely sure what it is doing, but it is definitely not what I want it to do. I commented out the loops because they seemed to have caused a problem, but there is still problems that are making the program do something else. Here is my code:

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
130
131
132
133
#include<iostream>
#include<string>

using namespace std;

double getData();
double taxAmount();
int main()
{
	cout << "The amount of Federal Tax you owe, based on the information you provided is: " << taxAmount();
   return 0;
}

double getData()
{
	char status, contribute;
	int child, Mstd = 7000, Sstd = 4000, Pexemp;
	double gross, percent = 0, grossPay = 0, taxIncome = 0;

	cout << "Enter M for MARRIED or S for SINGLE.\n";
	cin >> status;
	system("cls");

	/*while ((status != 'M') || (status != 'S'))
	{
		cout << "Invalid Selection. Please try again.\n";
		cout << "Enter M for MARRIED or S for SINGLE.\n";
		cin >> status;
		system("cls");
	}*/

		if (status == 'M')
		{
			cout << "Your Marital Status is MARRIED.\n";
			cout << "How many children live with you under the age of 14?\n";
			cin >> child;

			cout << "Enter your gross salary. If you and your spouse have income, combine together and enter it here.\n";
			cin >> gross;
		
			cout << "Would you like to contribute to your pension fund? Enter Y for YES or N for NO\n";
			cin >> contribute;
			system("cls");
				
				/*while ((contribute != 'Y') || (contribute != 'N'))
				{
					cout << "Invalid Selection. Please try again.\n";
					cout << "Would you like to contribute to your pension fund? Enter Y for YES or N for NO\n";
					cin >> contribute;
					system("cls");
				}*/

					if (contribute == 'Y')
					{
						cout << "You may contribute up to 6% of your gross income to your pension plan.\n";
						cout << "What percentage would you like to contribute?\n";
						cin >> percent;

						percent = percent / 100;
						grossPay = gross * percent;
						gross = gross - percent;
						Pexemp = (2 + child) * (1500);
						taxIncome = gross - Pexemp - Mstd;
					}
					else
						cout << "You chose not to contribute to your pension fund.\n";

					Pexemp = (2 + child) * (1500);
					taxIncome = gross - Pexemp - Mstd;
		}
		else
			cout << "Your Marital Status is SINGLE.\n";
			cout << "Enter your gross salary.\n";
			cin >> gross;

			cout << "Would you like to contribute to your pension fund? Enter Y for YES or N for NO\n";
			cin >> contribute;
			system("cls");

				/*while ((contribute != 'Y') || (contribute != 'N'))
				{
					cout << "Invalid Selection. Please try again.\n";
					cout << "Would you like to contribute to your pension fund? Enter Y for YES or N for NO\n";
					cin >> contribute;
					system("cls");
				}*/

					if (contribute == 'Y')
					{
						cout << "You may contribute up to 6% of your gross income to your pension plan.\n";
						cout << "What percentage would you like to contribute?\n";
						cin >> percent;

						percent = percent / 100;
						grossPay = gross * percent;
						gross = gross - percent;
						taxIncome = gross - 1500 - Sstd;
					}
					else
						cout << "You chose not to contribute to your pension fund.\n";
					
					taxIncome = gross - 1500 - Sstd;

	return taxIncome;
}

double taxAmount()
{
	double taxRate, fedTax = 0, over, overtax;
	
	if ((getData() > 0) && (getData() <= 15000))
	{
		taxRate = .15;
		fedTax = getData() * taxRate;
	}

		if ((getData() > 15000) && (getData() <= 40000))
		{
			taxRate = .25;
			over = getData() - 15000;
			overtax = over * taxRate;
			fedTax = 2250 + overtax;
		}

			if (getData() > 40000)
			{
				taxRate = .35;
				over = getData() - 40000;
				overtax = over * taxRate;
				fedTax = overtax + 8460;
			}
	return fedTax;
}


Functions just seem like they are not my thing. lol
It never gets to the second function to do the calculations and then return to main to display the results.
I tried changing the code to a switch(status) with a default: at the end that looped the "please try again" but for some reason, that was a fail, it created an endless loop. Any help would be much appreciated.
Last edited on
First thing that struck me is that you keep calling the getData function in the taxAmount function. It seems like you'd only want to call that once - - perhaps create a variable to store the income results and then use that variable in the comparisons.

e.g.

1
2
	double income = getData();
	if (income > 0 && income <= 15000)
Let me see if I am understanding what you are saying correctly.

 
double income = getData();

use this instead of just the function double getData();?
and would this me declared globally or just locally in the double taxAmount()

then in the second function double taxAmount(); just use double income[code] in place of [code]double getData();?
I think I understand that and I'm sure that portion will run correctly, but it is not even getting that far. Here is what happens on the console:

Asks M or S.
M
Ask how many children.
2
Asks for income.
20000
Asks to contribute to pension.
N
Notifies me that I chose N. Then asks for income again.

Most everything I can figure out by myself, just for some reason I can not get a couple of my programs to work exactly right.
You only want to call the getData function once to ask for income. Not every place in the taxAmount function where you want to use the income amount.


double taxAmount()
{
double taxRate, fedTax = 0, over, overtax;

// add variable to store result of getData
double income = getData();

//replace calls to getData() with the variable that's storing the result
if ((getData() > 0) && (getData() <= 15000))
if (income > 0 && income <= 15000)
{
taxRate = .15;
fedTax = getData() income * taxRate;
}
// same thing for the rest of the if statements here


Also - on line 71 - I think you want curly braces {} around the whole block of code you want to run for the else case (single taxpayer)?
Last edited on
Understood. I just made those changes you suggested about calling getData() only once, but the program does not even get that far.
Can you post your updated code?

For your input validation:

while ((status != 'M') || (status != 'S'))

What you're saying here is while an M was not entered or while an S was not entered, ask the user to re-enter. Whatever the user enters, one of those conditions will always be the case.

Try this - if it is not the case that M or S was entered.
while (!(status == 'M' || status == 'S'))

If the person decides to contribute to the pension fund, you're calculating the dollar amount of their income that they want to contribute, but then you subtract percent from gross, instead of the grossPay you calculated.

For example, this person has gross income of 50000 and they chose to contribute 5% of their income.

1
2
3
4
5
cin >> percent; // percent = 5
percent = percent / 100; // percent = .05
grossPay = gross * percent; // grossPay = 50000 * .05 = 2500
gross = gross - percent; // gross = 50000 - .05
	
Last edited on
I apologize for the lateness of my reply and I truly appreciate your help. Would your while statement not be the same as my while? And your code you input in your last response, is that not what my code is doing? I'm not trying to be a smart alack, I'm just trying to see how your code is different from mine. Here is the updated code, although it is not much different from the original post.

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
130
131
132
133
134
135
136
137
138
139
140
141
/*
	Programmer Name: Anthony Dodd
	Date Assigned:
	Date Due:
	Program Description:
*/

#include<iostream>
#include<string>

using namespace std;

double getData();
double taxAmount();
int main()
{
	cout << "The amount of Federal Tax you owe, based on the information you provided is: " << taxAmount();
   return 0;
}

double getData()
{
	char status, contribute;
	int child, Mstd = 7000, Sstd = 4000, Pexemp;
	double gross, percent = 0, grossPay = 0, taxIncome = 0;

	cout << "Enter M for MARRIED or S for SINGLE.\n";
	cin >> status;
	system("cls");

	/*while ((status != 'M') || (status != 'S'))
	{
		cout << "Invalid Selection. Please try again.\n";
		cout << "Enter M for MARRIED or S for SINGLE.\n";
		cin >> status;
		system("cls");
	}*/

		if (status == 'M')
		{
			cout << "Your Marital Status is MARRIED.\n";
			cout << "How many children live with you under the age of 14?\n";
			cin >> child;

			cout << "Enter your gross salary. If you and your spouse have income, combine together and enter it here.\n";
			cin >> gross;
		
			cout << "Would you like to contribute to your pension fund? Enter Y for YES or N for NO\n";
			cin >> contribute;
			system("cls");
				
				/*while ((contribute != 'Y') || (contribute != 'N'))
				{
					cout << "Invalid Selection. Please try again.\n";
					cout << "Would you like to contribute to your pension fund? Enter Y for YES or N for NO\n";
					cin >> contribute;
					system("cls");
				}*/

					if (contribute == 'Y')
					{
						cout << "You may contribute up to 6% of your gross income to your pension plan.\n";
						cout << "What percentage would you like to contribute?\n";
						cin >> percent;

						percent = percent / 100;
						grossPay = gross * percent;
						gross = gross - percent;
						Pexemp = (2 + child) * (1500);
						taxIncome = gross - Pexemp - Mstd;
					}
					else
						cout << "You chose not to contribute to your pension fund.\n";

					Pexemp = (2 + child) * (1500);
					taxIncome = gross - Pexemp - Mstd;
		}
		else
			cout << "Your Marital Status is SINGLE.\n";
			cout << "Enter your gross salary.\n";
			cin >> gross;

			cout << "Would you like to contribute to your pension fund? Enter Y for YES or N for NO\n";
			cin >> contribute;
			system("cls");

				/*while ((contribute != 'Y') || (contribute != 'N'))
				{
					cout << "Invalid Selection. Please try again.\n";
					cout << "Would you like to contribute to your pension fund? Enter Y for YES or N for NO\n";
					cin >> contribute;
					system("cls");
				}*/

					if (contribute == 'Y')
					{
						cout << "You may contribute up to 6% of your gross income to your pension plan.\n";
						cout << "What percentage would you like to contribute?\n";
						cin >> percent;

						percent = percent / 100;
						grossPay = gross * percent;
						gross = gross - percent;
						taxIncome = gross - 1500 - Sstd;
					}
					else
						cout << "You chose not to contribute to your pension fund.\n";
					
					taxIncome = gross - 1500 - Sstd;

	return taxIncome;
}

double taxAmount()
{
	double taxRate, fedTax = 0, over, overtax;
	double income = getData();
	
	if ((income > 0) && (income <= 15000))
	{
		taxRate = .15;
		fedTax = income * taxRate;
	}

		if ((income > 15000) && (income <= 40000))
		{
			taxRate = .25;
			over = income - 15000;
			overtax = over * taxRate;
			fedTax = 2250 + overtax;
		}

			if (income > 40000)
			{
				taxRate = .35;
				over = income - 40000;
				overtax = over * taxRate;
				fedTax = overtax + 8460;
			}
	return fedTax;
}
And your code you input in your last response, is that not what my code is doing?


That is your code - - I was just saying that I don't think it's doing what you intend it to do. I think what you mean to do is calculate the pension contribution dollar amount and subtract that from the gross pay, so they aren't taxed on the amount they set aside in the pension plan. What the code is doing is subtracting the percent amount, in this case .05, from the gross amount. I think you mean to subtract the 2500?

1
2
3
4
cin >> percent; // percent = 5
percent = percent / 100; // percent = .05
grossPay = gross * percent; // grossPay = 50000 * .05 = 2500
gross = gross - percent; // gross = 50000 - .05 




while ((status != 'M') || (status != 'S'))

This is testing whether one of the conditions there is true. If they enter 'S' - the first condition, status !=M, is true. If they enter 'M', the second condition, status!='S', is true. If they put in 'H', both conditions are true. Whether they put in a valid option or invalid option, that logic is going to be true and they won't get out of the while loop.

while (!(status == 'M' || status == 'S'))
This is saying while the status is not equal to either 'M' or 'S', continue the loop.

Or, you can distribute the negative across the two conditions, but then you need to flip the logical OR (||) to a logical AND (&&) like this.
while ((status != 'M') && (status != 'S'))

DeMorgan's laws can be helpful in figuring this out. :)
http://en.wikipedia.org/wiki/De_Morgan's_laws
Ah... makes sense. Not sure why I did not catch that. I'm usually pretty good at being able to "talk" my way through the code and what it is doing as I read each line. As I was reading what you were saying about my code while ((status != 'M') || (status != 'S')) I said, out loud, "&& would be the right way to go!" lol. Simple mistakes that can destroy the code. As far as the calculations, I see what you mean. I THINK what I was thinking was that I was overwriting the percent value with the amount of the pension contribution value, when in fact I was only changing the value of percent to a decimal. Let me make a few changes and see what happens.
Alright then. Fixed the loops, and they work properly. On to the next issue. So this is what happens as a sample run.
M or S
M
Children under 14?
2
Gross Income?
50000
Pension Contribution?
Y
% to Contribute?
5
Gross Income?
50000
Pension Contribution?
Y
% to Contribute?
5
Your Owed Fed Tax is : 9160

It seems as though it goes through the married selection and then through the single selection even though it was not selected. Should the variables be different in the single and married selections?
Line 79 - 109 - that whole "else" section for single - you're missing curly braces around that block.
Good call! Fixed it. Works perfectly now. Truly appreciate all the help. I did not think it would matter if I had the curly braces around the else statement. I guess it just depends on what the else statement has in it. Thanks again.
If you have more than one statement as part of a block you want to run in an if/else or loop, you need the curly braces, otherwise just the first line is run subject to the if/else etc. condition.

Glad it's working for you now.
Understood. Maybe you could take a quick look at my other program I am working on. Here is the link. The others that are "helping" are actually arguing amongst themselves. http://www.cplusplus.com/forum/general/128593/
Topic archived. No new replies allowed.