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:
- Pick
shipping: 0 whensubtotal >= free_shipping_threshold, otherwiseshipping_cost. - Compute
total = subtotal + shipping. - 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
- Declare
shippingwithout a value. It must exist before theif, because a variable declared inside a block disappears when the block ends. - Write the
ifthat testssubtotal >= free_shipping_threshold, with its closing brace. - Set
shippingto 0 inside it: the free case. - Add the
elseblock. It runs whenever the condition was false. - Set
shippingtoshipping_costin theelse: the paid case. - Compute
totalas the subtotal plus the shipping. Free shipping adds 0, so the total then equals the subtotal. - Build
line1,line2andline3: each is a label followed by a number turned into text withto_string. - Join the lines into
resultwith"\n"between them. Three lines need two newlines. - Print
resultwithcoutandendl.
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 wroteint shipping = 0;inside theifblock, that variable would exist only inside the braces, andtotalcould 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: $" + subtotaldoes not build a string: it is a pointer-and-integer expression that doesn't do what you want.to_string(subtotal)returns astring, and astringcan 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 astd::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.endlat 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: $0andTotal: $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
shippingin only one branch. Without theelse, an uninitialisedintwould 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:
| subtotal | Shipping line | Total line |
|---|---|---|
| 75 | Shipping: $10 | Total: $85 |
| 99 | Shipping: $10 | Total: $109 |
| 100 | Shipping: $0 | Total: $100 |
| 120 | Shipping: $0 | Total: $120 |
The next version builds on this program.
