Unit 4: Text, Grids & Input
Build something real.
Unit 4 of 8 in C++ for kids. Its 5 lessons are String Tools, Walking Through Text, Vector Tools, Grids & Asking the User and Quiz Machine — below is everything each one explains, and a question or two from it to try.
Every sample on this page was compiled and run with g++ before it shipped, and prints exactly what it says it prints.
This unit opens with a fortnight’s trial of everything — no card needed — or with a family plan, bought in the iPhone app. The first two units of every track are free for ever. Try it in the app.
🔧 String Tools
Taking a piece out
substr cuts a piece: where to start, and how many letters to take.
C++
string word = "dragonfly";
cout << word.substr(0, 6) << endl;
cout << word.substr(6, 3) << endl;
It prints
dragon fly
Asking a string about itself
length() counts the letters, empty() says whether there are none, and at() fetches one — like [ ] but it checks the number is sensible first.
C++
string w = "code";
cout << w.length() << endl;
cout << w.at(1) << endl;
string nothing = "";
cout << boolalpha << nothing.empty() << endl;
It prints
4 o true
Try it yourself
What does this print?
C++
string w = "birthday";
cout << w.substr(0, 5) << endl;
What does this print?
C++
string a = "cat";
string b = "cat";
cout << (a == b) << endl;
Answer them in the app
🚶 Walking Through Text
A string is loopable
A range-for walks a string one letter at a time, exactly as it walks a vector.
C++
for (char c : string("cat")) {
cout << c << endl;
}
It prints
c a t
Building text up
Start with an empty string and add to it as you go — the usual way to build a word letter by letter.
C++
string out = "";
for (char c : string("abc")) {
out = c + out;
}
cout << out << endl;
It prints
cba
Try it yourself
What does this print?
C++
string word = "banana";
int count = 0;
for (char c : word) {
if (c == 'a') {
count = count + 1;
}
}
cout << count << endl;
What does this print?
C++
string out = "";
for (int i = 0; i < 3; i++) {
out = out + "ab";
}
cout << out << endl;
Answer them in the app
🧺 Vector Tools
Shrinking as well as growing
pop_back removes the last item, clear empties the whole thing, and empty() reports whether anything is left.
C++
vector<int> nums = {1, 2, 3};
nums.pop_back();
cout << nums.size() << endl;
nums.clear();
cout << boolalpha << nums.empty() << endl;
It prints
2 true
Changing what is inside
A range-for hands you a copy of each item, so writing to it changes nothing. To edit the vector itself, loop by index.
C++
vector<int> nums = {1, 2, 3};
for (int i = 0; i < nums.size(); i++) {
nums[i] = nums[i] * 10;
}
cout << nums[0] << " " << nums[2] << endl;
It prints
10 30
Try it yourself
What does this print?
C++
vector<int> v = {5};
v.push_back(6);
v.push_back(7);
v.pop_back();
cout << v.size() << " " << v[v.size() - 1] << endl;
Why does for (int n : nums) { n = n * 2; } leave the vector unchanged?
- The vector is const
- n is a copy of each item, so doubling it changes only the copy
- You cannot multiply in a loop
- It does change it
Answer them in the app
⌨️ Grids & Asking the User
A vector of vectors
Put vectors inside a vector and you have a grid — a game board, a maze, a picture. Two numbers reach a square: row first, then column.
C++
vector<vector<int>> grid = {{1, 2}, {3, 4}};
cout << grid[0][1] << endl;
cout << grid[1][0] << endl;
It prints
2 3
Reading what the user types
cin >> reads one thing the player typed and puts it in a box. It waits until they press enter.
C++
int n;
cin >> n;
cout << n * 2 << endl;
It prints
If the player types 4, this prints 8
Try it yourself
What does this print?
C++
vector<vector<int>> g = {{1, 2, 3}, {4, 5, 6}};
cout << g.size() << " " << g[0].size() << endl;
Which way do the arrows point for reading, compared with printing?
- The same way for both
- cout << sends out; cin >> brings in — the arrows point the way the data travels
- cin uses <<
- cin needs no arrows
Answer them in the app
🏆 Quiz Machine
Try it yourself
What does this print?
C++
vector<string> words = {"a", "bb", "ccc"};
int total = 0;
for (string w : words) {
total = total + w.length();
}
cout << total << endl;
Answer it in the app