Homework Question-While Loops

Hello! The homework question is this one: write a program tht can be used to calculate the federal tax. The tax calculated as follows: For single peopl, the standard exemption is $4000; for married people, the standard exemption is $7000. A person can also put up to 6% of his or her gross standard income in a pension plan. The tax rates are as follows: If the the taxable income is

1) Between $0 and $15000, the tax rate is 15%
2)between $1500 and $40000, the tax is $2250 plus 25% of the taxable income over $1500
3)Over $40,000, the tax is $8460 plus 35% of the taxable income over $40000
Prompt the user to enter the following information:
4)Martial status
5)If the marital status is "married", ask for the number of children under the age of 14
6)Gross salary(if the martial status is "married" and both spouses have income, enter the combined salary.)
7)percentage of gross income contributed to a pension fund.

The program must consist of at leat the following functions:
a) function getData: this function asks the user to enter the relevant data.
b) function taxAmount: this function computes and returns the tax owned.

To calculate the taxable income, subtract the sum of the standard exemption, the amount contributed to a pension plan, and the personal exemption, which is $1500 per person.

And this 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
134
135
136
137
138
139
140
141
142
143
144
145
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <process.h>

double taxAmount(double salary, double kid, double pension, double status);
void getData();

using namespace std;

int main()
{
	cout << "This is a program to calculate your federal taxes." << endl;

	cout << "Welcome! Please enter in the required information!" << endl;

	getData();

	system("pause");
	return 0;

}

void getData()
{
	char martialStatus;
	char pensionChoice;
	double grossSalary;
	double pensionPercentage;
	double child;
	double mstatusNum;

	cout << "What is your Martial Status?" << endl;
	cout << "Enter S/s for Single, or M/m for Married: " << endl;
	cin >> martialStatus;
 
	if (martialStatus == 'S' || martialStatus == 's')
	{
		mstatusNum = 1;

		cout << "Please enter in your salary: " << endl;
		cin >> grossSalary;

		cout << "Enter in number of kids under the age of 14: " << endl;
		cin >> child;

		cout << "Would you like to contribute up to 6% of your Salary to a Pension Plan?" << endl;
		cout << "Enter Y/y for yes or N/n for No: " << endl;
		cin >> pensionChoice;

		if (pensionChoice == 'Y' || pensionChoice == 'y')
		{
			cout << "Enter in a number between 1-6 of how high: ";
			cin >> pensionPercentage;

			while (pensionPercentage < 1 || pensionPercentage > 6)
			{
				cout << "That is not the correct number, please enter in a number between 1-6: " << endl;
				cin >> pensionPercentage;
			}
		}
		else
		{
			pensionPercentage = 0;
		}

		cout << "We will now calculate your federal taxes..." << endl;
		cout << "Your taxes are: " << taxAmount(grossSalary, child, pensionPercentage, mstatusNum) << endl;

	}
	else
	{
		mstatusNum = 2;

		cout << "Please enter in the combined salary of you and your spouse: " << endl;
		cin >> grossSalary;

		cout << "Enter in number of kids under the age of 14: " << endl;
		cin >> child;

		cout << "Would you like to contribute up to 6% of your Salary to a Pension Plan?" << endl;
		cout << "Enter Y/y for yes or N/n for No: " << endl;
		cin >> pensionChoice;


		if (pensionChoice == 'Y' || pensionChoice == 'y')
		{
			cout << "Enter in a number between 1-6 of how high: ";
			cin >> pensionPercentage;

			while (pensionPercentage < 1 || pensionPercentage > 6)
			{
				cout << "That is not the correct number, please enter in a number between 1-6: " << endl;
				cin >> pensionPercentage;
			}
		}
		else
		{
			pensionPercentage = 0;
		}

		cout << "We will now calculate your federal taxes..." << endl;
		cout << "Your taxes are: " << taxAmount(grossSalary, child, pensionPercentage, mstatusNum) << endl;
	}
}

