Microsoft Visual Studio 2005 allows you to create different applications. This document provides steps for creating a single-file console application: 1) Create a Win32 console application project; 2) Add a source code file; 3) Write a program; 4) Compile and build the program; 5) Execute the program; 6) Debug if errors occur. The basic operations of creating a project file, adding source files, compiling, building, executing and debugging are the same for more complex programs as well.
Microsoft Visual Studio 2005 allows you to create different applications. This document provides steps for creating a single-file console application: 1) Create a Win32 console application project; 2) Add a source code file; 3) Write a program; 4) Compile and build the program; 5) Execute the program; 6) Debug if errors occur. The basic operations of creating a project file, adding source files, compiling, building, executing and debugging are the same for more complex programs as well.
TUTORIAL INTRODUCTION Microsoft Visual Studio 2005/2008 allows you to create many different types of applications. This guide addresses creating and using Console Applications. A console application is a program that runs inside a DOS window. This guide is divided into the following sections: Starting Visual Studio 2005/2008 Creating and Executing a Single-File Console Application STARTING VISUAL Studio 2005 To start the Microsoft Visual C++ compiler, click the Start button on the task bar to open the start menu. Open the Programs menu (All Programs in Windows XP) and Select the Microsoft Visual Studio 2005 folder, then the program Microsoft Visual Studio 2005. If the Microsoft Visual Studio window is not maximized, click the maximize button in the upper right corner of the title bar. Figure 1-1 Microsoft Visual Studio Figure 1-1 shows the initial application configuration setting for the first time. Choose your default environment setting by selecting Visual C++ Development Setting, then click Start Visual Studio Button. The configuration will be done only one time if you are using that computer for the first time. 2 Figure 1-2 shows the initial application window. CREATING AND EXECUTING A SINGLE-FILE CONSOLE APPLICATION One of the most common programs encountered in an introductory C++ programming class is a single source code file that contains the main function. To construct this type of application, you need to: Create a Win32 Console Application Project Add a source code file Write the program Execute the program Debug the program Create a Win32 Console Application Project Start Microsoft Visual Studio 2005. On the File menu select New, and then Project. ) Figure 1-3 Create a new project 3 Figure 1-4 Create a new project settings When the New Project dialog box opens, click on Visual C++ Projects in the Projects Types pane Select win32, and then make sure Win32 Console Application is selected in the Templates pane. Enter the following information (see Figure 1-4): Enter the project name in the Name textbox Select the location for the project in the Location textbox The application provides a default location for saving projects or you can select your own location by pressing the button to the right of the location textbox to open the Choose Directory dialog box. Press the OK button after entering the required information into the dialog box. When the Win32 Application Wizard appears, click next button, click on Application Settings, select Console application from the Application types, uncheck Precompiled header and check Empty project (see Figure 1-5) and then press the Finish button. 4 Figure 1-5 Win32 Console Application Wizards The Solution Explorer now contains the information about your project (see Figure 1-6.) Figure 1-6 Empty Win32 Console Application Creating and Executing a Single-File Console Application Figure 1-5 Win32 Console Application Wizard Figure 1-4 New project settings Select project type Select location for the project file Select project Enter project name Add a Source File Add a blank source code file to your project. On the Solution Explorer select Source files folder, Right click, from the pop up menu, click Add, and select New Item. (See Figure 1-7.) 5 Figure 1-7 Create new file Creating and Executing a Single-File Console Application When the New File dialog box appears, select Visual C++ in the Categories pane, click Code to select it and make sure C++ File (.cpp) in the Templates pane is selected. Type the file name. The file name can be the same name as the project, then type in a name (the .cpp extension will be added automatically.) Click Add. Figure 1-8 Create new file settings Blank project file is added 6 Figure 1-9 Naming project files To save the file with a specific name, on the File menu select Save Source1 As.When the Save File As dialog box appears (see Figure 1-10), select the folder to hold the file (it generally should be the same as the project name) from the Save in dropdown window, then type in a name (the .cpp extension will be added automatically.) Press the Save button to complete the action. Write the Program Type the source code for your program into the blank editing pane located in the right side of the window. Figure 1-10 contains the source code for a simple C++ console programthat displays the phrase, Hello Computer Science I. \n My names are Student Names \n. Note that the C++ editor automatically selects different colors for C++ reserved words and provides automatic indentation for blocks of code. Figure 1-10 HelloCS source code file Compile and Build the Program Before you can execute the program you have written, you must save it and compile it. To save the file, either click in the edit window to make it the active object or click on the hellocs.cpp filename in the Solution 7 Explorer, the select Save hellocs.cpp from the File menu. Once the program is saved, you compile it by selecting the Build Solution option from the Build menu. (See Figure 1-11.) This will cause Visual Studio to both compile and link your program. Once the program compiles and links, the results will be shown in the Output window at the bottom. (See Figure 1-12.) In this example, the program compiled with no errors and no warnings. Figure 1-11 Compiling a program Figure 1-12 Executing the program Creating and Executing a Single-File Console Application 11 After the build process has successfully completed, you can now execute the program by selecting Start Without Debugging from the Debug menu. (See Figure 1-12) The program results will appear in a new DOS window. Notice that the phrase Press any key to continue has been added to the program output. This additional code was added to keep the DOS window open until you have had a chance to view the output and press a key on the keyboard. (See Figure 1-13.) Once a key on the keyboard is pressed, the 8 program stops execution and the DOS window closes. This output phrase is actually included in your program, as you would see if you opened a DOS window and executed the program directly. Figure 1-13 Results of Executing the program Debug the Program Often, the program will have errors when you attempt to compile, build, and execute it. The program that was successfully executed in the previous example has been modified to include an errorthe semi-colon at the end of the return 0 statement has been removed. When the modified program is compiled, an error is displayed in the output window. (See Figure 1-14.) You can determine where the compiler found the error by scrolling up in the Output window until the specific error message is visible, then by double-clicking on the error message in the Output window. This will cause a pointer to appear in the left margin of the source file where the error was encountered. (See Figure 1-15.) Notice in this case that the pointer is on the line after the line containing the actual error. This occurs when the error induces a compiler recognized fault on a subsequent line. While not always exact, the error pointer, in conjunction with the error description, can help locate errors in the source code. Once the error is identified and corrected, the program must be saved, rebuilt, and executed again. 9 Figure 1-14 Compile error Error pointer Double click on this Figure 1-15 Locating error SUMMARY Microsoft Visual Studio 2005 allows you to create many different types of applications. This guide addressed creating and using single source file Console Applications, but the basic operations are the same for more complex programs. Always start by creating a project file of the appropriate type Create blank files for writing your own programs Compile Build Execute Debug, if necessary