Start learning free

LESSON 187 OF 667 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V3 in C++: Free Shipping with if/else

Practice Read the full explanation

Use an if/else to decide whether to charge shipping based on a free-shipping threshold.

You start with:

int subtotal = 75;
int shipping_cost = 10;
int free_shipping_threshold = 100;

Build:

Subtotal: $75
Shipping: $10
Total: $85

Rules:

  • If subtotal >= free_shipping_threshold, set shipping to 0; otherwise set it to shipping_cost.
  • total = subtotal + shipping.
  • Always include all three lines (Subtotal, Shipping, Total). When shipping is free, the line shows $0: for a subtotal of 120 the lines would be Subtotal: $120, Shipping: $0, Total: $120.
  • Join the three lines with a newline "\n" into one string result, then print it with cout << result << endl.

Decide shipping in an if/else first, then build the three lines.

Declare int shipping; before the if, so it is still visible after the if/else, then assign it in both branches.

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

int subtotal = 75;
int shipping_cost = 10;
int free_shipping_threshold = 100;

int shipping;
if (subtotal >= free_shipping_threshold) {
    shipping = 0;
} else {
    shipping = shipping_cost;
}
int total = subtotal + shipping;
string line1 = "Subtotal: $" + to_string(subtotal);
string line2 = "Shipping: $" + to_string(shipping);
string line3 = "Total: $" + to_string(total);
string result = line1 + "\n" + line2 + "\n" + line3;
cout << result << endl;

STEP 1 OF 11
    Ctrl + Enter to check

    C++ · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V3 in C++, explained

    Problem overview

    The cart now has a subtotal, a flat shipping_cost and a free_shipping_threshold. A cart whose subtotal reaches the threshold ships for free; any other cart pays the shipping cost. The program prints three lines, Subtotal, Shipping and Total, joined into one string called result.

    With the starter values (subtotal 75, shipping 10, threshold 100) the cart is under the threshold, so shipping is charged:

    Subtotal: $75
    Shipping: $10
    Total: $85
    

    The earlier versions only printed numbers. This one has to make a decision, so it needs an if/else.

    Approach: decide the shipping first, then build the text

    The three printed lines all depend on one value, the shipping. So the plan is to settle that value first and write the output afterwards:

    1. Pick shipping: 0 when subtotal >= free_shipping_threshold, otherwise shipping_cost.
    2. Compute total = subtotal + shipping.
    3. Format the three lines and print them.

    The comparison is >=, not >: a cart that lands exactly on the threshold ships free. When shipping is free the line still appears and shows $0, so the output always has three lines.

    Step by step

    1. Declare shipping without a value. It must exist before the if, because a variable declared inside a block disappears when the block ends.
    2. Write the if that tests subtotal >= free_shipping_threshold, with its closing brace.
    3. Set shipping to 0 inside it: the free case.
    4. Add the else block. It runs whenever the condition was false.
    5. Set shipping to shipping_cost in the else: the paid case.
    6. Compute total as the subtotal plus the shipping. Free shipping adds 0, so the total then equals the subtotal.
    7. Build line1, line2 and line3: each is a label followed by a number turned into text with to_string.
    8. Join the lines into result with "\n" between them. Three lines need two newlines.
    9. Print result with cout and endl.

    The solution in C++

    int subtotal = 75;
    int shipping_cost = 10;
    int free_shipping_threshold = 100;
    
    int shipping;
    if (subtotal >= free_shipping_threshold) {
        shipping = 0;
    } else {
        shipping = shipping_cost;
    }
    int total = subtotal + shipping;
    string line1 = "Subtotal: $" + to_string(subtotal);
    string line2 = "Shipping: $" + to_string(shipping);
    string line3 = "Total: $" + to_string(total);
    string result = line1 + "\n" + line2 + "\n" + line3;
    cout << result << endl;
    

    Details worth a closer look:

    • Why int shipping; comes first. If you wrote int shipping = 0; inside the if block, that variable would exist only inside the braces, and total could not see it. Declaring it outside and assigning in both branches keeps it visible afterwards. Because every path assigns it, it always has a value before it is read.
    • Numbers and text don't join with +. "Subtotal: $" + subtotal does not build a string: it is a pointer-and-integer expression that doesn't do what you want. to_string(subtotal) returns a string, and a string can be joined with +. The label is already text, so only the number needs converting.
    • Why the first + works. "Subtotal: $" + to_string(subtotal) adds a string literal to a std::string, which is fine. Two string literals added to each other would not be.
    • The dollar sign. It is an ordinary character inside the literal, so it is part of the label.
    • Newlines. "\n" is a single newline character. endl at the end adds the final one and flushes the output.

    Following the decision

    Take the starter cart first. 75 >= 100 is false, so the if block is skipped and the else block runs: shipping becomes 10, the total is 85, and the three lines read 75, 10 and 85.

    Now take a cart of 120. 120 >= 100 is true, so the if block runs and shipping becomes 0. The else block is skipped entirely, the total is 120, and the shipping line shows $0. Exactly one of the two blocks runs every time, which is why shipping is always assigned.

    Once you are comfortable with the long form, the same choice can be written in one line with the conditional operator: int shipping = subtotal >= free_shipping_threshold ? 0 : shipping_cost;. With a subtotal of 120 it gives 0, and with 75 it gives 10. The lesson uses the if/else form because it makes each branch easy to see, and it extends naturally when a branch needs several statements.

    Edge cases and common mistakes

    • A cart exactly on the threshold. With a subtotal of 100 the program prints Shipping: $0 and Total: $100. Using > instead of >= would charge 10 here.
    • Just under the threshold. With 99 the shipping is 10 and the total is 109, which is more than the 100 cart pays. That is how a threshold rule behaves.
    • Skipping the free line. The rules say all three lines always appear. Printing the shipping line only when it is non-zero would give a different output.
    • Setting shipping in only one branch. Without the else, an uninitialised int would be read in the paid case, and its value is unspecified.
    • Using = in a condition. if (subtotal = free_shipping_threshold) assigns instead of comparing and is always true when the threshold is not 0.

    Testing

    Changing only subtotal and running the same code gives these results:

    subtotalShipping lineTotal line
    75Shipping: $10Total: $85
    99Shipping: $10Total: $109
    100Shipping: $0Total: $100
    120Shipping: $0Total: $120

    The next version builds on this program.

    Keep going with C++650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free