I am to write a program that can be used to calculate the federal tax. The tax is calculated as follows: For single people, the standard exemption is \$4,000; for married people, the standard exemption is \$7,000. A person can also put up to 6% of his or her gross income in a pension plan. The tax rates are as follows: IF the taxable income is

- Between \$0 & \$15,000, the tax rate is 15%
- Between \$15,001 and \$40,000, the tax is \$2,250 plus 25% of the taxable income over \$15,000
- Over \$40,000, the tax is \$8,460 plus 35% of the taxable income over \$40,000

The user should enter:
- Marital Status
- If the martial status is "married" ask for the number of children under the age of 14
- Gross salary (If the marital status is "married" and both spouses have income, enter the combined salary
- Percentage of gross income contributed to a pension fund.

The program must consist of at least 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 owed

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

SO far the code I have is:

``````#include <iostream>
#include <string>

using namespace std;
int getNumChildren();
double taxAmount(int,double,double,int);

int main ()
{
void getData(); // function prototype

getData();  // function call

return 0;
}
void getData()
{

int noofChildren;
double salary, amtInPension;
int numPerson, standardExemption;
double tax;

cin >> status;
cout << endl;

if  (status == 'm' || 'M')
{
noofChildren = getNumChildren();

cout << "Do both spouses earn income? Please enter [Y]es or [N]o. ";
cout << endl;

if (answer == 'y' || 'Y')
{
cin >> salary;
cout << endl;
}

else if (answer == 'n' || 'N')
{
cout << "Please enter salary: ";
cin>> salary;
cout<< endl;
}

numPerson = 2 + noofChildren;
}
else
{
cin >> salary;
cout << endl;

numPerson = 1;
}

cin >> amtInPension;
cout << endl;

tax = taxAmount(numPerson, salary, amtInPension, standardExemption);
}

int getNumChildren()
{
int children;

cout << "Please enter number of Children under the age of 14: ";
cin >> children;
cout << endl;

return children;
}

double taxAmount(int numPerson, double salary, double amtInPension, int standardExemption)
{
double taxableIncome;

taxableIncome = salary - (1500.00 * numPerson) - amtInPension - standardExemption;
return(taxableIncome);
}``````

I've been having problems with the If/Else statements. Whenever I choose Married & Yes it works perfectly but whenever I choose Married & no it still says "Please enter combined salary" and I can't get the Single side to work period. It still outputs everything for the married side. (I hope this makes sense) And I don't think the function for taxAmount is right. I don't know where to begin with it though. Any help would be appreciated!!!!

-Amanda

## All 20 Replies

> if (status == 'm' || 'M')
Should be `if (status == 'm' || status == 'M')`

great!! I didn't think that would have mattered but I got everything working! NOW I just need help with the function taxAmount calculation!

Shouldn't the function prototype be above the int main()?

> Shouldn't the function prototype be above the int main()?
It's legal to do that, but you might argue over the usefulness of doing such a thing.

> Shouldn't the function prototype be above the int main()?
It's legal to do that, but you might argue over the usefulness of doing such a thing.

I didn't know it could be done any other way, my teacher told our class that it could only be put outside the main function.

It works either way, but putting the proto in the function IMO just clutters up the function.

but people you aren't focusing on the task at hand!! I need help with my function taxAmount!

but people you aren't focusing on the task at hand!! I need help with my function taxAmount!

Sorry.

great!! I didn't think that would have mattered but I got everything working! NOW I just need help with the function taxAmount calculation!

Ahh, that's why. Help with what? Details, please...

I need help with the function taxAmount in the program above at the very top! sorry about that

I don't see where you initialize standardExemption before you try to use it in calculation of taxableIncome.

Once you have taxableIncome you need to subject it to the pertinent tax rate given in the instructions before returning the tax as taxableIncome isn't taxed at 100% (yet, anyway). You can use a series of if statements to determine the tax rate similar to what you did to determine the salary.

I don't see where you initialize standardExemption before you try to use it in calculation of taxableIncome.

Once you have taxableIncome you need to subject it to the pertinent tax rate given in the instructions before returning the tax as taxableIncome isn't taxed at 100% (yet, anyway). You can use a series of if statements to determine the tax rate similar to what you did to determine the salary.

You're right! But my question is now because it's \$4000 for single & \$7000 for married should I have 2 variables one for single/ one for married or how would the program know which exemption amount to use??

Still working on it... lol

``````taxableIncome = salary - (1500.00 * numPerson) - amtInPension - standardExemption;

double taxableIncome;
int standardExemption;

if (tax >= 0 || tax <= 15000)

taxableIncome * .15

else if (tax >= 15001 || tax <= 40000)

taxableIncome - 2250

else if (tax > 40000)

taxableIncome - 8460

return(taxableIncome);
}``````

