Share via


Debugging a Project (C+)

In this step, you modify the program to fix the problem that was discovered when testing the project.

Prerequisites

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

To fix a program that has a bug

  1. To see what occurs when a Cardgame object is destroyed, view the destructor for the Cardgame class.

    On the View menu, click Class View or click the Class View tab in the Solution Explorer window.

    Expand the game project tree and click the Cardgame class.

    The area underneath shows the class members and methods.

    Right-click the ~Cardgame(void) destructor and click Go To Definition.

  2. To decrease the totalparticipants when a card game terminates, type the following code between the opening and closing braces of the Cardgame::~Cardgame destructor:

    totalparticipants -= players;
    cout << players << " players have finished their game.  There are now "
    << totalparticipants << " players in total." << endl;
    }
    
  3. The Cardgame.cpp file should resemble this after your changes:

    #include "Cardgame.h"
    #include <iostream>
    using namespace std;
    Cardgame::Cardgame(int p)
    {
        players = p;
        totalparticipants += p;
        cout << p << " players have started a new game.  There are now "
             << totalparticipants << " players in total." << endl;
    }
    
    Cardgame::~Cardgame(void)
    {
        totalparticipants -= players;
        cout << players << " players have finished their game.  There are now "
             << totalparticipants << " players in total." << endl;
    }
    
  4. On the Build menu, click Build Solution.

  5. On the Debug menu, click Start Debugging or press F5 to run the program in Debug mode. The program pauses at the first breakpoint.

  6. On the Debug menu, click Step Over or press F10 to step through the program.

    Note that after each Cardgame constructor executes, the value of totalparticipants increases. After each pointer is deleted (and the destructor is called), totalparticipants decreases.

  7. Step to the last line of the program. Just before the return statement is executed, totalparticipants equals 0. Continue stepping through the program until it exits or on the Debug menu, click Continue or press F5 to allow the program to continue to run until it exits.

Next Steps

Previous:Testing a Project (C+) | Next:Deploying Your Program (C+)

See Also

Tasks

Visual C++ Guided Tour

Other Resources

Building, Debugging, and Testing