CSCI 201 Lab 5 -- Debugging in Visual C++

This week you try out the Visual C++ debugger, but first take a look at a sample of your efforts in Lab 4 by looking at the Faces of CSCI 201 page.

Getting ready

First of all, if a directory called C:\FILES\Lab05 presently exists on your computer, delete it.

We're going to give you some programs and workspaces to start with this time. First download a ZIP'ed copy of the Lab 5 workspaces and store it in the C:\FILES directory.

Now, go from the Start button into the MS-DOS prompt and execute the following commands:

This should create five new directories, each with its own project, within the C:\FILES directory. If you didn't see all these files being inflated when you ran the pkunzip command, you need to see your instructor.

Initial debugging

Start up Visual C++ and open, via the File » Open Workspace menu choices, the workspace c:\files\Lab05\Zero\Zero.dsw. Build the program.

Now we are going to use the debugger to step through your program. Warning: Debuggers are not easy to master, but you'll save yourself a great deal of time if you learn to use them.

Start the debugger by pressing the F11 key. You will see some flashing windows, but should finally see a window that contains your program with a small yellow arrow pointing at the line of code the will be executed next.
Initial debug window

Now press F10 to single step through your code. Notice the yellow arrow move as you tiptoe through the C code. You may (depending on how your Visual C++ is installed) also see windows displaying values within your program.
Variable display window

It's best to stop when you get to the closing curly brace.
End of the line

You should stop the debugger with the menu selections Debug » Stop Debugging or the shift-F5 key combination. However, there's a good chance that you'll someday you'll go to far and find yourself debugging some Intel assembly code. If you see something like "add esp,0Ch", you should immediately shift-F5 to stop the debugger.

Now set the value assigned to x in its declaration to 1, rather than 0. Now step through the program and see if you execute the other branch of the if-then-else.

What? You didn't. Well, there's a bug in your program. Fix it.

Instructor check-off 1

Show your instructor the fixed code.

Debugging with I/O

Go through the file menu File » Close Workspace to close the file project. Now open the workspace c:\files\Lab05\Nand\Nand.dsw and study the program.

Start debugging with F11 key and then use F10 to step through the code. The interesting twist occurs when you get to the input statement:

No matter how much you F10, the yellow arrow won't move. You need to find the console window for your program and give it the input it wants. ALT-TAB might reveal the console window. If that doesn't work, look for the application called simply Nand at the bottom of your screen.

If you want your input characters echoed as you type them, you must type the F10 after the arrow reaches the input statement. If you go back to the console while the arrow is sitting on the input statement without pressing F10, you will not see the characters you type.

Continue to step through the program until you reach the end.

Instructor check-off 2

Show you instructor that you managed to reach the return statement while debugging the program.

Setting a breakpoint

Close the Nand workspace and open the workspace c:\files\Lab05\Nested\Nested.dsw. Again look over the program.

Use your mouse to move the cursor to the program statement

Now start to add a breakpoint with the file menu selections Edit » Breakpoints. This will display a dialog box.
Breakpoint dialog
Press on the right-pointing arrow to the right side of the empty text field below the words Break at. You should then be given a choice between a line number, like 13, and Advanced. Go for the line number and then click OK.

A red dot should appear just to the left of the line you chose.
Breakpoint dialog

Now press the F5 key or use the menu choices Build » Start Debug » Go to begin execution of your program. Your code should be executed up to the breakpoint you just set. At that time, you should see the yellow arrow on top of the red dot. At that point, you can continue to use F10 to single step or you can return to warpspeed with F5.

Instructor check-off 3

Set two more breakpoints in your program and show your instructor the dots.

Fixing a program

Open again close your present workspace and open up a new one from c:\files\Lab05\Overlap\Overlap.dsw. This project has some problems. The EzWindows library hasn't been added and there's some ???'s left in the program Overlap.cpp that need to be filled in with the right expressions.

Instructor check-off 4

Run the fixed program for your instructor.

Note to lab instructors: If time is running short, drop this part of the lab.

Return to CSCI 201 page
CSCI logo Return to the UNCA Computer Science home page