The five kinds of box you need, what really ends up inside each one, and cin — so your programs can finally ask a question instead of only talking.
cin and know which way the arrows pointLevel 9 gave you the idea: a variable is a named box holding one thing, and the box has a kind. Here is that idea in real C++, which is where the kind stops being a concept and starts being a word you have to type.
Five types cover almost everything in a first course. Note the quote marks: single quotes mean one character, double quotes mean text. They are different types and swapping them is a real error.
string needs #include <string> at the top, alongside <iostream>. Leave it out and you get the “undeclared identifier” error from the last level — now you know what it means.
Pick a type, type a value, and see what C++ really stores. Some of these are not what you would expect, and the surprising ones are the ones that cost people marks.
Every result below is what the real compiler does — including when it says nothing.
Pick a type on the left and change the value.
So far your programs have only talked. cin lets them listen. It is cout’s mirror image, and the arrows flip to match.
>>, out to the screen is << — and if you can picture this you never have to memorise which is which.Leave the endl off the question. Without it the cursor waits at the end of your prompt, which is what a question is supposed to look like. With it, they type on the line below and it looks broken.
Here is a program that averages two numbers. It has the bug from the last level in it. Type two numbers, run it, and watch.
Switch between the two versions and give it 7 and 2.
# press run
Try dividing by zero above. On an Apple Silicon Mac it prints 0 and exits normally — no crash, no warning, nothing. On a typical Intel machine the identical program dies with a floating point exception. C++ calls this undefined behaviour: the language declines to say what should happen, so the hardware decides.
This is the lab-parity problem from level 1, made real. A bug that is invisible on your laptop and fatal on the marker’s machine — which is why you check for zero before dividing, rather than finding out.
Someone will type “hello” where a number belongs. In modern C++ the variable is set to 0 and cin quietly goes into a failed state, so every later cin is skipped too. Your program races to the end printing nonsense. Checking that the read worked is a level-11 habit worth having early.
Some values should never move once set. The tax rate. The number of suites. The maximum score. Put const in front and C++ will refuse to let anything change it — including you, at 1am, by accident.
error: cannot assign to variable 'TAX_RATE' with const-qualified type note: variable 'TAX_RATE' declared const here
That is a hard error, not a warning — nothing gets built. Which is the whole point: it is a mistake you cannot make, rather than one you have to remember not to make.
Two reasons, and the second matters more than the first. It stops accidental changes — fine. But it also gives the number a name. A program full of bare 0.20 is a program where nobody can tell tax from a discount, and where changing the rate means hunting every copy. One const at the top, changed once, changes everywhere.
CAPITAL_LETTERS with underscores is the usual convention, so a reader can see at a glance that it never moves.
Your catalog asks for programs that are “correct and maintainable”. Maintainable starts here, and it costs nothing: int numberOfStudents instead of int n. double totalPrice instead of double tp.
You are not writing for the compiler — it does not care. You are writing for whoever reads this in week eleven, which is you, having forgotten all of it.
Start with a lowercase letter and run words together with capitals: firstName, totalCost, isFinished. And say what it holds, not what type it is — age, never intAge. The type is already written one word to the left.
Write four lines using two of the five types and a bit of arithmetic. Include one thing you think is sneaky — an int division, a bool being printed, a string with a number in it.
Say the exact output out loud before compiling. Not roughly — exactly, character for character.
Two things worth trying: print a bool and watch it come out as 1, not “true”. And add "42" + "1" as strings and get 421, because + on text means “stick together”. Every time your prediction is wrong you have found a real gap in what you believe, which is worth far more than a program that worked.
>> points into the box, << points out to the screen. Never memorise it — picture it.
7 / 2 is 3, and storing it in a double does not save you. One side has to be decimal.
Free to do, and it is most of what “maintainable” means in a first course.
Not recall — these are the shapes an exam actually uses. Every answer below was produced by compiling and running the code, so if you disagree with one, the compiler is the one to believe.
Have a real go before revealing. Being wrong here is worth more than being right in three weeks.
What does this print?
And this one?
What comes out here?
This one connects back to level 8 of the thinking track.
Someone types their full name and you read it with cin >> name. What ends up in name?
← back to the whole C++ track · stuck on anything? ask peter.