/* testfunc15.cpp CIS 150 6/9/2005 David Klick Demonstration of more complex function usage. */ #include #include #include using std::cin; using std::cout; using std::setw; using std::setprecision; using std::fixed; using std::showpoint; double calcDiscount(int qty); double calcAmount(double priceEach, int qty, double discountPct, double taxRate, double &gross, double &discountAmt, double &subtot, double &taxAmt); int main(void) { double price = 12.00; const double taxrate = .075; double amt; double disc; int quantity; double amtGross, amtNet, amtTax, amtDisc; do { cout << fixed << showpoint << setprecision(2); cout << "How many items do you want at $" << price << "? "; cin >> quantity; if (quantity < 0) { cout << "Error: Quantity must be at least 0\n"; } } while (quantity < 0); disc = calcDiscount(quantity); amt = calcAmount(price, quantity, disc, taxrate, amtGross, amtDisc, amtNet, amtTax); cout << fixed << showpoint << setprecision(2); cout << " Price: " << setw(10) << price << '\n' << " Quantity: " << setw(7) << quantity << '\n' << " Gross: " << setw(10) << amtGross << '\n' << "Discount %: " << setw(7) << (int)(disc*100) << "%\n" << "Discount $: " << setw(10) << amtDisc << '\n' << " Subtotal: " << setw(10) << amtNet << '\n' << " Tax: " << setw(10) << amtTax << '\n' << " Total: " << setw(10) << amt << '\n'; return 0; } /* Returns the percentage discount given a quantity. */ double calcDiscount(int qty) { double pct = 0.0; if (qty >= 100) pct = 0.2; else if (qty >= 12) pct = 0.1; return pct; } /* Calculates the total amount for a bill given a price, a quantity, a discount percentage, and a tax rate. The total is returned. Four additional reference variables contain additional intermediate information: gross amount, discount amount, net after subtracting discount from gross, and the tax amount. */ double calcAmount(double priceEach, int qty, double discountPct, double taxRate, double &gross, double &discountAmt, double &subtot, double &taxAmt) { gross = priceEach * qty; discountAmt = gross * discountPct; subtot = gross - discountAmt; taxAmt = subtot * taxRate; return subtot + taxAmt; }