1. ## C++ coding idea

I'm a beginner C++ coder having seen most of the code for the very first time over the past few weeks, just to give an idea of my expertise.

In my class we were given the problem to write a program that will calculate income Tax but I am stuck on how to calculate that and was hoping someone could give me an idea on how to proceed...

Here is my confusion... We have tax brackets say the 1st 500 is taxed at 5% and then 2nd 500 is taxed at 8% and the 3rd 500 is taxed at 10% if a person enters an incom of say 1500 I can't figure out how to divy that up so that the tax is properly calculated for all 3 tax rates that would be applied. What I have is basically

if income <=500 then taxrate = .05
else if income >500 and taxrate <=1000 taxrate = .08
else if income >1000 then taxrate = .10
taxdue=income*taxrate

As you can see that would calculate tax using only one tax rate for the entered amount... I'm stuck on how to calculate the tax properly.... Any suggestions?

Thanks!

2. Code:
```  if (income <= 500)
tax = income * 0.05;
else if (income > 500 && income <= 1000)
tax = income * 0.08;
else if (income > 1000)
tax = income * 0.10```

3. Thanks I've got that... but the problem I have is that the tax is calculated over different brackets

For say 2000 you calculate the tax on the first 500 and then the next 500 is at a higher rate all the while keeping a total for output

Given a tax brackett like:

500 would be taxed at 5%
501-1000 taxed at 8%
over 1000 taxed at 10%

Course I am using small numbers as an example... this one is puzzling to me... not sure how to do this..

4. This is simple dude

just do tax += income * rate

5. Guess I'm just not seeing it then... I am very much a beginner so bare with me

To give a full example say you input \$70,000 as your income and the tax bracketts are:

10% for up to \$15,100
15% for 15,001 to 61,300
25% for 61,301 to 70,000

\$1,510 (10% of that first 15,100 of the 70,000)
\$6,930 (15% for your income between 15,001 to 61,300)
\$2,175 (25% for your income between 61,300 to 70,000)

Total taxes would be \$10,615 on an income of \$70,000

So it seems there has to be some way to figure out what part of your income needs to be taxed at the higher rates as you go through the bracketts.

tax+= income*rate would just calculate the tax for the entire amount (not stepping through different rates) and keep a total of that right?

6. Dude first try to write a simple program then post it please don't depend on spoon feeding otherwise you won't learn that fast and you will not understand the language and programming concepts

and what ever formula you wrote above try to convert that in simple C/C++ program then we will discuss on that further

7. Thanks for the reply, I do have the program written and am just trying to get that last part ... I am trying to figure out how to solve the issue of the tax rate rising through the different bracketts... I had it coded as you first suggested

//take status code and set the tax rate accordingly
if (statusCode == 1)

if ( income <= 7550)

taxrate = .10;

else if(income >= 7551 && income <= 30650)

taxrate = .15;

else if(income >= 30651 && income <= 74200)

taxrate = .25;

Which of course calculates for the entire income input and doesn't break it down as in my last example. I'm not sure how I would accomplish that, not looking for a spoon of anything

8. [QUOTE=Bnorman;903096]Guess I'm just not seeing it then... I am very much a beginner so bare with me

10% for up to \$15,100
15% for 15,001 to 61,300
25% for 61,301 to 70,000

Hey but by looking at your code i can't figure out where is the above one

Code:
```if ( income <= 7550)

taxrate = .10;

else if(income >= 7551 && income <= 30650)

taxrate = .15;

else if(income >= 30651 && income <= 74200)

taxrate = .25;```

it should be changed according to your tax rule which you posted above

9. If the income is in excess of the bracket cutoff, you subtract out the bracket, and use the balance to calculate the next rate. If the income still exceeds the second bracket, you subtract again, and calculate the highest tax on the final balance.

Then you add in the constant tax for any bracket skipped. The tax for lower brackets is constant, because in order to get to the next bracket you have to hit a fixed amount.

10. Yea I am sorry I've been trying to use very basic examples of what I am trying to accomplish. I think I am lacking in the explanation of the problem and I apologize...

In a nutshell there are 4 different filing types (StatusCodes is my variable for that)

1. being a single person
2. being married
3. married but seperate

Each has a different tax brackett for their income which I am using my statusCode to set

cin>>statusCode
cin>>income

if (statusCode ==1) // a single filer
...if statements to set tax rate based on > and < of income range

if (statusCode==2) // a married filer
... if statments to set tax rate based on > and < of income range

I'm thinking somehow to take the high end of the tax bracket say 7550 (0 - 7550)

if (income <= 7550)
taxrate = .10;

apply 10 % to it and then see if income is >= to the next brackett high end (7551 to 30650) if it is then tax it at 15% and go on to the next... I can't really write the code for that cause I'm not sure how to calculate that which is where my problem lies... If I can figure out the proper way to calculate that I think i can do the coding... Talking through it like this is giving me ideas so I REALLY appreciate you conversing with me on it!

11. Originally Posted by Bnorman
I can't really write the code for that cause I'm not sure how to calculate that which is where my problem lies...
Right. So you should sit down with some values and work out what they are and come back when you have. rdrast makes a good point in that if you go over the line (from 10% into 15% or whatever your rates are), you only have to add a fixed amount to your tax since you know you maxed out the 10% rate no matter what.

12. Ok

so what i am getting from your last post is that

Code:
```std::cout << "Please enter status"
std::cin >> status

calculate_tax(status)

void calculate_tax(const int status) {
switch (status)
case 1:
case 2:
case 3:
case 4:
and so on
}```

13. Going "Over a Bracket" is easy.

Say bracket is 7500, income is 11500, and the low rate is 0.10, the mid rate is 0.15.

Well, over the bracket, so that is fixed 7500 * 0.10 = 750.
Now, what's left at the higher rate? Well, it's 11,500 - 7500 (the 7500 was already taxed), or 4000.
MidBracketTax = (11500 - 7500) * 0.15 = 4000 * 0.15 = 600.

So, total tax is 750 + 600 = 1350.

If you have yet a third bracket, you handle that the same way, only the REMAINDER is taxed at the highest rate.

Having different classifications of filing isn't bad, as you just have to fill in the proper values for:
(example)
LowBracketCutoff, MidBracketCutoff, HighBracketCutoff
LowBracketRate, MidBracketRate, HighBracketRate
and run it through the above math.

14. Right on... Thanks to everyone for their input! I'm still working on it and thinking in pseudo code it might be

if income >= high end of tax bracket 1 apply the first tax rate and add to ttlTax
if income < high end of tax brackett 2 (income minus highend bracket 1) * 2nd tax rate and add to ttlTax

cout<<"Your Total Taxes for "<<income<< " Will be "<<ttlTax<<endl;

That sound like the right line of thinking? Guess maybe it's the math that has me stumped more than the code?

Bad thing is we're not allowed to use functions just modular code since we haven't covered functions to this point.

15. Thanks Rdast... you must have posted while I was still trying to figure out what to say haha!!

I'll put in a good word with Santa for everyone, really appreciate the discussion and suggestion.... helps us newbies out a lot!