Problem overview
This version of the receipt adds a flat discount and a sales tax to the single-item purchase. The starter data holds item, price, quantity, tax_rate (a percentage) and discount (an amount). The program prints four lines: Subtotal, Discount, Tax and Total.
The rules spell out the arithmetic. The subtotal is price times quantity. The discount is subtracted from the subtotal first, and the tax is charged on what is left, using integer division, so it is rounded down. The total is the discounted amount plus the tax. For the starter data the output is:
Subtotal: $36
Discount: $5
Tax: $2
Total: $33
Version 1 printed only the item, the quantity and one total. Now there are four different amounts, so each one gets its own variable.
Approach: compute the numbers, then format the lines
Keep the calculation and the formatting apart. First compute four integers in the order the receipt needs them, each from the previous ones. Then turn each into a text row and join the rows.
subtotal = price * quantityis 12 * 3 = 36.after_discount = subtotal - discountis 36 - 5 = 31.tax = after_discount * tax_rate / 100is 31 * 8 / 100. The product is 248, and dividing by 100 gives 2, because integer division discards the .48.total = after_discount + taxis 31 + 2 = 33.
The tax multiplies first and divides second, for the same reason as in percentage calculations with integers generally: dividing first would discard information too early. With the rate before the division, 31 * 8 / 100 is 2, while 31 / 100 * 8 would be 0.
The order of the discount and the tax matters too. The rules say the tax applies to the discounted amount. Taxing the subtotal and subtracting the discount afterwards would give a different tax.
Why whole numbers
All the amounts in this version are int values, so the program works in whole dollars. That keeps the arithmetic exact: integer addition, subtraction and multiplication never lose a fraction, and the only rounding happens at the one division, where the rules ask for it. Floating-point numbers such as double would need conversions on the way back to whole numbers, and would add decisions about rounding that the lesson does not have.
The one division is the tax. Dividing a non-negative whole number by 100 rounds down, so the tax never exceeds the exact value. The product after_discount * tax_rate is far below the largest int for any realistic receipt, although a very large price and quantity could still overflow, and signed overflow is undefined behaviour in C++.
Step by step
- Create
subtotalasprice * quantity. It is the cost before any discount or tax. - Create
after_discountassubtotal - discount. - Create
taxasafter_discount * tax_rate / 100. Between twointvalues,/is integer division and drops the fraction. - Create
totalasafter_discount + tax. This is what the customer pays. - Build
line1as"Subtotal: $"plusto_string(subtotal). C++ cannot join a number to text with+, so each number goes throughto_string. - Build
line2the same way for the discount. - Build
line3for the tax. - Build
line4for the total. - Join the rows into
result, with"\n"between each two. Four rows need three newlines. - Print
resultwithcoutandendl.
The solution in C++
string item = "Coffee Mug";
int price = 12;
int quantity = 3;
int tax_rate = 8;
int discount = 5;
int subtotal = price * quantity;
int after_discount = subtotal - discount;
int tax = after_discount * tax_rate / 100;
int total = after_discount + tax;
string line1 = "Subtotal: $" + to_string(subtotal);
string line2 = "Discount: $" + to_string(discount);
string line3 = "Tax: $" + to_string(tax);
string line4 = "Total: $" + to_string(total);
string result = line1 + "\n" + line2 + "\n" + line3 + "\n" + line4;
cout << result << endl;
Details worth a closer look:
- Naming. The variables use snake_case,
after_discountandtax_rate, as the starter does. C++ does not mind, but keep the lesson's names so that your code matches the steps. itemis unused. The starter declares it, and this version prints no item name. That is fine: the program compiles and runs the same.- A long
+chain. The first operand ofline1 + "\n" + line2 + ...is astringvariable, so every+after it works. Each+creates a temporary string, which is fine for this size.
Edge cases and common mistakes
- Tax rounding. The tax is rounded down, not to the nearest unit. A discounted amount of 7 at a rate of 8 gives 56 / 100, which is 0, so the tax line shows
Tax: $0. - Zero discount. With a price of 10, quantity 1, rate 8 and discount 0, the receipt shows a subtotal of 10, a discount of 0, tax 0 and total 10.
- Discount equal to the subtotal. The amount after the discount is 0, so the tax and the total are 0.
- A discount larger than the subtotal.
after_discountbecomes negative, and so does the total. The lesson does not define what should happen, so the solution does not guard against it. - Taxing the subtotal. Writing
subtotal * tax_rate / 100gives 36 * 8 / 100 = 2 for the starter data, which happens to match, but it is wrong in general because the rules tax the discounted amount. - Dividing before multiplying.
after_discount / 100 * tax_rateis 0 for any discounted amount below 100. - Missing newlines. Joining the rows without
"\n"prints everything on one line.
Testing
| price, quantity, tax_rate, discount | Output rows (Subtotal, Discount, Tax, Total) |
|---|---|
| 12, 3, 8, 5 | $36, $5, $2, $33 |
| 10, 1, 8, 0 | $10, $0, $0, $10 |
| 10, 1, 8, 3 | $10, $3, $0, $7 |
| 10, 1, 8, 10 | $10, $10, $0, $0 |
