How to Use the Debugger

What is the debugger?

As you probably know, many times a programmer will encounter coding errors, or “bugs”. Visual Studios automatically lists these compile errors for you. However, sometimes there are runtime errors that Visual Studios cannot detect, and sometimes we are unsure where exactly a program breaks down. The debugger is a useful tool to catch these errors. It allows you to run a program step by step and pinpoint exactly where the program encounters an error. Below are instructions on how to use the debugger.

Before you Start…

How to Add Line Numbers to your Coding Interface

Before you begin to use the debugger, first add line numbers to your coding interface. Visual Studios lists the line number for compile errors so you know which line to fix.

In the Visual Studios toolbar, click Tools. Scroll down and click Options.
Scroll down and expand the Text Editor tab. Under Text Editor, click All Languages.
On the right side of the window, look for the Display section. Check the Line Numbers box and click OK.
You should now have line numbers next to your code. (Quick tip! You can click the region containing line numbers to quickly highlight a line).

How to Read the Output Window

The output window lists compile information and compile errors. By default, the output window is below your code window. Otherwise, in the Visual Studios toolbar, clickView and then click Output.

Once you debug or compile and encounter an error, you should see something like this:

y:\mydocuments\visual studio 2005\projects\hw0\hw0\hw0.cpp(18) : error C2143: syntax error : missing ‘;’ before ‘}’

The first part is the file path. Next to the file is the line number in (). Note that errors may occur right before or after the line number.
After you will see either error or warning. Most of the time your program will run fine with a warning. Next is the error code. Sometimes the explanation of the error may be confusing, so you can always google the error code a better explanation.
After is the error type and error description. Oftentimes it will clearly explain the error. Always check for spelling mistakes before you look for anything else!

The Debugger

Toggling(Switching on or off) Breakpoints

When you run the debugger, breakpoints will automatically pause your code at the specified line. To toggle breakpoints, first look for a gray sidebar on the left edge of your coding window. Click it and you will see a big red dot. This represents a breakpoint. Now your code will run until the breakpoint; it will not run the line where you toggled a breakpoint. Try this for yourself! Toggle a breakpoint and press f5 to start debugging. Run your program as usual. Press shift+f5 to stop debugging. Remember, you can have multiple breakpoints. To delete a breakpoint, just click on it again. You can also delete all breakpoints by clicking Delete All Breakpoints under Debug.
(Quick Tip! Sometimes you might not know where exactly to place a breakpoint. Type cout << “/nLINEBREAK/n” and run your code to visually see what happens at a line!)

Running through your code

After you have toggled breakpoints and your code pauses, you have four options: Continue, Step Into, Step Over, and Step Out.

Continue: runs your code until the next breakpoint or the end of your program.

To understand the next three options, note that C++ is actually composed of code. Visualize the code as a planet. Below us is the core and mantle, which represents the underlying scripts of C++ and its various libraries. Above us is the atmosphere, which represents the code used to initialize .exe and .dll files. In between is the code you write.

Step Into:¬†Literally steps into code. Use this to step into functions nested within functions, such as a function in your main routine. Step into will pause inside the function. Otherwise you’ll step into the C++ scripts, which will probably make no sense to you.

Step Over: This is the most useful. Step Over executes the entire line of your code and pauses at the next line. Use this to run your code line by line.

Step Out: Literally steps out of your code. Use this to step out of functions nested within functions, i.e. to return from a function’s code to your main routine. Step out will also pause outside of the function.

Note Step Into and Step Out will cancel each other out, useful if you accidentally enter the wrong level.