Lemur zaprasza
What You'll Learn Starting Visual C++ The Parts of the Screen Entering a Program The Disk and Your Programs Running the Program Stopping Visual C++ Get Ready, Get Set, Go! Homework General Knowledge Find the Bug Extra Credit - 2 - Overview of C++ Programs dialog box editing window workbench menu What You'll Learn Starting Visual C++ The parts of the screen Entering a program Stopping Visual C++ Get ready, get set, go! The beginning programmer needs to master only the basics of the Visual C++ environment. Visual C++ includes the Visual Workbench, a full-screen editor, compiler, linker, and debugger that offers a standard interface from which you can do almost anything you will ever want to do in Visual C++. This unit takes a quick look at how you start Visual C++ and use it to enter and run programs. You will enter a couple of short programs, compile them, fix their problems, run them, and save them to the disk drive. This unit is not about the Visual C++ language itself; that topic is left for the rest of the book. This unit's job is to get you comfortable with the mechanics of entering and editing programs, so that those tasks will be behind you and you can then concentrate on the most important thingthe Visual C++ programming language. Starting Visual C++ Learn to start the Visual C++ compiler. Because of the wide assortment of computer configurations, there is no way to describe the start-up sequence of every person's Visual C++ programming environment. Nevertheless, you probably have installed Visual C++ (or had a system administrator, coworker, or friend do it for you) using Appendix A, so your Visual C++ should work exactly as described here. If you find that this section's Visual C++ start-up instructions do not work on your computer, you might have to ask someone familiar with Windows to help you. Getting Visual C++ to start properly does not require an understanding of C++, but it requires a working knowledge of Windows. Most people who own Visual C++ should adhere to the following steps to start Visual C++. If you are running Windows 3.1: Turn on the PC if it is not already on. If your PC does not automatically start Windows, type WIN at the DOS prompt. Find the Microsoft Visual C++ program group in Program Manager. Double Click on the Visual C++ icon. This will start the Visual Workbench. You should see the screen shown in Figure 2.1. If you are running Windows 95: Turn on the PC if it is not already on. Press the Start button on the status bar. Click Programs from the popup menu. Click the Microsoft Visual C++ option, and a further menu will pop up. Click the Visual C++ item. This will start the Visual Workbench. You will see the screen shown in Figure 2.1. Figure 2.1. The Visual C++ workbench. Before going further, you should know that this book's Visual C++ compiler is limited in some respects compared to other full-functioning C++ compilers available from Microsoft. None of these limitations will hamper your learning C++; the enclosed Visual C++ compiler is extremely powerful and contains a programming system unheard of just a decade ago. Nevertheless, the following items somewhat constrain the enclosed Visual C++ system: The compiler only supports 16-bit programming. The programs you produce can run on both Windows 3.1 and Windows 95. They are not true Windows 95 programs. This Visual C++ version is a Windows 3.1 program. It runs perfectly under Windows 95 and Windows NT The product comes without printed documentation. However, there are comprehensive help files accessible from the Workbench help menu that cover all aspects of C++ programming. The rest of this unit takes you through a guided tour of your new Visual C++ compiler. Learn to start your Visual C++ compiler so that you can begin writing programs as soon as possible. The Parts of the Screen It's important to recognize your Visual C++ screen elements inside the workbench. definition A menu is a list of operations displaying things you can do. The workbench includes a menu bar across the top of the screen with menus such as File, Edit, and so on. You can select any menu in one of two ways: Press the Alt key, and then press the underscored letter of the menu you want to choose. If you have a mouse, move the mouse pointer over the name of the menu you want to select and click the left mouse button. Either action displays a menu that falls down from the menu name you selected. The menu you see when you select from the menu bar is called a pull-down menu because it acts like a rolling window shade that you can pull down. The list of items you see on the File menu relates to your program's file, which you eventually write and save to the disk. To select from a pull-down menu after you have displayed it, move the mouse pointer over the menu option you want to select and click the left mouse button. You also can press the up-arrow and down-arrow keys to move the highlight to the option you want to select, and then press Enter to select that option. Table 2.1 gives you an overview of the various Visual C++ menus on the menu bar. During the early part of your Visual C++ programming career, you will have little use for most of the menu options on the various pull-down menus. Table 2.1. An overview of the menu bar's selections. Menu Description File File-related commands that operate on your program file, such as loading a program from disk, saving a program you enter to disk, and printing the program. Edit Includes options that aid in adding, changing, searching for, and deleting text from the current program. View Options for moving around the editor and controlling the display. Project This menu includes the Build command, which runs the compiler and linker, and the Execute command, which enables you to test your program without leaving the workbench. Browse Used for examining C++ classes. Debug This pull-down menu includes the commands that enable you to examine the code as it runs to help you get errors out of your program. Tools Special features that can be added to the workbench (not available as installed). Options Options for setting up the workbench to better suit the way a programmer works. Window Standard Windows menu for selecting and arranging windows. Help Visual C++ provides a rich assortment of online help. Instead of having to look in a bulky Visual C++ reference manual, you can select from the various help-related topics to find anything you need about the workbench or Visual C++. Not all of the pull-down menu options are available at all times. For instance, if you select the Edit pull-down menu, you'll notice that most of its options are grayed. Visual C++ is telling you that the grayed options are not active given your current environment. For example, if you've just started Visual C++ and have not yet entered a program, it makes no sense to copy or paste text because there is no text to work with yet. The menu lets you issue commands to Visual C++. Entering a Program You'll do most of your work inside the editing window. The large area in the middle of the Visual C++ workbench is the editing window in which you type and edit your C++ programs. If you just started Visual C++, chances are good that the large middle portion of the screen has nothing in it. To begin your first foray into the Visual C++ environment, select New from the File pull-down menu. This selection informs Visual C++ that you want to create a new program, and if a program is already in the editing area, you want to discard it (after making sure that the programmer does not want to save changes he or she made to this file). After you select File | New, an editing window opens in the workbench. This editing window takes up only some of the screen. If you want the editing window to fill the screen (most programmers do), you can resize it by double-clicking the title bar or pressing the maximize button in the top right corner. The resizing action is a standard part of Microsoft Windows. If you do not understand how Windows works, ask someone how you perform these tasks or refer to your Windows documentation. Visual C++ is a Multiple Document Interface (MDI) application. It follows the standard Windows rules for handling several windows at the same time. You can edit more than one file at once. To get started with editing, enter the following program (EDIT.CPP) exactly as you see it. As you enter the program, you will notice that the Visual C++ editor works much like a simple word processor. Although Visual C++ includes many powerful editing commands such as copy, cut, paste, and multiple window editing, you need to remember only these basic editing tips to get started: Use the arrow keys to move the cursor (the blinking line) around the screen. The cursor for editing is different from the mouse pointer. You can click the mouse pointer to set the edit cursor to a place in the text, but it is always the cursor that gives the current text position. If you want to delete a character, position the cursor over the character to delete and press the Delete key. The character disappears, and the characters to the right shift to the left, filling in the hole left by the deleted character. If you want to insert a character, position the cursor where you want to insert and type the new text. The first time you do this, you will be in insert mode, meaning that the characters to the right of those you type shift to the right and make room for new ones. If you press the Insert key, Visual C++ changes to overtype mode, meaning that the new characters you type replace those on the screen. The Insert key toggles you back and forth between the two modes. The OVR indicator on the status bar tells you what mode the editor is in currently. // Filename: EDIT.CPP #include <iostream.h> void main() { cout << "Visual C++ is easy!" } The Disk and Your Programs As soon as you have typed a program into the editor, you should save it to the disk. Then you can load it back into the editor any time you need it, and it is also safely tucked away in case of power failure. Use options from the File pull-down menu to save and load programs. To save the program you just typed, select Save from the File pull-down menu. When you do, you will see the dialog box shown in Figure 2.2. Dialog boxes are not menus, but they do offer choices of things you can do next. Visual C++ requires you to save the program to a filename. Visual C++ makes a guess as to what you want to name your file, but it is a lousy guess. Press the Backspace key to get rid of the name Visual C++ selects (UNTITLED.1) in the File Save As entry box. Figure 2.2. Preparing to save your program. As with all programs in this book, the assumed filename is embedded at the top of each program after the word Filename:. This program's filename happens to be EDIT.CPP, but you can name it anything you want as long as you follow the naming conventions required by DOS. Using the File Save dialog box, you could select a different disk drive and directory if you wanted to save your program in a directory different from that chosen for you. For now, do nothing more than type EDIT.CPP into the top box where the cursor is sitting, and press Enter or click OK. Visual C++ then saves the program to disk. There is a deliberate error in the program, but Visual C++ won't notice this as you save the program. You have to ask Visual C++ to check the program by compiling it. This version of the compiler does not support the long filename convention used by Windows 95. Whenever you want to start a new program and have saved the old one to disk, select File | New and Visual C++ gives you a clean slate to start another program. Use File | Close to remove a program from the screen that you no longer want to view. Visual C++ will ask you whether you want to save it if you have made changes. If you want to load a program from the disk, you can select File | Open and type the filename of the program you want to run. Visual C++ loads the program, and you are on your way. Running the Program In the early days of computing, you had to compile, link, and execute your program to run it. Visual C++ makes these steps a snap by automating them with a couple of keystrokes. To run the program, select Execute from the Project pull-down menu (Ctrl+F5 is the shortcut for running the program). When you do this, Visual C++ inspects the program file and the program it should build to decide what it needs to do to build the program. In this case, it decides that the program does not exist and asks you whether you want to build it (which you do, of course). When you answer yes to its question, Visual C++ compiles your programthat is, it attempts to. After a brief pause, Visual C++ displays its output window (like the one in Figure 2.3) with several messages. The last message is displayed in the status bar too. It says EDIT.EXE - 1 error(s), 0 warning(s) Figure 2.3. Finding a problem. Visual C++ found an error with the program, and until you fix the error, Visual C++ will not be able to run it. When it finds an error, Visual C++ notes where it found the error. When you double-click the actual error message or press the F4 key, it tries to highlight the offending line. In this case, it almost did so (it highlighted the line below the actual problem). The statement beginning with cout does not have a semicolon at the end of it as Visual C++ requires. You can fix the error by moving the cursor to the end of the line and typing a semicolon. After you add the semicolon, run the program again by pressing Ctrl+F5 or by selecting Execute from the Project pull-down menu. When you do, Visual C++ compiles and links your program. Now press Ctrl+F5 again; this time, a window pops up with your program's message, Visual C++ is easy!. To work nicely with Windows, Visual C++ builds a little home for your program to run in. Windows programming is very complicated. To enable the beginner to get the benefits of working in the Windows environment without the complications of writing programs, Microsoft provides a feature called QuickWin, which allows simple programs to run under Windows. After inspecting the output of your program, you can remove the results by using the standard Windows actions to close a window, or by pressing Ctrl+C. QuickWin is a piece of code that is added into your program automatically by the linker. All of these keystrokesCtrl+F5, F4, and so forthseem to be a lot to remember. The good news is that you don't have to remember them. They are always available on the pull-down menus. While you are getting comfortable with Visual C++, rely on the menus as much as possible. As time goes by, you will pick up the shortcut keys. Even better news is that Visual C++ also gives you a toolbarthe row of buttons just under the menu. The pictures are meant to tell you what the buttons mean, but Visual C++ explains the option if you click the mouse button (without releasing it) on the button you are interested in. You can avoid running a toolbar command by moving the mouse off the button before releasing the left mouse button. The workbench's editing menus and shortcut keys give you power that's easy to master. Stopping Visual C++ Always exit the Visual C++ environment and Windows before turning off your computer. When you are finished with your Visual C++ session, you can close the workbench by selecting File | Exit or double-clicking the system menu button (or clicking the X for exit button in Windows 95). If you have not saved your program to the disk, Visual C++ gives you one last chance to do so with a dialog box. You must then stop Windows before switching off your PC. Under Windows 3.1, you close the program manager; under Windows 95, you press the Open button and choose Shutdown. Of course, you do not have to stop and start your computer after each session. Always return to DOS under Windows 3.1 or run Shutdown under Windows 95 before turning off your PC. You can lose programs that you worked long and hard on if you don't. The File | Exit selection terminates Visual C++ and returns to Windows. Get Ready, Get Set, Go! You're ready to program! Learning Visual C++'s entire programming environment, including all the ins and outs of the editor and compiler's options, is not as critical as learning the Visual C++ language. The compiler is just a way to transform your program from a source program (the Visual C++ instructions you write) to an executable file. Nevertheless, you will add to your editing skills over time, picking up shortcut keys and editing techniques to improve your programming experience. Even though you do not know the entire range of editing commands (very few veteran Visual C++ users do either), you have seen that entering and running a program is as easy as using a word processor. The most important part of using Visual C++ is not the editor but the programming language itself. Now that you have the basic editing skills down, it is time to start the first step on your journey toward programming. Turn the page and enter the ranks of Visual C++ programmers. Now that you've mastered the workbench, it's time to move to the specifics of the Visual C++ language. Homework General Knowledge What does workbench stand for and what is the workbench? What are the two ways to select from a menu? What command prepares the workbench for a new program file? What is the difference between insert mode and overtype mode? When you run a Visual C++ program, the resulting output doesn't appear on the workbench's screen. Where do you see the results? What is a keyword? What is an operator? What are shortcut keys? How do you start Visual C++? What command do you type to stop Visual C++? True or false: You must install Visual C++ before you can compile programs with Visual C++. True or false: You must save your program to the disk if you want to edit the program in a subsequent session. True or false: You should exit Visual C++ before turning off your computer. True or false: Not all of Visual C++'s menu options are available at all times. There is no What's the Output? section here due to this unit's conceptual nature. Find the Bug Figure 2.4 contains an image of the workbench screen with an error showing. What kind of bug did the workbench find, a syntax error or a logic error? Figure 2.4. An error just occurred. Extra Credit Just for practice, type the following program in the workbench and compile it. There will be an error, but the error will not show up until you run the program. Even though main() is incorrectly spelled as mane() on the sixth line, your compiler cannot catch all syntax errors, only many of them. Run the program to see what happens, and then correct the error and test the program again. // Filename: PROBLRM.CPP #include <iostream.h> void mane() { // Something's wrong here! cout << "This won't display."; return; } |