Lemur zaprasza
- Project 1 - Programming with Visual C++ In this lesson, you learned about the fundamentals of the Visual C++ compiler and how to enter and edit programs using Visual C++'s workbench. You saw the following: Introduction to programming concepts How C++ compares to other languages Starting Visual C++ Using Visual C++'s menus How to enter and edit Visual C++ programs How to compile Visual C++ programs Stopping Visual C++ This project reviews entering and editing a Visual C++ program using the workbench. You are not expected to understand how the program works. The format of future projects will concentrate much more on Visual C++'s language specifics using a different project format. Step 1: Start Visual C++. Before using Visual C++, you must start the workbench. Follow these steps: Turn on your computer. Start Windows by typing WIN (if it does not automatically start Windows). For Windows 3.1, choose the program group Microsoft Visual C++. Double-click on the icon titled Visual C++. For Windows 95, click on the Open button, choose Programs from the popup menu, and select Microsoft Visual C++. Click on Visual C++. Step 2: Open a program window. The Visual C++ workbench is known as an MDI application. You enter and edit programs in one or more windows that appear in the workbench. Before typing a new program, you must open a new file in a new window. Type Alt+F,N (File New) to open a new file in an editing window. Although I describe keystrokes to issue the commands, feel free to use the mouse to select menu options. Neither is a better way to use Windows; just use the way that is easiest for you. Type the following program. When typing the program, be sure to press Enter at the end of each line (including the very last line). Type as accurately as possible so that no mistakes appear later. The program takes more lines than will fit in the workbench editing window, so you'll see the workbench scroll downward when you fill the open window. Project 1 Listing. The Visual C++ project program. // Filename: PROJECT1.CPP // Prints the first 20 odd, then even, numbers. // Once done, it prints them in reverse order. #include <iostream.h> void main() { int num; // The for loop control variable cout << "The first 20 odd numbers:\n"; for (num = 1; num < 40; num += 2) { cout << num << ' '; } cout << "\n\nThe first 20 even numbers:\n"; for (num = 2; num <= 40; num += 2) { cout << num << ' '; } cout << "\n\nThe first 20 odd numbers in reverse:\n"; for (num = 39; num >= 1; num -= 2) { cout << num << ' '; } cout << "\n\nThe first 20 even numbers in reverse:\n"; for (num = 40; num >= 2; num -= 2) { cout << num << ' '; } return; } When you finish typing the complete program, you can use PageUp, PageDown, and the arrow keys to scroll the top of the program back into view. Step 3: Compile and run the program. Before running the program, you must compile it. Choose Project | Build, (Shift+F8) which is an alternative to Project | Execute when you know the program needs to be made. If errors appear, the status bar will display an error count. Pressing F4 guides you through the code and leaves you near where the errors are. You can fix any errors that might appear. Recompile the program when you type it exactly as the listing shows. If no errors appear, the error count will be zero and you can then press Ctrl+F5 to execute the program. Here is what you'll see in the QuickWin window: The first 20 odd numbers: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 The first 20 even numbers: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 The first 20 odd numbers in reverse: 39 37 35 33 31 29 27 25 23 21 19 17 15 13 11 9 7 5 3 1 The first 20 even numbers in reverse: 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2 Close the output window and return to the workbench's editing window (use Alt+Tab or click on the workbench window if you do not directly return to the workbench). Step 4: Save your work. If you exit Visual C++ without saving your program, you'll lose the program and have to reenter it in order to see the results again. Therefore, you'll want to save your programs to a disk file. All of the programs in this book, including the one shown in Listing 1, are stored on the enclosed program disk. You don't have to save this listing unless you want the practice, because it is already on the disk. To save a program, select Alt+F,S (for File Save). You'll see the File Save dialog box appear on the screen. Type a program name along with a path name if you want to store the program in a subdirectory. All Visual C++ programs should end with the .CPP filename extension. Step 5: Exit Visual C++. Select Alt+F4 (for File Exit) to close Visual C++. If you did not save your program, Visual C++ tells you with a final dialog box that gives you one last chance to save the program. After saving the program, you can exit Visual C++ and switch off your PC. You should always return to DOS with Windows 3.1 or shut down your PC under Windows 95 before powering off your computer. |