Start learning free

LESSON 185 OF 667 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V1 in C++: A One-Item Receipt

Practice Read the full explanation

Practice variables, arithmetic, and string concatenation by printing a tiny one-item receipt.

You start with:

string item = "Coffee Mug";
int price = 12;
int quantity = 3;

Build:

Coffee Mug x3
Total: $36

Rules:

  • Compute total = price * quantity.
  • First line: "<item> x<quantity>" (e.g. "Coffee Mug x3").
  • Second line: "Total: $<total>".
  • Join the two lines with a newline "\n" into one string result, then print it with cout << result << endl.

Use to_string(quantity) and to_string(total) when concatenating numbers to a string.

line1 + "\n" + line2 joins both lines into one string.

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;

STEP 1 OF 5
    Ctrl + Enter to check

    C++ · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V1 in C++, explained

    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

    1. Compute total. Declare an int equal to price * quantity. For 12 and 3, that is the cost of all the mugs.
    2. Build line1. It reads like Coffee Mug x3: the item name, then the text " x" with its leading space, then the quantity. The quantity is an int, so it goes through to_string. The text pieces are already strings and need no conversion.
    3. Build line2. Start from the text "Total: $" and add to_string(total). The dollar sign is an ordinary character.
    4. Build result. Join line1, the newline "\n" and line2. The newline starts a new row at the point where it sits.
    5. Print result with cout and finish with endl, 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 a string, 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 a string. What does not compile is adding two plain literals, such as "Total: " + "$".
    • to_string versus cout. cout << total can 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: $" + total does not compile. C++ reports that it cannot find an operator+ for a std::string and an int.
    • Quantity zero. The same code prints a valid receipt: for "Lamp" with price 30 and quantity 0, the output is Lamp x0 and Total: $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 an int holds, and signed overflow is undefined behaviour in C++. The lesson's values are small.

    Testing

    item, price, quantityOutput
    Coffee Mug, 12, 3Coffee Mug x3, then Total: $36
    Pen, 2, 1Pen x1, then Total: $2
    Lamp, 30, 0Lamp x0, then Total: $0

    The next version builds on this receipt.

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