Checkpoint: C++ Basics
Reviewed & published by Brayan K
You've learned data types, I/O, strings, casting, enums, namespaces and references — let's combine them. This checkpoint is a single, slightly bigger program that touches several skills at once, plus a short quiz to confirm everything has stuck before you move on to containers.
Part of the free C++ course at LearnCodingFast — hands-on lessons with worked examples and the output they print, plus practice exercises and a quick quiz.
What You've Learned
- Data Types: int, double, char, bool, overflow & float traps
- Input & Output: cin >>, getline, formatting, cin.ignore()
- Strings: concatenate, find, substr, to_string/stoi
- Type Casting: static_cast, round-then-cast, dynamic_cast
- Enums: enum class, custom values, switch to label
- Namespaces: ::, using-declarations, aliases
- References: aliases, pass-by-reference, const&
- Putting it together: one program, many skills
🧭 How This Checkpoint Works
The build challenge below is a small "greeter" program broken into three clearly labelled steps. Each step exercises a different lesson:
- Step 1 — Input & Output: read a full line of text with getline.
- Step 2 — Strings: make an UPPERCASE copy with transform + toupper.
- Step 3 — Casting + Enums: report the length with to_string and pick a mode via an enum class.
Read the fully worked version first, then complete the "Your turn" version where three lines are blanked out.
🎯 Build Challenge: The Greeter
Here is the complete program with every step explained. Run it as-is, then try changing mode to Mode::Normal and predict what changes before you run it again.
#include <iostream>
#include <string>
#include <algorithm> // transform
#include <cctype> // toupper
using namespace std;
// A small enum to classify the request.
enum class Mode { Shout, Whisper, Normal };
// Map the enum to a readable label (Enums lesson).
string label(Mode m) {
switch (m) {
case Mode::Shout: return "SHOUT";
case Mode::Whisper: return "whisper";
case Mode::Normal: return "Normal";
}
return "?";
}
int main() {
// 🎯 BUILD CHALLENGE — combine input, strings, casting and enums.
// STEP 1 (Input & Output): read a whole line into 'name'.
cout << "Enter your name: ";
string name;
// getline(cin, name); // 👈 uncomment / complete this
// STEP 2 (Strings): make an UPPERCASE copy of the name.
string shout = name;
// transform(shout.begin(), shout.end(), shout.begin(),
// [](unsigned char c){ return toupper(c); });
// STEP 3 (Type Casting + Strings): build a greeting whose length
// we also report. to_string turns the size_t length into text.
Mode mode = Mode::Shout; // try changing this!
string out = (mode == Mode::Shout) ? shout : name;
string greeting = "Hello, " + out + "! ("
+ to_string(out.size()) + " chars, mode="
+ label(mode) + ")";
cout << greeting << '\n';
// Input: ada
// Expected (mode = Shout): Hello, ADA! (3 chars, mode=SHOUT)
return 0;
}Now do it yourself. Three lines are replaced with ___ — fill each one using the hints in the comments.
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;
enum class Mode { Shout, Whisper, Normal };
string label(Mode m) {
switch (m) {
case Mode::Shout: return "SHOUT";
case Mode::Whisper: return "whisper";
case Mode::Normal: return "Normal";
}
return "?";
}
int main() {
// 🎯 YOUR TURN — finish the three steps marked TODO.
cout << "Enter your name: ";
string name;
// TODO 1: read a full line into name
___; // 👉 getline(cin, name);
// TODO 2: uppercase a copy of name into 'shout'
string shout = name;
___; // 👉 transform(...toupper...)
Mode mode = Mode::Shout;
string out = (mode == Mode::Shout) ? shout : name;
// TODO 3: build the greeting using to_string for the length
string greeting = "Hello, " + out + "! ("
+ ___ + " chars, mode=" // 👉 to_string(out.size())
+ label(mode) + ")";
cout << greeting << '\n';
// Input: ada -> Hello, ADA! (3 chars, mode=SHOUT)
return 0;
}Reveal the Solution
Give it a genuine try first. When you're ready, expand to compare the three completed lines.
// TODO 1 — read a whole line (spaces included):
getline(cin, name);
// TODO 2 — uppercase a copy in place:
transform(shout.begin(), shout.end(), shout.begin(),
[](unsigned char c){ return toupper(c); });
// TODO 3 — turn the length (a size_t) into text:
to_string(out.size())
// With input "ada" and mode = Shout, the program prints:
// Hello, ADA! (3 chars, mode=SHOUT)Each line maps to one lesson: getline (Input & Output), transform/toupper (Strings), and to_string (Strings + the idea of converting between a number and text from Type Casting).
📝 Checkpoint Quiz
Answer each from memory, then reveal to check. No peeking until you've committed to an answer.
1. Why does cin >> x followed by getline often read an empty string?
The >> leaves the newline in the buffer, and getline reads that leftover newline. Call cin.ignore(...) in between to discard it.
2. What does static_cast<int>(4.8) evaluate to, and why?
4 — converting a double to int truncates toward zero (it drops the fraction) rather than rounding. Use round() first if you want 5.
3. What does find return when the substring is not present?
std::string::npos — a very large unsigned value. Always compare against npos, never -1.
4. Why can't cout print an enum class value directly?
A scoped enum has no implicit conversion to int. You must static_cast<int> it, or write a function that returns a readable name via a switch.
5. What is the difference between void f(int n) and void f(int& n)?
The first takes a copy, so changes don't affect the caller. The second takes a reference (alias), so changes to n are visible to the caller after the call.
6. Why is using namespace std; discouraged in a header file?
It forces the whole std namespace into every file that includes the header, which can cause name clashes far from where you wrote it. Qualify names or use targeted using-declarations instead.
Stuck? Where to Look
- 📥 Reading input: revisit Input & Output.
- 🔤 Transforming text: revisit Strings.
- 🏷️ The enum and its label: revisit Enums.
- 🔁 Converting a number to text: revisit Type Casting.
🎉 Checkpoint Complete
- ✅ Combined input, strings, casting and enums into one working program
- ✅ Read a full line, transformed it, and reported its length as text
- ✅ Reviewed the six core ideas of C++ basics in the quiz
- ✅ Identified exactly which lesson to revisit for anything that felt shaky
Practice quiz
Why does cin >> x followed by getline often read an empty string?
- getline only reads numbers
- getline ignores the first line of input
- The >> leaves the trailing newline in the buffer, which getline then consumes
- cin must be flushed with endl first
Answer: The >> leaves the trailing newline in the buffer, which getline then consumes. Use cin.ignore() between them to discard the leftover newline.
What does static_cast<int>(4.8) evaluate to?
- 4
- 5
- 4.8
- It is a compile error
Answer: 4. Converting a double to int truncates toward zero; it drops the fraction rather than rounding.
What does std::string::find return when the substring is not found?
- -1
- 0
- An empty string
- std::string::npos
Answer: std::string::npos. npos is a very large unsigned value; always compare against npos, never -1.
Why can't cout print an enum class value directly?
- enum class values are private
- A scoped enum has no implicit conversion to int
- cout only prints strings
- enums are not real types
Answer: A scoped enum has no implicit conversion to int. static_cast it to int, or write a function that maps it to a readable name via switch.
What is the difference between void f(int n) and void f(int& n)?
- The first passes a copy; the second passes a reference so changes are visible to the caller
- No difference at all
- The second is always slower
- The first cannot take an int argument
Answer: The first passes a copy; the second passes a reference so changes are visible to the caller. A reference parameter is an alias for the caller's variable, so mutations persist.
Which header provides std::transform?
- <string>
- <cctype>
- <algorithm>
- <vector>
Answer: <algorithm>. std::transform lives in <algorithm>; toupper comes from <cctype>.
What does to_string(42) produce?
- The int 42
- The std::string "42"
- The char '4'
- A compile error
Answer: The std::string "42". to_string converts a numeric value into its textual std::string form.
Why is 'using namespace std;' discouraged in a header file?
- It slows down compilation only
- It is a syntax error in headers
- It disables the standard library
- It forces all of std into every file that includes the header, risking name clashes
Answer: It forces all of std into every file that includes the header, risking name clashes. Prefer qualified names or targeted using-declarations to avoid leaking the namespace.
What is the value range concern with a plain int holding a very large product?
- int can never overflow
- Signed integer overflow is undefined behavior and may wrap unexpectedly
- int automatically becomes a double
- Overflow always throws an exception
Answer: Signed integer overflow is undefined behavior and may wrap unexpectedly. Use a wider type like long long, or check bounds, when values may exceed int's range.
What does std::stoi("123") return?
- The string "123"
- The char '1'
- The int 123
- A double 123.0
Answer: The int 123. stoi parses a string into an int; stod/stof exist for floating-point values.
Continue this course
- Previous: References
- Next: Vectors (std::vector) — The dynamic array you'll use everywhere
- Quick reference: C++ cheat sheet
Frequently asked questions
What should I do if I get stuck on the checkpoint build?
Go back to the specific lesson the step relates to — reading input is in Input & Output, transforming text is in Strings, and the enum is in Enums. Each step here maps to exactly one earlier topic. The revealed solution is there once you've genuinely tried, not as a first resort.
Is the checkpoint quiz graded?
No. It is for your own confidence. If you can answer all six questions without peeking, you are ready to move on to containers like vectors and maps. If a couple trip you up, that points you to the exact lesson worth a quick re-read.
Do I need to memorize every member function of std::string?
Not at all. You should remember the handful you used most — size, find, substr, and the conversions to_string/stoi. For everything else, knowing it exists and looking up the signature is completely normal, even for professionals.