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
- Read the inputs. Read three values from
std::cinin this order:unitPrice,quantity,amountPaid. One chainedstd::cin >> a >> b >> c;is ideal. - Compute the subtotal:
unitPrice * quantity. - 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) mainreturning0(1.1)
Constraints
- Use
intfor every variable. No other types. - Only
#include <iostream>. No functions of your own, noif, no loops (not taught yet). - Initialize every variable when you define it (Chapter 1's golden rule).
- Don't change the three
std::coutlines or the output format.
Build & run locally
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 cleanHints
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"whenamountPaid < subtotal(needsif, Chapter 4/8). - Support prices with cents using
doubleand 2-decimal formatting (Chapters 4 & 28).
// 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;
}
Try the lab first — the learning is in the attempt.
// Chapter 1 — C++ Basics · Project: Cash Register (REFERENCE SOLUTION)
//
// One possible complete solution. Peek only if you're stuck — you'll learn far
// more by getting your own version to pass `make test` first.
#include <iostream> // std::cin, std::cout
int main()
{
int unitPrice {};
int quantity {};
int amountPaid {};
// TASK 1 — read all three with one chained extraction (whitespace-separated).
std::cin >> unitPrice >> quantity >> amountPaid;
// TASK 2 — an expression can be placed directly inside the {} initializer.
int subtotal { unitPrice * quantity };
// TASK 3 — reuse subtotal; amountPaid is left intact for clarity.
int change { amountPaid - subtotal };
std::cout << "Items: " << quantity << " x $" << unitPrice << '\n';
std::cout << "Subtotal: $" << subtotal << '\n';
std::cout << "Change: $" << change << '\n';
return 0;
}
Items: 3 x $5 Subtotal: $15 Change: $5
5 3 20
# Chapter 1 — Cash Register · output-comparison grader.
# Targets follow the drills/CLAUDE.md Makefile contract.
CXX := clang++
CXXFLAGS := -std=c++17 -Wall -Wextra
.PHONY: all build run test solution test-solution clean
all: build
build: starter/app
starter/app: starter/main.cpp
$(CXX) $(CXXFLAGS) starter/main.cpp -o starter/app
run: build
./starter/app
# Your grader: feed tests/input.txt on stdin, compare output to tests/expected.txt.
test: build
@./starter/app < tests/input.txt > tests/.out.txt
@diff tests/expected.txt tests/.out.txt && echo "PASS ✅ output matches expected." || echo "FAIL ❌ fill in the TODO blocks until the diff above is empty."
solution: solution/app
./solution/app < tests/input.txt
solution/app: solution/main.cpp
$(CXX) $(CXXFLAGS) solution/main.cpp -o solution/app
# Proof the exercise is solvable: the reference solution must match expected output.
test-solution: solution/app
@./solution/app < tests/input.txt > tests/.sol.txt
@diff tests/expected.txt tests/.sol.txt && echo "PASS ✅ solution matches expected." || echo "FAIL ❌ solution does NOT match expected."
clean:
rm -f starter/app solution/app tests/.out.txt tests/.sol.txt
make test locally
(see “Build & run locally” above).