First, we'll have you download and compile a sample program. We'll then help you learn to use the debugger to watch the program work.
Let's begin by doing a little setup. First, open MSVC by double-clicking the MSVC 5.0 icon on the desktop. Follow these steps to build a new workspace:
To start out, just run the program a couple of times to see how it behaves. This is a simple car insurance program that isn't that hard to understand. For this to make sense, you will want to do the insurance calculations by hand and compare with the results the program gives. There are (at least) two bugs that you should notice right away. First, you should notice that no matter what type of vehicle you say that you have, you are charged for the sports car rate. Also, you notice that if you have both accidents and violations, the premium is computed correctly, but if you have only accidents or violations, then you are not penalized properly.
Here is a quick overview of how a typical debugging session takes place. You start by telling the debugger where in your code you'd like it to interrupt your program's execution so that you can examine the state of its variables. This is done by setting break points. Next, you start your program in a special way so that the debugger has control over its execution. When your program reaches one of your breakpoints, its execution stops and your source code is displayed with an indication of which statement will be executed next. At this point you can examine the current values of any or all variables, and then execute statements one at a time while you watch your variables change.
Here are some steps you can follow to do this yourself:
Take a close look at this box-- this is really useful information. At the bottom of the Variables window, select the Locals tab. It now lists all of the local variables for this function (main, at this point) along with their values. Resize this box and the box for hw2_soln.c so you can see both at the same time. What value to these variables have?
Look at the variables box again. Note that the list of variables shown has changed, and now include only the local variables and parameters for this function. Note the value of fee, then use the Step over button to watch the program find the fee for a sports car and apply the ABS discount. When you get to the bottom of the function, click Step over one more time and you'll return to main(). Click again, and you should see the value of premium change to the value that vehicle_fee() calculated.
What happened? Depending on your computer's setup, you probably got a
long delay followed by something you didn't recognize and don't understand.
You might have gotten dis-assembled machine code, or if you were lucky, you
got to see some of the source code that Microsoft wrote to implement the
scanf function. Either way, you need to get back to Kansas where Aunty Em is
keeping you program. To do this, click the Step out button
.
Step out finishes whatever function you're currently in and takes
you back to wherever you were when the function call was made. In this case,
your program window should pop up and ask you to select a coverage level.
Enter one, and you will be once again land back in your source code.
If you have stepped through enough of the program and just want it to continue running, simply click the go button again. This will cause execution to continue from the current statement until it reaches another breakpoint, or the end of the program is reached.
Another useful button to know about it the Stop debugging button
.
You might use this if, as you were stepping through your code, you found the
problem you were looking for. You can click Stop debugging to
immediately halt the execution of your program so you can make changes,
recompile, and try again.
At this point, you should have enough tools to let you make good use of the debugger. It is worth pointing out that you can place breakpoints almost anywhere you like. Since you already know that the adjustments to the fee for accidents and traffic violations are not calculated correctly, you might guess that it would be good to put a breakpoint on the first executable line (executable lines are the ones after the variable declarations) of the function driving_adjustments(). Do this, and restart the debugger. Use the step commands and the variable windows to discover the problem with the program.
There are certainly other useful debugger features that you could use, but this is enough to get you going. To learn about more of these, try searching for debugging commands from the help menu.