Object-oriented Programming

Chapter 3 - Writing, Compiling, and Debugging Simple Programs

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Running Your First Program
To run a program after you have completed a successful Build or Rebuild All operation, simply click on the Project menu’s Execute command. If you do this with the sample program, and enter a Y when asked if you would like to continue, your screen should display something like the following:
-858993460



0

-858993460

-858993460

-858993460

-858993460

-858993460



Welcome to a trace demonstration!

Would you like to continue (Y/N) y
Figure 3-15 illustrates what happens after you type the Y and press enter.
Figure 3-15: Run-time error message window
Using the Integrated Debugger
The sample program’s output begins by dumping the uninitialized contents of the array. It then asks if you want to continue. A Y (yes) answer logically indicates that you would now like to fill the array with your own values and then reprint the array’s contents to the screen.
In this sample execution, you responded with a Y. However, if you examine the program’s output, you can easily see that you were never prompted for input. In addition, the array’s contents have not been changed, as evidenced by the duplicated output.
In other words, although you have a program that appears to be syntactically correct—there are no syntax errors—the application fails to perform as expected. These types of errors are called logical errors. Fortunately, the Visual C++ IDE integrated debugger has several features ready to come to your rescue.
Although the integrated debugger has many features, you will regularly use only a small subset of the commands. Basically, a debugger provides two powerful capabilities. First, it allows you to execute your program line by line, instead of at full speed. Second, it allows you to examine the contents of any variable at any point in your program.
When used correctly, these capabilities allow you to quickly locate an offending line of code. Unfortunately, the debugger does not automatically correct the code. (So, for the moment, your job security as a programmer is still not threatened!)
The Subtle Differences Between Step Into and Step Over
Figure 3-16 shows the Start Debug menu. When you start the debugger, usually by pressing f11, a Debug Toolbar appears. Two of the more frequently used buttons represent the options Step Into (fifth button from the left on the Debug Toolbar) and Step Over (sixth button). Both commands execute your program line by line.
Figure 3-16: The Debug menu
The appearance of the edit window is different if you are using either one of these commands. When you are debugging a program using Step Into or Step Over, the integrated debugger highlights the line of code about to be executed.
The only difference between Step Into and Step Over occurs when the statement about to be executed is a function call. If you select Step Into on a function call, the debugger jumps to the function header and continues debugging the code inside the function. If you select Step Over on a function call, the debugger executes the associated function at full speed and then returns to the statement following the function call. You should use this command whenever you are debugging a program that incorporates previously tested subroutines.
Using either Step command, invoke the command three times. Figure 3-17 shows the sample program as it will appear after you have invoked Step Into or Step Over three times.
Figure 3-17: Viewing the window after three Step into or Step Over operations
As you can see from Figure 3-17, the single-step arrow (also called a trace arrow) is positioned next to the call to the print_them( ) function.
For now, we want to execute the function at full speed. To do this, choose the Step Over command now. If you watch closely, you will notice that the function executes and the trace arrow stops on the first printf( ) statement. So far, so good. Now press f10 three times, until the trace arrow stops on the scanf( ) statement.
At this point, you need to switch to the program’s execution window. You can do this by pressing the ALT-TAB key combination. (You may need to use this key combination several times, depending on the number of tasks you have loaded.) When you are in ERROR’s window, as shown in Figure 3-18, answer the question “Would you like to continue (Y/N)” with a Y and press ENTER.
Figure 3-18: Select “Y” to continue with the ERROR.C program
The integrated debugger immediately responds with the error message shown in Figure 3-19.
Figure 3-19: The debugger reports an error
This message relates to the scanf( ) statement just executed. See if you understand enough of the C language to figure out what the problem is.
The problem relates to the incorrect use of the scanf( ) function. The scanf( ) function expects to receive the address of a memory location to fill. Examine this statement:
scanf(“%c”,continu);
As you can see, this statement does not provide an address. The solution is to place the address operator (&) in front of the variable continu. Correct the statement so that it looks like this statement:
scanf(“%c”,&continue);
Save the change and execute a Rebuild All.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net