I never was very good at math so I'm not sure if I got this here right. I'm not sure how to do this part of the program:

-Between 15,001 & 40,000 the tax is 2,250 plus 25% of the taxable income over 15,000

- Over 40,000, the tax is 8,460 plus 35% of the taxable income over 40,000

Other then that, does this look right???

As far as I can see, ccording to the instructions in the first post the only place the importance of single vs married comes in is in the calculation (besides combining income if married) is in determining the standard exemption. Once you have the taxable income, the actual tax rate is the same.

>>Other then that, does this look right???

Once you have taxableIncome I'd do something like this----note this is in pseudocode only. It starts with your base code snippet, as you can see.

``````if (tax >= 0 || tax <= 15000)
taxableIncome *= .15
else if (tax >= 15001 || tax <= 40000)
marginalIncome = taxableIncome - 15000;
marginTax = .25 * taxableIncome.
baseTax = 2250;
totalTax = baseTax + marginTax
else if (tax > 40000)
similar to above``````

You might also want to look at the way you handle the pension deduction. I'd do it a bit different based on the instructions posted. I'd ask them to input % of total income they want placed in pension. Calculate the pension deduction using the % input and total income and then subtract it from total income.

does this look correct then?

``````double taxAmount(int numPerson, double salary, double amtInPension, int standardExemption)
{
double taxableIncome, marginalIncome, marginTax;
int standardExemption;
int tax;
int baseTax, totalTax;

taxableIncome = salary - (1500.00 * numPerson) - amtInPension - standardExemption;

if (tax >= 0 || tax <= 15000)

taxableIncome *= .15

else if (tax >= 15001 || tax <= 40000)

marginalIncome = taxableIncome - 15000;
marginTax = .25 * taxableIncome;
baseTax = 2250;
totalTax = baseTax + marginTax;

else if (tax > 40000)

marginalIncome = taxableIncome - 40000;
marginTax = .35 * taxableIncome;
baseTax = 8460;
totalTax = baseTax + marginTax;

return(totalTax);
}``````

Also as far as the pension deduction how would I get the percentage from the other function into the function taxAmount?

whenever I try to run the program it give me a complier error saying illegal else w/o matching if? Any ideas on that? Also whenever I have

``int standardExemption``

it says: redefinition of formal parameter 'standardExemption'

It seems you overlooked my note that I posted pseudocode. Pseudocode means you leave out details, sometimes lots, sometimes a little. Often the details left out are syntax related details since they are so pickyunish. Pretend you're debugging my code by entering the appropriate details to my pseudocode before you try to run it in your program. If the code you posted in the first post was actually yours, much of the "debugging". like appropriate use of {}s, shouldn't be too hard.

If you're going to return totalTax, which I think is appropriate, be sure you have a value for totalTax for the under 15000 dollar case, too.

Since you pass standardExemption to taxAmount() don't declare another variable usinig the same name from within taxAmount(). If you do, how's the compiler going to know which version of standardExemption you're refering to? If your compiler flags this as a redefinition error, consider yourself lucky, because if might have considered it legal and used one version of standardExemption on time and the other version another, creating a difficult bug to find since it might appear inconsistent.

I wouldn't pass the % of salary deducted as pension to taxAmount() in your program version(though you could in other program versions). I'd get the % deduction and calculate the total pension deduction (what you called amtInPension) back in main() and pass amtInPension to taxAmount() as you have coded in the prototype.

If the code you posted in the first post was actually yours, much of the "debugging". like appropriate use of {}s, shouldn't be too hard.

Yes it's my code. Granted I had some help with it. It was MY code. and here is MY code again.

``````#include <iostream>
#include <string>

using namespace std;
int getNumChildren();
double taxAmount(int,double,double,int);

int main ()
{
int numPerson, standardExemption, tax;
double salary, amtInPension, amtdeducted;

void getData(); // function prototype

getData();  // function call

amtdeducted = salary - amtInPension;
tax = taxAmount(numPerson, salary, amtInPension,standardExemption);

cout << "The tax is: " << tax << endl;

return 0;
}
void getData()
{

int noofChildren;
double salary, amtInPension, amtdeducted;
int numPerson, standardExemption;
double tax;

cin >> status;
cout << endl;

if  (status == 'm' || status == 'M')
{
noofChildren = getNumChildren();

cout << "Do both spouses earn income? Please enter [Y]es or [N]o. ";
cout << endl;

{
cin >> salary;
cout << endl;
}

{
cout << "Please enter salary: ";
cin>> salary;
cout<< endl;
}

numPerson = 2 + noofChildren;
}
else
{
cin >> salary;
cout << endl;

numPerson = 1;
}

cout << "Please enter amount that you want to contribute to the Pension Plan: ";
cin >> amtInPension;
cout << endl;

tax = taxAmount(numPerson, salary, amtdeducted, standardExemption);
}

