Chapter 1 · C++ Basics
Exercise · Chapter 1

Cash Register

You're writing the brain of a supermarket cash register. The program reads three whole numbers — a unit price, a quantity, and the cash the customer hands over — then prints a tidy receipt showing the line item, the subtotal, and the change due.

It's deliberately tiny: no functions, no if, no loops (those come in later chapters). The whole point is to get fluent with the four things Chapter 1 is about — defining and initializing variables, reading input with std::cin, computing with operators and expressions, and printing with std::cout.

Your tasks

  1. Read the inputs. Read three values from std::cin in this order: unitPrice, quantity, amountPaid. One chained std::cin >> a >> b >> c; is ideal.
  2. Compute the subtotal: unitPrice * quantity.
  3. Compute the change: amountPaid - subtotal.

Success criteria

make test prints PASS ✅. It feeds the program the input in tests/input.txt (5 3 20 → price 5, qty 3, paid 20) and checks that the output matches tests/expected.txt exactly:

Items: 3 x $5
Subtotal: $15
Change: $5

Until you fill in the tasks, the program prints zeros and make test shows FAIL ❌ with a diff. Turning that red into green is the whole exercise.

Concepts practiced
  • Defining variables and list/value-initializing them with {} (1.4)
  • Reading input with std::cin >>, chained (1.5)
  • Arithmetic operators and expressions (1.9, 1.10)
  • Output with std::cout << and '\n' (1.5)
  • main returning 0 (1.1)
Constraints
  • Use int for every variable. No other types.
  • Only #include <iostream>. No functions of your own, no if, no loops (not taught yet).
  • Initialize every variable when you define it (Chapter 1's golden rule).
  • Don't change the three std::cout lines or the output format.
Build & run locally
shell
make          # compile starter/  ->  starter/app
make run      # run it; then type:  5 3 20  <Enter>
make test     # grade your code against the expected output
make solution # run the reference solution if you get stuck
make clean
Hints
Task 1 — reading input

std::cin >> unitPrice >> quantity >> amountPaid; reads three whitespace-separated integers in one statement (the extraction operator skips the spaces between them).

Tasks 2 & 3 — the math

Replace the empty {} initializer with the real expression, e.g. int subtotal { unitPrice * quantity };. An expression can go right inside the braces.

Stretch goals
  • Print "Insufficient payment" when amountPaid < subtotal (needs if, Chapter 4/8).
  • Support prices with cents using double and 2-decimal formatting (Chapters 4 & 28).
starter/main.cpp C++
// Chapter 1 — C++ Basics · Project: Cash Register  (STARTER)
//
// Fill in the three TODO blocks below. Build/run/grade with:
//     make        build
//     make run    run (then type:  5 3 20  and press Enter)
//     make test   grade against the expected output
//
// Everything lives in main() on purpose — defining your own functions is Chapter 2.

#include <iostream>   // std::cin, std::cout

int main()
{
    // The three inputs. Chapter 1's golden rule: initialize on definition.
    int unitPrice {};
    int quantity {};
    int amountPaid {};

    // ─── TASK 1: read the three inputs from std::cin, IN THIS ORDER ─────
    //   unitPrice, then quantity, then amountPaid.
    // Hint: one chained extraction reads them all:  std::cin >> a >> b >> c;
    //
    //   >>> YOUR CODE HERE <<<
    //
    // ───────────────────────────────────────────────────────────────────

    // ─── TASK 2: compute the subtotal = unitPrice * quantity ───────────
    // Replace the empty {} with the real expression.
    //
    //   >>> YOUR CODE HERE <<<
    int subtotal {};
    // ───────────────────────────────────────────────────────────────────

    // ─── TASK 3: compute the change = amountPaid - subtotal ────────────
    // Start from amountPaid below and subtract the subtotal.
    //
    //   >>> YOUR CODE HERE <<<
    int change { amountPaid };
    // ───────────────────────────────────────────────────────────────────

    // The receipt is printed for you — do NOT change these lines or the format.
    std::cout << "Items: " << quantity << " x $" << unitPrice << '\n';
    std::cout << "Subtotal: $" << subtotal << '\n';
    std::cout << "Change: $" << change << '\n';

    return 0;
}
Run
Submit
Run in your browser — coming soon For now: copy or download the files and use make test locally (see “Build & run locally” above).