We're going to give you some programs and workspaces to start with this time. First download a ZIP'ed copy of the Lab 7 workspaces and store it in the C:\files directory.
Use Powerzip to extract the files in it to the c:\files directory.
This should create a C:\files\lab07 directory and four subdirectories, each with its own project, within the C:\files\lab07 directory. Check the window for c:\files where you should see a new lab07 directory. If you don't see it, make sure you extracted the files to the c:\files directory.
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 an new window with debugger buttons on top of your other windows.
You may have to move the debugger window over to the right to see the window that contains your program.
That window now has a small yellow arrow pointing to the line of code that will be executed next.
Now press F10 to single step
through your code. Notice the yellow arrow move
as you tiptoe through the C code.
You should also see a window displaying the values of objects that are variables within
your program. Hit the Local tab in this window to set it to display
the variable values you need for this lab.
It's best to stop when you get to the closing
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. Hint: what does the assignment operator, '=', do?
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:
If you want your input characters echoed as you type them, you must type the F10 after the arrow reaches the input statement. So use F10 to step to the cin line and then use it again on the line. Then go to the console window and enter the characters. 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.
Use your mouse to move the cursor to the program statement
A red dot should appear just to the left of
the line you chose. This red dot shows you where a breakpoint has been
placed in your code. When you run your program, execution will stop
when the code at the breakpoint is reached. The debugger window will be
displayed, and you will be able to debug you code starting
at the breakpoint.
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. You should examine the variables in the window at the bottom left of you screen to confirm that the values you entered have been stored in p, q, and r, before continuing with either single steps (F10) or regular running (F5).
is exactly the same as the while loop
i = 1;
Build this program and run it. It should first ask you to enter two integers, the
second one larger than the first, and then it should print out all the
integers from the first integer to the second integer. If you enter 3 5, it
should print out
Instead, it gets stuck in an infinite loop. Hit Control-C (hold down the ctrl key and press the C key) to break out of the program. Remember that key combination - there will be infinite loops in your future programs! Now lets use the debugger to find why this program doesn't work.
Hit F11 to start the debugger. Oh, Oh - it wants the source code for the main function in ezwin and we don't have it! Hit cancel and now you'll see a screen full of assembly language! Better not try to step through that! Hit shift-F5 to get out of the debugger. So how do we get past main() to debug our code in ApiMain()?
Remember breakpoints? We need something like a breakpoint to let you run your program till it gets past the ezwin stuff and into ApiMain. Move the cursor to the start of the for loop in ApiMain. Now hit Ctrl-F10; this is the run to cursor command. Now the the programs runs to the the point where you positioned the cursor and stops in the debugger. Now you'll be able to single step through the loop to see why it never ends. Hint: look at the value of the loop counter variable, i, in the debugger's variable window. You'll have to click on the scroll bar on this small window so that you can move it down to where it shows i. Notice that you can see the value of any variable by simply placing your mouse cursor on it, but the value you see that way is the value when you put the cursor on the variable. The value you see in the variable window is always the current value stored in a variable.
OK, i isn't changing the way we think it should. It should start at the value stored l and increment by one each time you step through the loop. Look at the code and watch the value of i as you step through the code to figure out what is happening and why. Once you understand what the program is doing, you have a good shot at figuring out how to make it do what it is supposed to do. If you don't understand EXACTLY what a program is doing, you have very little chance of modifying it so that it will do what you want it to do. Figuring out what your program is doing is what debuggers are all about!
Now how do we fix it? To really fix this problem, we have to fix the code and then rebuild the executable, but we can get out of the infinite loop in order to what the rest of the program does by giving i a value at least as big as m. Place the mouse cursor on m to find its value. Now go to the variable window and double click on the value shown for i. Enter a new value at least as big as m. The debugger lets you singlestep, set breakpoints, see the values in your variables, and even change those values. It also does a bunch of other stuff. This is a great way to find the errors in your code!
If you give i a big value, then when you single step a few times you should leave the loop. If you step off the bottom of the program you'll find yourself back in the ezwin assembly language code again. Hit shift-F5 to get out of the debugger. Fix the program.