cadence_learn
the_c++_track / level 04 of 15 ▶ 18 min

five types, and a way to listen.

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.

after this level you'll be able to
  • Choose the right type for a value and declare it correctly
  • Predict what C++ stores when the value does not match the box
  • Read input with cin and know which way the arrows point
the five you need

declare what kind of thing it is, then use it

Level 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.

int age = 19; // whole numbers double price = 4.99; // numbers with a decimal point char grade = 'A'; // exactly one character — SINGLE quotes string name = "Maya"; // text — DOUBLE quotes bool passed = true; // true or false, nothing else

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.

one extra line for string

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.

try it yourself

what actually ends up in the box

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.

▤ lab 04a · the declaration bench

Every result below is what the real compiler does — including when it says nothing.

what C++ stores

Pick a type on the left and change the value.

Start with int and the value 3.9 — that one is on the exam somewhere.
◈ ask an ai about this

“Explain int, double, char, string and bool in C++ to a beginner. When would I choose each one?”

chatgpt ↗ claude ↗

the other direction

asking someone a question

So far your programs have only talked. cin lets them listen. It is cout’s mirror image, and the arrows flip to match.

which way the arrows point, and why
KEYBOARD 25 cin >> age; arrows point AT the box int age 25 cout << age; arrows point AWAY …and back out to the screen The arrows always point the way the data is travelling. That is the whole rule, and it is why they are never the same shape twice. Getting them backwards — cin << age — is a very common first-week error, and the compiler’s message about it is not obvious.
Follow the data, not the symbols. Into the program is >>, out to the screen is << — and if you can picture this you never have to memorise which is which.
int age; cout << "How old are you? "; // ask — no endl, so the cursor stays put cin >> age; // wait for them to type and press return cout << "Next year you will be " << age + 1 << endl;

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.

try it yourself

run it, and then break it

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.

▤ lab 04b · the average

Switch between the two versions and give it 7 and 2.

output
# press run
this panel reproduces what the real program prints — it is a simulation, not a compiler
Give the int version 7 and 2. The answer will be wrong, and nothing will warn you.
the one that behaves differently on different machines

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.

and if they type letters

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.

◈ ask an ai about this

“How does cin work in C++, and why does 7 / 2 give 3 even when I store it in a double?”

chatgpt ↗ claude ↗

boxes that must not change

const — a promise the compiler enforces

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.

const double TAX_RATE = 0.20; const int MAX_STUDENTS = 30; TAX_RATE = 0.25; // the compiler refuses. verified:
terminal
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.

why it is worth typing

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.

names

the cheapest quality you can buy

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.

two rules and you are done

Start with a lowercase letter and run words together with capitals: firstName, totalCost, isFinished. And say what it holds, not what type it isage, never intAge. The type is already written one word to the left.

do this together

guess the output before you run it

☶ two people · 15 minutes · one laptop

Write short programs for each other. Predict first, always.

person a — the author

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.

person b — the predictor

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.

what to keep

three things worth remembering

01

Follow the data

>> points into the box, << points out to the screen. Never memorise it — picture it.

02

Whole numbers divide wholly

7 / 2 is 3, and storing it in a double does not save you. One side has to be decimal.

03

Name it for what it holds

Free to do, and it is most of what “maintainable” means in a first course.

check yourself

5 questions before you move on

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.

▢ check yourself5 questions

Have a real go before revealing. Being wrong here is worth more than being right in three weeks.

01

What does this print?

int x = 3.9; cout << x;
02

And this one?

bool b = true; cout << b;
03

What comes out here?

string s = "42"; cout << s + "1";
04

This one connects back to level 8 of the thinking track.

char c = 'A'; cout << (int)c;
05

Someone types their full name and you read it with cin >> name. What ends up in name?

answered: 0 of 5right first time: 0
Stuck? Peter reads these personally and replies to your email.
Ask Peter →