Checkpoint: C++ Basics

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.

Learn Checkpoint: C++ Basics in our free C++ course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick recall.

Part of the free C++ course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

The build challenge below is a small "greeter" program broken into three clearly labelled steps. Each step exercises a different lesson:

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.

Now do it yourself. Three lines are replaced with ___ — fill each one using the hints in the comments.

Give it a genuine try first. When you're ready, expand to compare the three completed lines.

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

⏱ Timed Quiz

Answer each from memory, then reveal to check. No peeking until you've committed to an answer.

1. Why does 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.

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.