Something is wrong with my math.

Hi everyone, hope everyone is doing great. so I have this problem with this code. The program is asking to pick (M, 5, 50000, 6) for the input. The answer is supposed to be "5875.00" not 5486.65. The same with (S, 250001, 6) for the input and comes out as "88785.33" which supposed to be 74785.33. I think I messed up wherewith the calculation. Could anyone please explain what I did wrong? thanks, coders.

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

#include <iostream>
#include <iomanip>

using namespace std;

void getData(char &maritalStatus, int &numberOfPersons, double &gross_Salary, double &pension_contribution)
{

    cout << "Please enter your Marital Status." << endl;
    cout << "[M]arried or [S]ingle: ";
    cin >> maritalStatus;

    cout << "Please enter your salary: ";
    cin >> gross_Salary;

    numberOfPersons = 1;

    cout << "Please enter your Pension Plan contribution(limit upto 6%): ";
    cin >> pension_contribution;

    while (pension_contribution > 6.00)
    {
        cout << "The limit is upto 6% only." << endl;
        cout << "Please enter your Pension Plan contribution(limit upto 6%): ";
        cin >> pension_contribution;
    }

    pension_contribution = pension_contribution / 100 * gross_Salary;
    cout << endl;
}

double taxAmount(char maritalStatus, int numberOfPersons, double gross_Salary, double pension_contribution)
{
    double standardExemption = 0;
    double income;

    if (maritalStatus == 'm' || maritalStatus == 'M')
        standardExemption = 7000;
    else
        standardExemption = 4000;

    income = gross_Salary - ((1500.00 * numberOfPersons) + pension_contribution + standardExemption);
    return (income);
}

double calcTax(double taxable_Income)
{
    if (taxable_Income >= 0 && taxable_Income <= 15000)
        return taxable_Income * 0.15;
    else if (taxable_Income >= 15001 && taxable_Income <= 40000)
        return 2250.00 + taxable_Income * 0.25;
    else
        return 8460.00 + taxable_Income * 0.35;
}

int main()
{
    char maritalStatus = ' ';
    int numberOfPersons = 0;
    double gross_Salary = 0, pension = 0;
    double federal_tax = 0, taxable_Income = 0;

    cout << fixed << showpoint << setprecision(2);

    getData(maritalStatus, numberOfPersons, gross_Salary, pension);

    taxable_Income = taxAmount(maritalStatus, numberOfPersons, gross_Salary, pension);

    federal_tax = calcTax(taxable_Income);

    cout << "Marital Status: " << maritalStatus << endl;
    cout << "Number of Persons in the family: " << numberOfPersons << endl;
    cout << "Income earned: " << gross_Salary << endl;
    cout << "Pension Plan contribution: " << pension << endl;
    cout << "Taxable Income: " << taxable_Income << endl;
    cout << "Thus, the Federal Tax payable is: " << federal_tax << endl;
    return 0;
}
The code you show hardcodes numberOfPersons to 1.

And I think in calcTax you need to subtract the previous top amount in each tax bracket:

1
2
3
4
5
6
7
8
9
double calcTax(double taxable_income)
{
    if (taxable_income <= 15000)
        return taxable_income * 0.15;
    else if (taxable_income <= 40000)
        return 2250.00 + (taxable_income - 15000) * 0.25;
    else
        return 8460.00 + (taxable_income - 40000) * 0.35;
}

You might consider a struct to gather your variables together:

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
struct Worker {
    char   marital = 'S';
    int    persons = 1;
    double salary  = 0;
    double pension = 0;
    double taxable_income = 0;
    double federal_tax = 0;
};

void getData(Worker& w)
{
    while (true)
    {
        cout << "Enter your Marital Status.\n"
             << "[M]arried or [S]ingle: ";
        cin >> w.marital;
        w.marital = toupper(w.marital);
        if (w.marital == 'M' || w.marital == 'S') break;
        cout << "Hey, buddy, I'm not lookin' for no trouble.\n";
    }

    if (w.marital == 'S')
        w.persons = 1;
    else {
        while (true) {
            cout << "Enter the number of people in your family: ";
            cin >> w.persons;
            if (w.persons > 1 && w.persons < 100) break;
            cout << "Please count again.\n";
        }
    }

    cout << "Enter your salary: ";
    cin >> w.salary;

    while (true)
    {
        cout << "Enter your Pension Plan contribution (limit upto 6%): ";
        cin >> w.pension;
        if (w.pension >= 0 && w.pension <= 6) break;
        cout << "The limit is up to 6% only.\n";
    }

    w.pension = w.pension / 100 * w.salary;
    cout << '\n';
}

Last edited on
Topic archived. No new replies allowed.