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
Creating Your First Program
The first thing you need to do before you enter a program is open a new file. From the File menu, choose the New... menu item. This option opens the New dialog box shown in Figure 3-1.
Figure 3-1: The New dialog box allows the programmer to start a new program
This dialog box is used to select the type of file you wish to create. For our example, click on the Win32 Console Application. Once you have given the project a name, the Visual Development Studio presents you with the Application options seen in Figure 3-2.
Figure 3-2: Selecting a Win32 Console Application type
For this example, choose the empty project radio button and click on Finish. To open a clean edit window, click on the Page icon (first icon on the extreme left of the Edit Toolbar). With a clean editing area, you are ready to begin entering a program. Enter the following example program:
/* NOTE: This program contains errors */ /* entered for the purpose of teaching */ /* you how to use the Integrated Debugger! */
#include <stdio.h>
/* The following symbolic constant is used to dimension the array */ #define SIZE 5
/* Function Prototype */ void print_them(int offset,char continue,int iarray[SIZE]);
void main( void ) { int offset; /* array element selector */ int iarray[SIZE]; /* integer array */ char continue = 0; /* used to hold user’s response */
/* First function call prints variables “as is” */ print_them(offset,continue,iarray);
/* Welcome message and input of user’s response */ Printf(\n\nWelcome to a trace demonstration!"); printf(“\nWould you like to continue (Y/N) ”); scanf(“%c”,continue);
/* User-input of new integer array data */ if(continue == ‘Y’) for(offset=0; offset < SIZE; offset++) { printf(“\nPlease enter an integer: ”); scanf(“%d”,&iarray[offset]); }
/* Second function call prints user-entered data */ print_them(offset,continue,iarray);
}
/* Function outputs the contents of all variables */ void print_them(int offset, char continue, int iarray[SIZE]) { printf(“\n\n%d”,offset); printf(“\n\n%d”,continue); for(offset=0; offset < SIZE, offset++) printf(“\n%d”,iarray[offset]); }
Enter the program exactly as you see it. If you are familiar with the C language, you will notice that there are errors in the program. Do not correct them. The errors were placed there specifically to give you hands-on experience with various features of the integrated environment.