Tax Rate Calculator Revised

I wrote this code for a class assignment, and had posted it priorly, but I am still getting the incorrect output and can't see at this point as to what is causing this. (ex. 10,000 as a single filer comes out to a tax of 1,300, shouldn't it be 1,100?)

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

#include <iostream>
#include <string>

using namespace std;

int main()
{
    const double tax_rate1 = 0.10;
    const double tax_rate2 = 0.15;
    const double tax_rate3 = 0.25;
    const double rate_single_limit1 = 8000;
    const double rate_single_limit2 = 32000;
    const double rate_married_limit1 = 16000;
    const double rate_married_limit2 = 64000;

    double tax1 = 0.0;
    double tax2 = 0.0;
    double tax3 = 0.0;

    double income;
    cout << "Please enter your income: ";
    cin >> income;

    cout << "Please enter marital status (m/s): ";
    string marital_status;
    cin >> marital_status;

    if (marital_status == "s")
    {
        if (income <= rate_single_limit1)
        {
            tax1 = tax_rate1 * income;
        }
        if (income > rate_single_limit1 && income <= rate_single_limit2)
        {
            tax1 = tax_rate1 * income;
            tax2 = tax_rate2 * (income - rate_single_limit1);
        }
        if (income > rate_single_limit2)
        {
            tax1 = tax_rate1 * income;
            tax2 = tax_rate2 * (income - rate_single_limit1);
            tax3 = tax_rate3 * (income - rate_single_limit2);
        }

    }
    else
    {
        if (income <= rate_married_limit1)
        {
            tax1 = tax_rate1 * income;
        }
        if (income > rate_married_limit1 && income <= rate_married_limit2)
        {
            tax1 = tax_rate1 * income;
            tax2 = tax_rate2 * (income - rate_married_limit1);
        }
        if (income > rate_married_limit2)
        {
            tax1 = tax_rate1 * income;
            tax2 = tax_rate2 * (income - rate_married_limit1);
            tax3 = tax_rate3 * (income - rate_married_limit2);
        }
    }

    double total_tax = tax1 + tax2 + tax3;

    cout << "The tax is $" << total_tax << endl;
    return 0;
}
Last edited on
your code works well. so much so that the output conforms to what you wrote.
I can tell you that the sum in line 63 is useless, since from your code you can only calculate one of the values between tax1, tax2 and tax3

sorry i use income = 500 and this work normal
Last edited on
My issue is when entering values over either of my basic limits(etc. 10000 for single and 18000 for married) the calculation comes back with an out put of "The tax is $3.2003e+006." , or something similar. How would I correct this?
Last edited on
Hello DeathShaman1,

Welcome to the forum.

Does this have something to do with the implemented tax rates? etc. the 800 + .15 and such?
Yes it does. Consider a final income of 50,000 * 800.15 the answer is 40,007,500.00. Not quite the number you are looking for. This would grow even larger with 8800.25 * 50,000.

You will need to figure out what 800, 4400, 1600 and 8800 are supposed to be and how much need to be added to the .15 and .25.

It would also help to know some input for test that you have the answer to.

I tend to agree with ar2007 that line 63 has no real logic to it, but it still works because those variables were initialized to zero when defined. It is a quick and dirty way to catch the variable that has a value. It is there and I would not bother with it for now.

Hope that helps,

Andy
Hello DeathShaman1,

Your last post showed up before I finished mine.

Add the header file "iomanip" and just before line add this
std::cout << std::fixed << std::showpoint << std::setprecision(2);

This worked for me.

Hope that helps,

Andy
you should do something similar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
	if ( income <= limit1 )
	{
		tax = income * tax_rate_1 ;
	}
	else if( income > limit1 && income <= limit2 )
	{
		tax = ( limit1 * tax_rate_1 ) + ( income - limit1 ) * tax_rate_2 ;
	}
	else
	{
		tax = ( limit1 * tax_rate_1 ) + ( limit2 * tax_rate_2 ) + ( income - limit2 ) * tax_rate_3
	}

	cout << tax ;


edit: tax_rate_1 = 0.1 , tax_rate_2 = 0.15 , tax_rate_3 = 0,25
Last edited on
Hello DeathShaman1,

I agree with ar2007's edit on "tax_rate_1" etc. and the use of "tax" which would eliminate line 63 and cause a change in line 65 form "total_tax" to just "tax".

I am not sure who's formulas would work best yours or ar2007's. This is where some known test data and answers would come in handy.

As I looked over the definitions of the different "tax_rate"s I could not figure any use for the large numbers you are adding. Once I removed them it made better sense. The 0.10, 0.15 and 0.25 make more sense and a more acceptable answer.

Hope that helps,

Andy
Hello DeathShaman1,

Your last post showed up before I finished mine.

Add the header file "iomanip" and just before line add this
std::cout << std::fixed << std::showpoint << std::setprecision(2);

This worked for me.

Hope that helps,

Andy

Sorry Andy, but I had not read your 10.40pm message as it was posted at the same time at 10.40pm.

anyway I posted that piece of code, because it seemed more linear.
the use of 'fixed' would respect the author's encoding way.

bye
Topic archived. No new replies allowed.