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:

    solitaire = new Cardgame(1);
    
  3. To set a breakpoint on that line, on the Debug menu, click Toggle Breakpoint, or press F9. Alternatively, you can click in the area to the left of a line of code to set or clear a breakpoint.

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

  4. On the Debug menu, click 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 is the next line to be executed.

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

    Right-click the totalparticipants variable and click Add Watch to display that variable in the Watch window. You can also select the variable and drag it to the Watch window.

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

    The value of totalparticipants is now displayed as 13.

  7. Right-click the last line of the main method (return 0;) and click Run to Cursor. The yellow arrow to the left of the code points to the next statement to be executed.

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

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

  9. On the Debug menu, click Stop Debugging orpress Shift-F5 to stop the program.

Next Steps

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

See Also

Tasks

Visual C++ Guided Tour

Other Resources

Building, Debugging, and Testing