#include #include #include // for INT_MAX double compute_tax (double taxable_income) { if (taxable_income <= 10000) { // pay just 17% on everything return 0.17 * taxable_income; } if (taxable_income <= 25000) { // pay 17% on the first 10000 and 22% on thge balance return (0.17 * 10000) + (0.22 * (taxable_income - 10000)); } // a high roller return (0.17 * 10000) + (0.22 * 15000) + (0.30 * (taxable_income - 25000)); } double compute_taxable_income (double earned_income, int age, int dependents) { double taxable_income; taxable_income = earned_income - 5000; if (age >= 65) { taxable_income -= 2000; } taxable_income -= dependents * 1000; if (taxable_income < 0) { taxable_income = 0; } return taxable_income; } void calculate_and_output_tax (double earned_income, int age, int dependents) { double taxable_income, tax; taxable_income = compute_taxable_income (earned_income, age, dependents); tax = compute_tax (taxable_income); cout << setprecision(2) << setiosflags(ios::fixed | ios::showpoint) << "Taxable income: $" << taxable_income << " Tax: $" << tax << endl; } int main () { double earned_income; int age, dependents; for (;;) { cout << endl << "Please enter earned income, age, and dependents" << endl << "(all zeroes to stop): "; cin >> earned_income >> age >> dependents; if (cin.fail()) { // the input operation failed altogether (the user did not enter // a valid double value followed by two int values). // begin by outputting an error message. cout << "Garbage input ignored." << endl; // now reset cin and discard the balance of the current input line // (clear the deck so that we can try again). cin.clear (); cin.ignore (INT_MAX, '\n'); } else if ((earned_income == 0) && (age == 0) && (dependents == 0)) { return 0; // we're all done - go home } else if ((earned_income < 0) || (dependents < 0) || (age < 0)) { // the input values are unreasonable cout << "Unreasonable values ignored." << endl; } else { calculate_and_output_tax (earned_income, age, dependents); } } // end for (;;) } // end main