Start learning free

LESSON 185 OF 281 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V2 in C++: Discount and Tax with Integer Math

Practice Read the full explanation

Add a discount and sales tax to a single-item purchase using integer math.

You start with:

string item = "Coffee Mug";
int price = 12;
int quantity = 3;
int tax_rate = 8;
int discount = 5;

Build:

Subtotal: $36
Discount: $5
Tax: $2
Total: $33

Rules:

  • subtotal = price * quantity.
  • after_discount = subtotal - discount (the discount is a flat amount taken off before tax).
  • tax = after_discount * tax_rate / 100 (integer division, so the result is rounded down). The tax is charged on the discounted amount.
  • total = after_discount + tax.
  • Build four lines (Subtotal, Discount, Tax, Total) and join them with a newline "\n" into one string result, then print it with cout << result << endl.

Compute the four numbers (subtotal, after_discount, tax, total) before formatting.

Use to_string(...) to turn each number into text before concatenating it to a string.

C++'s int / int truncates, so after_discount * tax_rate / 100 already rounds down.

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;

STEP 1 OF 10
    Ctrl + Enter to check

    C++ · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V2 in C++, explained

    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 * quantity is 12 * 3 = 36.
    • after_discount = subtotal - discount is 36 - 5 = 31.
    • tax = after_discount * tax_rate / 100 is 31 * 8 / 100. The product is 248, and dividing by 100 gives 2, because integer division discards the .48.
    • total = after_discount + tax is 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

    1. Create subtotal as price * quantity. It is the cost before any discount or tax.
    2. Create after_discount as subtotal - discount.
    3. Create tax as after_discount * tax_rate / 100. Between two int values, / is integer division and drops the fraction.
    4. Create total as after_discount + tax. This is what the customer pays.
    5. Build line1 as "Subtotal: $" plus to_string(subtotal). C++ cannot join a number to text with +, so each number goes through to_string.
    6. Build line2 the same way for the discount.
    7. Build line3 for the tax.
    8. Build line4 for the total.
    9. Join the rows into result, with "\n" between each two. Four rows need three newlines.
    10. Print result with cout and endl.

    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_discount and tax_rate, as the starter does. C++ does not mind, but keep the lesson's names so that your code matches the steps.
    • item is 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 of line1 + "\n" + line2 + ... is a string variable, 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_discount becomes 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 / 100 gives 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_rate is 0 for any discounted amount below 100.
    • Missing newlines. Joining the rows without "\n" prints everything on one line.

    Testing

    price, quantity, tax_rate, discountOutput 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
    Keep going with C++1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free