Problem overview
The Shopping Cart project builds a small receipt printer. In this first version the cart holds a single item. The starter code gives you item, price and quantity, and the program prints a two-line receipt: the item and the quantity on the first line, and the total on the second.
With the starter data, the output is:
Coffee Mug x3
Total: $36
The rules also fix how the output is produced: compute total = price * quantity, build each line as a string, join the two lines with a newline into one string named result, and print that string with cout and endl.
Approach: numbers become text, then text is joined
The arithmetic is one multiplication: the price of one mug repeated once per mug. The interesting part is the text. The receipt lines mix words and numbers, and C++ does not allow "Total: $" + total when total is an int. The function to_string turns a number into a string, and strings can be joined with +.
Building each line in its own variable, line1 and line2, keeps the program readable: each variable corresponds to one row of the receipt. A final string result puts the rows together with the newline character "\n" between them, and a single cout prints everything.
Step by step
- Compute
total. Declare anintequal toprice * quantity. For 12 and 3, that is the cost of all the mugs. - Build
line1. It reads likeCoffee Mug x3: the item name, then the text" x"with its leading space, then the quantity. The quantity is anint, so it goes throughto_string. The text pieces are already strings and need no conversion. - Build
line2. Start from the text"Total: $"and addto_string(total). The dollar sign is an ordinary character. - Build
result. Joinline1, the newline"\n"andline2. The newline starts a new row at the point where it sits. - Print
resultwithcoutand finish withendl, so that the last row also ends with a line break.
The solution in C++
string item = "Coffee Mug";
int price = 12;
int quantity = 3;
int total = price * quantity;
string line1 = item + " x" + to_string(quantity);
string line2 = "Total: $" + to_string(total);
string result = line1 + "\n" + line2;
cout << result << endl;
Details worth a closer look:
- Where the first string comes from. In
item + " x" + to_string(quantity), the first operand is astring, so each+after it joins a string with another string or a literal. The chain works from left to right. - A literal on the left.
"Total: $" + to_string(total)is fine because the right side is astring. What does not compile is adding two plain literals, such as"Total: " + "$". to_stringversuscout.cout << totalcan print a number directly, but this lesson builds the whole receipt as a string first, so the number has to become text."\n"is one character. It is the escape sequence for a newline, written with a backslash. A forward slash,"/n", would print those two characters.
Edge cases and common mistakes
- Forgetting
to_string. Writing"Total: $" + totaldoes not compile. C++ reports that it cannot find anoperator+for astd::stringand anint. - Quantity zero. The same code prints a valid receipt: for
"Lamp"with price 30 and quantity 0, the output isLamp x0andTotal: $0. - Missing space. The item and the quantity are separated by a space before the
x, and there is none after it. Writing"x "or leaving the space out changes the line. - Overflow. The product is an
int. For very large prices and quantities it could exceed what anintholds, and signed overflow is undefined behaviour in C++. The lesson's values are small.
Testing
| item, price, quantity | Output |
|---|---|
| Coffee Mug, 12, 3 | Coffee Mug x3, then Total: $36 |
| Pen, 2, 1 | Pen x1, then Total: $2 |
| Lamp, 30, 0 | Lamp x0, then Total: $0 |
The next version builds on this receipt.