int getNumChildren()
{
int children;

cout << "Please enter number of Children under the age of 14: ";
cin >> children;
cout << endl;

return children;
}

double taxAmount(int numPerson, double salary, double amtdeducted, int standardExemption)
{
double taxableIncome, marginalIncome, marginTax;
int tax;
int baseTax, totalTax;

taxableIncome = salary - (1500.00 * numPerson) - amtdeducted - standardExemption;

if (tax >= 0 || tax <= 15000)
{
marginTax =.15 * taxableIncome;
totalTax = marginTax;
}
else if (tax >= 15001 || tax <= 40000)
{
marginalIncome = taxableIncome - 15000;
marginTax = .25 * taxableIncome;
baseTax = 2250;
totalTax = baseTax + marginTax;
}
else if (tax > 40000)
{
marginalIncome = taxableIncome - 40000;
marginTax = .35 * taxableIncome;
baseTax = 8460;
totalTax = baseTax + marginTax;
}

return(totalTax);
}``````

I am not getting the right answer. Reasonably so I think. I have no idea if I done the Pension amt correct

``````int main ()
{
int numPerson, standardExemption, tax;
double salary, amtInPension, amtdeducted;

void getData(); // function prototype

getData();  // function call

amtdeducted = salary - amtInPension;
tax = taxAmount(numPerson, salary, amtInPension,standardExemption);

cout << "The tax is: " << tax << endl;

return 0;``````

but is this what you were talking about for the first if?

``````if (tax >= 0 || tax <= 15000)
{
marginTax =.15 * taxableIncome;
totalTax = marginTax;
}
else if (tax >= 15001 || tax <= 40000)
{
marginalIncome = taxableIncome - 15000;
marginTax = .25 * taxableIncome;
baseTax = 2250;
totalTax = baseTax + marginTax;
}
else if (tax > 40000)
{
marginalIncome = taxableIncome - 40000;
marginTax = .35 * taxableIncome;
baseTax = 8460;
totalTax = baseTax + marginTax;
}

return(totalTax);``````

THANKS for the help so far. :) :confused:

Yes, that's what I was talking about. The {} are appropriate and the variable names used and methods of calculation are consistent. Very good.

Now, back in main() after asking for amtInPension with this:

cout << "Please enter amount that you want to contribute to the Pension Plan: ";
cin >> amtInPension;

If amtInPension is the % of salary to deduct as pension, then change that % into a decimal version (1% = 0.01, etc) and multiply the decimal version and salary together to get amtdeducted. By the way, personally I would use a different name than amtInPension, because to me amount would be total value. I think a name like percentInPension would help a lot in describing what the information is, but naming conventions are quite personal so you're free to use whatever name you want, legally.

In your latest post I still don't see where you give the variable called standardExemption a value before you subtract it from salary when calculating taxableIncome. Therefore the value of standardExemption will be junk and that means taxableIncome is going to be junk, even if you do subtract the correct pension amount from salary.

Finally, it should be--- if taxableIncome is less than 15000 dollars and if taxableIncome is between 15001 and 40000 dollars etc, not if tax is less than 15000, etc.

``````void getData()
{

int noofChildren;
double salary, amtInPension, amtdeducted;
int numPerson, standardExemption;
double tax;

cin >> status;
cout << endl;

if  (status == 'm' || status == 'M')
{
noofChildren = getNumChildren();
standardExemption = 7000;

cout << "Do both spouses earn income? Please enter [Y]es or [N]o. ";
cout << endl;

{
cin >> salary;
cout << endl;
}

{
cout << "Please enter salary: ";
cin>> salary;
cout<< endl;
}

numPerson = 2 + noofChildren;
}
else
{
cin >> salary;
cout << endl;

standardExemption = 4000;
numPerson = 1;
}

cout << "Please enter amount that you want to contribute to the Pension Plan: ";
cin >> amtInPension;
cout << endl;

tax = taxAmount(numPerson, salary, amtdeducted, standardExemption);
}``````

for amtInpension HOW do I set it as a percentage?? I'm still so confused by that. But above will that work for Standardexemption if they take the married path standardexemption is 7000 & then if not it's 4000? I think that should work but I don't know for sure??

A percentage (0% to 100%) is a value from 0 to 1. So:
0% = 0.00
10% = 0.10
25% = 0.25
100% = 1.00

From that you should be able to figure out how to convert your input percent to internal value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.