double taxAmount(double salary, double kid, double pension, double status)
{
	double const sinExem = 4000.00;
	double const marExem = 7500.00;
	double const rate1 = 0.15;
	double const rate2 = 0.25;
	double const rate3 = 0.35;
	double const tax1 = 2250.00;
	double const tax2 = 8460.00;
	double federalTaxes;
	double penPercent = pension / 100;
	double taxableAmount;
	double penTotal = salary * penPercent;

	if (status == 1)
	{
		taxableAmount = salary - (1500.00 * kid) - penTotal - sinExem;
	}
	else if (status == 2)
	{
		taxableAmount = salary - (1500.00 * kid) - penTotal - marExem;
	}

	if (taxableAmount >= 0 || taxableAmount <= 15000)
	{
		federalTaxes = taxableAmount * rate1;
	}
	else if (taxableAmount >= 15001 || taxableAmount <= 40000)
	{
		federalTaxes = (taxableAmount * rate2) + tax1;
	}
	else if (taxableAmount >= 40001)
	{
		federalTaxes = (taxableAmount * rate3) + tax2;
	}

	return federalTaxes;

}


While this is running and I am getting the answers I am wanting I was having issues before doing any user input error check to make sure the user input valid characters such as 'M' or 'S' for married or 'Y' or 'N' for if they want to calculate their pension, and allowing them to re-enter data. The structure of the while loop was as such:

1
2
3
4
5
6
while (pensionChoice != 'Y' || pensionChoice != 'y' || pensionChoice != 'N' || pensionChoice != 'n')
{
cout << "That is not a valid answer, please enter again: " << endl;
	cin >> pensionChoice;
}
		


typically what happens is if I enter in any of the correct input, it keeps executing that while loop the one for married has a similar structure. Any help appreciated!
There's something wrong with your answer checking.
Try this:
1
2
3
4
5
do {
	cout << "What is your Martial Status?" << endl;
	cout << "Enter S/s for Single, or M/m for Married: " << endl;
	cin >> martialStatus;
} while(martialStatus != 'S' && martialStatus != 's' && martialStatus != 'M' && martialStatus != 'm');

or, if you want it to say something like "That is not a valid input":
1
2
3
4
5
6
7
8
9
10
	cout << "What is your Martial Status?" << endl;
	cout << "Enter S/s for Single, or M/m for Married: " << endl;
	cin >> martialStatus;
	while(martialStatus != 'S' && martialStatus != 's' && martialStatus != 'M' && martialStatus != 'm') {
		cout >> "That is not a valid input. Please enter again: " << endl;
		//The following lines will clear the stream buffer, in case the user entered more than one character
		cin.clear();
		cin.ignore(255, '\n');
		cin >> martialStatus;
	}

If you are confused why AND is used instead of OR, or what the stream buffer is, take a look at this post:
http://www.cplusplus.com/forum/beginner/189400/
Last edited on
Hi,

I have a psychotic hatred of constructs like this:

while(martialStatus != 'S' && martialStatus != 's' && martialStatus != 'M' && martialStatus != 'm') {

IMO, they are UGLY, error prone and non scalable.

First, make use of the toupper() or tolower() functions to convert the char into either upper or lower case, this halves the logic right from the start.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bool ValidData = false;

while(!ValidData) {
        std::cout << "What is your Martial Status?\n";
	std::cout << "Enter S/s for Single, or M/m for Married: \n";
        cin >> martialStatus;

        char martialStatusUC = toupper(martialStatus); 

        if(martialStatusUC == 'S') {
              ValidData = true;
              ProcessSingleStatus();  // function call       
        }
        else if (martialStatusUC == 'M') {
              ValidData = true;
              ProcessMarriedStatus();   // function call    
        }
        else {
               ValidData = false;
                std::cout << "Invalid Option try again\n ";
        }
}


My code is longer, but much easier to read and maintain or extend. If more options are needed, consider using a switch with a default case. Or you could use a switch anyway.

Good Luck !!
@TheIdeasMan

I agree, those constructs are quite ugly. However, I'm not so sure that just tolower-ing or introducing a switch would help extensibility.

I' tend try and avoid branching (be it in switches or ifs) all together if possible for this sort of thing. I find the command pattern can be pretty helpful.

Super-simplified example involving map and basic command calls: https://ideone.com/MwvxbM
iHutch105 wrote:
I find the command pattern can be pretty helpful.


I agree, I have known how to use a container of function pointers for awhile now. It's just that I thought it would be probably too hard for a beginner. Perhaps I should have mentioned that :+)
Sure, probably is out of scope. I sort've derailed this one a bit. :-)
Thank for the help!
Topic archived. No new replies allowed.