Loading...

C++ Debugging

The enums page left enum class Mark with Ok and Pine, selected Mark::Ok, and printed ok from box.cpp. This page opens the Quality and Wrap-up module on purpose. The workshop file on disk stays box.cpp so you keep one familiar build path. The new work names debugging: careful checking when a result looks wrong even though the program still runs. Drop the enum classes from the checked file. Store int x = 10; and int y = 2;. Print each value with a short label. A temporary minus sum shows 8 on purpose in the prose. The checked file uses plus so the last line is 12. Naming waits for the next page.
Debugging here means printing the facts you stored, reading the wrong result, then changing the one bad operator. Reading std::cout << "x=" << x is reading a labeled input. Reading a temporary x - y is reading the bug that prints 8. Reading the checked x + y is reading the fix that prints 12. Keep #include <iostream> so the prints work. Do not invent a naming rule on this page.
Programs use this habit when the build succeeds but the number on the screen is not the number you meant. You do not open a debugger window on this page. You do not need a new workshop file name. The build command stays familiar: one source file box.cpp, one output name box, one run that prints three lines. You compile only box.cpp again for this workshop file on disk.
Think of debugging as a flashlight on values you already stored. The previous page showed a typed mark list. This page shows labeled numbers around a sum. Do not expect naming in the checked file. The printed x=10, y=2, and 12 through the fixed plus is the whole lesson point.

Above main, keep only the include. Inside main, write int x = 10;, int y = 2;, then print x= with the value, then print y= with the value, then int sum = x + y;, then print sum, then return 0;. Keep #include <iostream> at the top. Do not keep enum class Mark. Do not invent a naming demo on this page.
Walk the run carefully from the first line of main. The program starts at main. It stores ten and two. The first print shows x=10. The second print shows y=2. The sum uses plus, so twelve is stored. The third print shows 12. Then main returns and the program ends. There are three printed lines on the screen: x=10, then y=2, then 12. The checked file never keeps the temporary minus bug.
Say the steps out loud once while you read the file. Store x and y. Print both with labels. Add them with plus. Print the sum. Return. That short story is the whole workshop path. Nothing else belongs in the checked body. No enum. No debugger window. When the screen shows x=10, y=2, and 12, the story matched the source.

If you temporarily write int sum = x - y; and print sum, the screen shows x=10, y=2, and 8. That failed experiment stays in the prose as the clue. The checked file uses plus so the last line is 12.
If you change the operator without printing the labels, you guess in the dark. The checked file keeps the labels so the inputs stay visible.
A common mistake is to treat 8 as a compile failure. Another common mistake is to invent the word naming as a required keyword here. A third mistake is to leave leftover enum class Mark in the file. A fourth mistake is to wait for cin. A fifth mistake is to open a debugger window as if this page required one.
This page stops before the next tool, where clear names make a small program easier to read. The next page names that tool once at the end. The debugging habit you have just written only shows labeled prints and one fixed sum. Keep the printed 12 so the order stays clear for a first careful reading of the file.
Remember that debugging reads facts before it changes a line. You still need a correct operator to finish. After a successful run the screen shows x=10, y=2, and 12. After you undo a bad experiment the checked file returns to that plus path. Stability of the checked file matters as much as learning the print-and-fix habit itself.

Open box.cpp. Remove the enum from the enums page. Write int x = 10; and int y = 2;. Print each with a short label. Write int sum = x + y; and print sum. Do not leave a minus in the checked sum. The program file name on disk stays box.cpp. The checked file is below, and the new habit is debugging with labeled prints.

box.cpp debugging labeled x y plus sum

cpp
#include <iostream>

int main()
{
    int x = 10;
    int y = 2;
    std::cout << "x=" << x << std::endl;
    std::cout << "y=" << y << std::endl;
    int sum = x + y;
    std::cout << sum << std::endl;
    return 0;
}

Rebuild with g++ -std=c++17 box.cpp -o box, then run ./box. If g++ is missing and clang++ is the compiler you installed, swap only the tool name: clang++ -std=c++17 box.cpp -o box. The screen shows x=10, then y=2, then 12.
Check the three printed lines first. The table gives each row its piece, so compare your run with it line by line before you move on to experiments. If the last line is 8, the sum still uses minus. If the labels are missing, you cannot trust the inputs. If old enum code remains, remove it before you trust the sum path.

📊 debugging prints and what the screen shows

Piece What the screen shows
First x=10, because the first labeled print runs
Last 12, because the checked sum uses plus
Rule print labeled inputs, then fix the one bad operator
Screen x=10 then y=2 then 12

Try one change at a time. Temporarily set int sum = x - y;, rebuild, and see x=10, y=2, and 8. Restore plus so the last line is 12 again. Next, temporarily print ready instead of sum, rebuild, see ready, then restore the sum print. Each experiment changes one thing on purpose, so you see either a wrong last number or a different third line. Undo each change before the next one starts, so the file always returns to the checked workshop version with plus and 12 once again after every single careful try back on disk again.
This page ends the debugging tool and stops before the next one. The next page is naming, which names clear identifiers in a small program. The debugging habit you have just written only shows labeled prints of x and y from box.cpp while the fixed plus still prints 12. You leave with x=10, y=2, and 12 on the screen. Count the printed lines before you leave. There are exactly three printed lines on that final screen, and those lines are x=10 then y=2 then 12 alone right there now after the final successful build run on disk today now after the labeled fix check.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
4
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top