/* testfunc15.cpp CIS 150 2008-06-10 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); void cont(); int main(int argc, char* argv[]) { 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'; cont(); return 0; } /* double calcDiscount(int qty); Arguments: int qty: the quantity of items (which determines the discount) Returns: The applicable discount rate as a decimal percentage. 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; } /* double calcAmount(double priceEach, int qty, double discountPct, double taxRate, double &gross, double &discountAmt, double &subtot, double &taxAmt); Arguments: doublepriceEach: the price of an individual unit int qty: the number of units ordered double discountPct: the discount percentage in decimal form double taxRate: the tax rate in decimal percentage form double &gross: a ref var where the gross total for the bill will be stored double &discountAmt: a ref var where the total discount amount for the bill will be stored double &subtot: a ref var where the subtotal for the bill will be stored double &taxAmt: a ref var where the tax amount for the bill will be stored Returns: The calculated total for this bill (a double). Note also that the gross amount, discount amount, subtotal, and total tax amount are also returned through reference variables sent in as arguments. 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; } /* void cont(); Arguments: none Returns: nothing Clears input buffer and pauses waiting for user to press Enter. */ void cont() { if (cin.rdbuf()->sungetc() != -1 && cin.get() != '\n') cin.ignore(80,'\n'); cout << "Press enter to continue..."; cin.get(); }