IC210 Lab 5 -The Debugger
Pre-lab homework.† None for this lab.
Removing programmer errors with the
Debugger, Or, Zen and the Art of Finding Errors and Understanding Your Code
This lab is extremely important for your programming development (and sanity)
because learning how to use the debugger can make your life easier! If you can
use the debugger effectively, you'll gain a greater understanding of the way
your code works and be able to quickly identify and correct even subtle
problems with your programís control flow logic.
Using the debugger to understand your code
For our first portion of the lab, we'll examine the operation of the Hankel Matrix program
we developed in class.
- Create a new project named
to the project, compile and run. Satisfy yourself that everything is
- In Visual C++, right-click in
an area of the menu bars that has no buttons. This will pop up a window
listing the available toolbars. If it is not selected already, select the Debug
- Put your cursor on the line
of code with
and click on the Run to Cursor icon from
the Debug toolbar. The program will build if necessary, then
begin executing and stop at the line where your cursor is. A yellow arrow
next to your source indicates the next line to execute. The Run to Cursor
option is really handy if you want to stop in a certain spot, and don't
want to wade through every line of code before it to get there.
- Notice that the Visual C++
screen has changed. Two new windows have been opened at the bottom of the
screen, the Watch window and the Call Stack
window. On the bottom of the left-hand window is a tab for locals;
Click on it. (Note: If the Locals or Call Stack
Windows disappear, you can bring them back by going up to the Debug
menu and choosing Windows, which brings up a list of
windows related to debugging. Just find the one you want in the list and
click on it.) Click the Step Over
on the Debug toolbar until the yellow arrow goes past the variable
n. You will see the integer
n displayed with its current value.
If we had declared other variables, they would be shown here as well.
Since we have not assigned anything to
it will have a random value.
- Click the Step Over button to
step through the
lines. Progress will stop on the
cin line. The program is
waiting for you to input
Click on the window in which your program is running ().
Enter a value for
the Program window, and verify that it shows up in the Locals
- In addition to the automatic
addition of variables to the Variables window, it is
possible to manually add variables and expressions. Click on the Watch
1 tab next to the Locals tab, click in the blank
cell on the left-hand side, and enter an expression like
n*4. You should see 4 times the
current value of
whatever you entered.
- Proceed through your program
using Step Over. Observe how the variables change as you
proceed through the loops. Observe how they get created and destroyed as
- Walking through you program
in this manner is very slow. To move faster, we'll revisit the Run
to Cursor button. Position the cursor to the line with the first
curly brace following the first
Click on the Run to Cursor button. What happened (check
the Program Window)? Click on the Run to Cursor
button again and check the output.
- Visual C++ provides two
additional debugging buttons, Step Into
and Step Out.
These buttons will become of use when we cover functions next week. For
now, just know they exist. Note, if you accidentally end up in a screen
full of code from one of the libraries, you can get back to your code by
using the Step Out
button. Note, if you Step Out of
your program, you'll end up in some "disassembled" code. This is
the machine code the exits your program. If you close the window with the
machine code, you should be back to your program.
- To stop debugging, simply
click the aptly named Stop Debugging button.
Use the debugger to find and fix the problems in the following programs:
program to calculate
interest rates only works properly on every other year. Why?
- One of last year's homework
assignments had the students write a loan calculator. This student's
solution to the problem appears
to work, but gives different answers than the instructor's solution. Is
the instructor wrong? (Hint: The instructor is NEVER wrong! What's wrong
with the student's solution!)
If you finish the lab assignment early you should try to
tackle this problem:
program is supposed to
report when two vectors are perpendicular. But, it always reports the
vectors as not perpendicular regardless of the vectors input. For example,
try (2, 20)
and (5, 110).
- Write a program that prints
rectangles (in text) on the screen. The outline of the rectangles should be
* characters and the inside should be empty. In particular,
your program will read in a height, a width, and an offset, and print out
(in text) a rectangle of the appropriate height and width, indented by the
number of spaces specified in the offset. Typical runs of your program might
look like this:
As you write this program, try to make use of the process of stepwise
refinement that we discussed in lecture. Run your program and show your
instructor the output with user input height=10, width=25, and offset=5.
Enter height (greater than 2): 5
Enter width (greater than 2): 8
Enter offset: 3
Enter height (greater than 2): 5
Enter width (greater than 2): 4
Enter offset: 10