### 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.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980`` `````` #include #include 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:

 ``123456789`` ``````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:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546`` ``````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.