Visual C++ Debug Tutorial

QUESTION: How do I set breakpoints, set watches, and set through an executing program line-by-line?

ANSWER: We will use the program DebugMessage.cpp as our example. Open Visual C++.

1. You begin by opening the file DebugMessage.cpp.Visual C++ will then display the file DebugMessage.cpp in the code window

Start
click picture to enlarge

2. Once the file DebugMessage.cpp is open, you build the program, as shown in the Visual C++ Build Tutorial: Select Build | Build Solution or Ctrl+Shift+B.

3. Set a breakpoint at the following line by clicking the cursor on this line, and then clicking on the "hand" (Insert/Remove Breakpoint). A red breakpoint "stopsign" appears to the left of the line of code.

    cin >> number;

Start
click picture to enlarge

4. Run DebugMessage.cpp in debug mode by choosing Debug | Start or F5.

Start
click picture to enlarge

5. Note that execution stops at the point where the breakpoint was set.

Start
click picture to enlarge

6. Set a watch on the variable "number" by typing "number" in the name column of the Watch1 window in the middle of the screen.

Start
click picture to enlarge

7a. Step to the next line using Debug | Step Over (or F10).

Start
click picture to enlarge

7b. And enter your magic number and press Enter.

Start
click picture to enlarge

7c. You will see the yellow arrow (that marks the current line in the code) has moved to the next, non-comment line of code and that "number" in the Watch1 window has the value you typed in for your Magic Number (here 129).

Start
click picture to enlarge

8. Step to the next line using Debug | Step Over (or F10) and see the value of "number" change according to the calculation executed in the assignment statement. Watch the yellow arrow move through the code one line at a time.

Start
click picture to enlarge

9. At this point, the cout statement does do output. It is visible if you switch to the console window. You do not get the "Press any key to continue." prompt.
Note that if you get too enthusiastic with F10 and keep going after the end of the program, you'll fall into Windows C code. So, no harm is done, just ignore it.