// FILE: CompuTax.cpp // DRIVER PROGRAM FOR FUNCTION COMPUTE_TAX #include #include "money.h" // Functions used ... // COMPUTES TAX OWED ON ADJUSTED GROSS INCOME money compute_tax (money); // IN: adjusted gross income int main () { // Local data ... const money sentinel = -1.0; money my_income; // input: adjusted gross income (taxable) money my_tax; // output: computed tax amount // Test compute_tax function -- all possible paths. cout << "Driver program for function compute_tax." << endl; cout << "Enter income greater than zero (or " << sentinel << " to stop test): "; cin >> my_income; while (my_income != sentinel) { my_tax = compute_tax (my_income); if (my_tax >= 0.0) { cout << "The tax on " << my_income; cout << " is " << my_tax << endl; } // end if else { cout << "Income " << my_income << " was negative. "; cout << "Try another value." << endl; } // end else cout << endl << endl; cout << "Enter income greater than zero (or " << sentinel << " to stop test): "; cin >> my_income; } // end while cout << "A " << sentinel << " was entered. " << "Test execution terminated." << endl; return 0; } // COMPUTES TAX OWED ON ADJUSTED GROSS INCOME money compute_tax (money adj_income) // IN: adjusted gross income // Pre: adj_income must be assigned a nonnegative value. // Post: Tax amount computed based on tax category. // Returns: Tax owed if adj_income is nonnegative; otherwise -1.0. { // Local data ... const money cat1_max = 34000.00; const money cat2_max = 82150.00; const double cat1_rate = 0.15; const double cat2_rate = 0.28; const double cat3_rate = 0.31; const money min_valid_amount = 0.0; const money invalid_input = -1.0; money tax_amount; // computed tax amount to be returned // Compute and return tax. if (adj_income < min_valid_amount) return invalid_input; // return for invalid input else if (adj_income > cat2_max) tax_amount = cat1_rate * cat1_max + cat2_rate * (cat2_max - cat1_max) + cat3_rate * (adj_income - cat2_max); else if (adj_income > cat1_max) tax_amount = cat1_rate * cat1_max + cat2_rate * (adj_income - cat1_max); else tax_amount = cat1_rate * adj_income; return tax_amount; } // end compute_tax