C++ Foundations — Debugging

10 broken programs · hunt before opening the fix list · ← Module home

The hunt protocol (from Lab 00, now yours): for each program — (1) read the code and predict the intended output; (2) find the bug(s) by reading; write down what you’d tell the compiler-finder; (3) compile, read the first error, fix one thing, recompile; (4) classify each bug: compile error / logic error / data-type mistake; (5) one bug-diary line. Only then open the fix list. Hints are per-exercise: use them one at a time.


D1 — The missing period. Doesn’t compile.

#include <iostream>

int main() {
    std::cout << "Start\n"
    int total = 5;
    std::cout << total << "\n";
    return 0;
}
Hint 1

Read the error’s line number — then read the line above it.

Hint 2

Every statement ends with ;. Which one doesn’t?

D2 — The garbage value. Compiles (with a warning). Prints nonsense.

#include <iostream>

int main() {
    int total;
    total = total + 10;
    std::cout << total << "\n";
    return 0;
}
Hint 1

What was inside total before total + 10 ran?

Hint 2

Read the warning: “variable ‘total’ is used uninitialized…”. Initialize at declaration.

D3 — The invisible name. Doesn’t compile.

#include <iostream>

int main() {
    cout << "Hello\n";
    return 0;
}
Hint 1

error: 'cout' was not declared in this scope. Where does cout live? (Which namespace?)

Hint 2

std::cout. Every standard-library name wears the std:: prefix.

D4 — The counter that wasn’t. Compiles. Wrong output.

#include <iostream>

int main() {
    int count = 0;
    count == 1;             // first data item arrives
    count == 1;             // second
    count == 1;             // third
    std::cout << count << "\n";   // expected: 3
    return 0;
}
Hint 1

Run it: prints 0. Three operations happened and changed nothing — what does == do? What did you mean?

Hint 2

== compares and discards the answer. Assignment (or += 1/++) is the updater.

D5 — The one-character bug. Compiles (with a warning). Wrong output.

#include <iostream>

int main() {
    int marks = 50;
    if (marks = 40) {
        std::cout << "exactly 40\n";
    }
    std::cout << marks << "\n";
    return 0;
}
Hint 1

Two surprises: “exactly 40” prints and marks becomes 40 — from one line. What does = do inside the if?

Hint 2

= assigns (the expression’s value is what was stored — truthy). The comparison operator is ==.

D6 — The average that wasn’t. Compiles. Wrong output.

#include <iostream>

int main() {
    int total = 17;
    int count = 2;
    double average = total / count;
    std::cout << average << "\n";      // expected: 8.5
    return 0;
}
Hint 1

Prints 8. The division happened before the storage — in which type?

Hint 2

Both operands are ints → integer division → 8 → converted to 8.0 after. Cast an operand before the division: static_cast<double>(total).

D7 — The vanishing fraction. Compiles. Wrong output.

#include <iostream>

int main() {
    const double HALF = 1 / 2;      // should be 0.5
    std::cout << HALF << "\n";
    return 0;
}
Hint 1

Same family as D6, but hidden inside a literal expression. What types are 1 and 2?

Hint 2

Make one operand floating-point: 1.0 / 2 (or cast). Literals are ints until a decimal point says otherwise.

D8 — The character that lied. Compiles. Wrong output.

#include <iostream>

int main() {
    char digit = '7';
    int value = digit;              // should be 7
    std::cout << value + 1 << "\n"; // expected: 8
    return 0;
}
Hint 1

Prints 56. What is '7' really? (Lesson 2 §2.6: a number in a costume.)

Hint 2

'7' is 55. The standard conversion: digit - '0' (55 − 48 = 7).

D9 — The mutating constant. Doesn’t compile.

#include <iostream>

int main() {
    const int MAX_MARKS = 100;
    MAX_MARKS = 50;                 // teacher scaled the exam
    std::cout << MAX_MARKS << "\n";
    return 0;
}
Hint 1

error: assignment of read-only variable 'MAX_MARKS' — the compiler is protecting the constant. Is mutating it even the right fix?

Hint 2

If the value genuinely changes, it isn’t a constant: make it a normal variable with a proper name — or keep the constant and the decision that uses it.

D10 — The loop-shaped leak. Compiles. Infinite loop (Ctrl+C to stop).

#include <iostream>

int main() {
    int minutes = 5;
    while (minutes > 0) {
        std::cout << minutes << "\n";
    }
    return 0;
}
Hint 1

The condition never changes — what’s missing from the body? (Lesson 2 of the Problem-Solving module: “what moves toward the stop?”)

Hint 2

minutes = minutes - 1; (or minutes--;) at the body’s end.


Fix list (after your own fixes)

# Bug class Fix Lesson
D1 compile — missing ; add ; to the “Start” statement 1.3
D2 logic — uninitialized read int total = 0; 2.11 · M6
D3 compile — undeclared name std::cout 2.8 note + 1.1
D4 logic — == vs update count += 1; ×3 (or count++) 3.2
D5 logic — = in condition if (marks == 40) 3.2 · M4
D6 data-type — int division static_cast<double>(total) / count 4.3
D7 data-type — int literals 1.0 / 2 4.2 · M1
D8 data-type — char code int value = digit - '0'; 2.6 · M3
D9 design — const misuse re-think: variable, or redesign 2.9
D10 logic — missing update minutes--; in the body Problem-Solving §14

Scoring: 8+ found-and-classified alone → ready for the Lab. 6–7 → re-read the linked sections. ≤5 → redo predictions first (they train the reading eye this page tests).


← Module home · Challenges · Lab