Walkthrough: Testing a Project (C++)

Running a program in Debug mode enables you to use breakpoints to pause the program to examine the state of variables and objects.

In this step, you watch the value of a variable as the program runs and deduce why the value is not what you might expect.

Prerequisites

This topic assumes that you understand the fundamentals of the C++ language.

To run a program in Debug mode

  1. Click on the TestGames.cpp tab in the editing area if that file is not visible.

  2. Set the current line in the editor by clicking the following line:

    Cardgame.solitaire(1);

  3. To set a breakpoint on that line, on the menu bar, choose Debug, Toggle Breakpoint, or choose the F9 key.

    A red circle appears to the left of a line with a breakpoint set.

  4. On the menu bar, choose Debug, Start Debugging or press F5.

    When the program reaches the line with the breakpoint, execution stops temporarily, because your program is in Break mode. A yellow arrow to the left of a line of code indicates that it is the next line to be executed.

  5. To examine the value of the Cardgame::totalParticipants variable, hover over it with the mouse. The variable name and its value of 12 is displayed in a tooltip window.

    Open the shortcut menu for the Cardgame::totalParticipants variable. Choose Add Watch to display that variable in the Watch 1 window. You can also select the variable and drag it to the Watch 1 window.

  6. On the menu bar, choose Debug, Step Over or press F10 to step to the next line of code.

    The value of Cardgame::totalParticipants is now displayed as 13.

  7. Open the context menu for the last line of the main method (return 0;) and choose Run to Cursor. The yellow arrow to the left of the code points to the next statement to be executed.

  8. The Cardgame::totalParticipants number should decrease when a Cardgame terminates. At this point, Cardgame::totalParticipants should equal 0 because all Cardgame instances have been destroyed, but the Watch 1 window indicates Cardgame::totalParticipants equals 18.

    There is a bug in the code that you will detect and fix in the next section.

  9. On the menu bar, choose Debug, Stop Debugging or press Shift+F5 to stop the program.

Next Steps

Previous: Walkthrough: Building a Project (C++) | Next: Walkthrough: Debugging a Project (C++)

See Also

Tasks

Visual C++ Guided Tour

Other Resources

Building and Debugging