Lab 0: Visual C++ Instructions


IDEs

An IDE (integrated development environment) manages the programs that programmers write; they include at least an editor and compiler and often some analysis and management tools.

Most (probably all) IDEs provide you with an editor to enter the text of a program and save it in a file. Once your program is saved, you can translate it into your computer's machine language (the binary 0s and 1s that a computer understands) using the Visual C++ compiler and linker. Graphically the process can be pictured as follows:

Starting Visual C++

Invoke Visual C++ according to your instructor's directions. (This may involve double-clicking on an icon on your computer's desktop or choosing a menu item.)

Creating a Project

The Visual C++ IDE requires that all programs be part of a project that manages the translation of a source program into machine language. Normally, we create a project first, before we create any source files for our program, although it is quite easy to add existing source files to a project.

To create a project, move the mouse within the Microsoft Visual C++ window and click on the File menu, and choose New. This should cause a window to appear labeled New, containing tabs labeled Files, Projects, Workspaces, and Other Documents. Make sure that the Projects tab is selected, and then click on the Win32 Console Application icon. Each exercises of the labs in this manual will use this same kind of project.

First, fill in the text box labeled Location by clicking the file-locator button to the right of the Location box. In the window that appears, navigate to your own folder where you can save your files.

Then fill in the name of the project in the Project name box; this should always be the same as the name of our program. Since our program is to be called bases, type bases in the text box labeled Project name. As you type, the name bases is also appended to the location; Visual C++ will automatically create a folder named bases in which to store the work you do for this project. Click the OK window, and you'll get a wizard window that asks you what type of console application you want to create. Every lab in this manual will provide you with at least a few files, so you want an empty project. Select this radio button, and his the Finish button. You'll be back in the main window with bases prepended to the title of the window.

Creating the Source Program

Next, we want to create a new file named bases.cpp in which to store our program. To do so, move the mouse to the File menu within the Microsoft Visual C++ window and choose File -> New again. The same New window will appear as before. This time, make sure that the Files tab is selected, then choose C++ Source File from the list of options presented.

In the text box labeled File Name, enter bases.cpp. Everything else (like the name of the project and the location) should be set correctly, so click the OK button. Another window titled bases.cpp will then appear within the main Microsoft Visual C++ window, in which we can type our program.

Within the bases.cpp window, enter (and personalize) the C++ source code from the main exercise. Hint: copy-and-paste works just fine from a browser into a editor window in Visual C++.

Personalizing the Program

This program is (according to the comments) the work of a fictitious person, on a fictitious date, in a fictitious course, at a fictitious university. Edit the program's opening comment (the part between the /* and */ symbols) as appropriate to make it your work on the current date, in your course at your university.

Saving Your Work

Helpful hint: Many menu items have keyboard shortcuts and some may be found in a toolbar. Explore the IDE to find these tools to make the tedious jobs a bit easier and faster.

When the bases.cpp window contains the bases.cpp program, you can store this program in a file by choosing File -> Save. (Did you find a keyboard shortcut for this?)

If you look in the folder for your project, you will see that it is no longer empty, but now contains a variety of files that you never asked to be created! Don't worry about these other files; they are just your project and other administrative files Visual C++ uses to keep track of what is where. Above everything else, don't delete them!

Translating Your Program

When your source program is entered and saved, it is time to translate it into the computer's machine language. In Visual C++, this can be done using the Build -> Build bases.exe menu choice. When this is done, Visual C++ opens the Build window at the bottom of the Microsoft Visual C++ window and displays the result of the translation there. If all is well, you should see these messages appear:

Compiling...
bases.cpp
Linking...

bases.exe - 0 error(s), 0 warning(s)
You may get a warning about main() not returning a value. You can safely ignore this warning, or (to make it go away), add return 0; just before the last curly brace in the file.

If you see anything else, then your program contains syntax errors (also known as "typographical errors"); these must be fixed before you can run the program.

When your program contains errors, the Build window at the bottom of the main window lists the errors. If you double-click on an error message, Visual C++ will move the cursor to the line in bases.cpp where it discovered the error. Compare that line and the surrounding lines with those of the source program in your web browser and fix any discrepancies. Then re-translate your program. If you still have errors, continue this edit-translate cycle as many times as necessary until your program is error-free.

Running Your Program

Running program bases.exe is just as easy as anything else in Visual C++. Choose Build -> Execute bases.exe, and the IDE will run the program for you.

When it prompts you for input, enter an easy-to-check number (like 15, 16 or 17) and verify that you get the correct results. When you see the message

Press any key to continue
don't press a key; instead, continue to the next part of the exercise.

Printing a Hard Copy of Your Program

A paper copy of electronic information is called a hard copy; it is often useful to have a hard copy of source code. As with much of Visual C++, this is quite easy: just make sure that bases.cpp is selected, and choose File -> Print. If you must do anything beyond this (such as specify a printer), your instructor will inform you of the details.

Printing an Execution Trace

It is often useful to be able to print a hard copy of the output from your program, especially for more complicated programs. Unfortunately, Visual C++ does not make this particularly easy. If you examine the bases.exe execution window, you'll note that there is no File menu or other means of printing its contents.

This forces us in a different direction. What we will do is (i) make a copy of our bases.exe window, (ii) paste that copy into a different application, and (iii) print the copy from that application.

For the first step, first make sure that the bases.exe window is selected, and then press Alt-Print Screen (hold down the Alt key and then press the Print Screen key). This copies the active window to a special system file called the clipboard.

To access that copy, we need a different program. Go to the Start menu (at the bottom of the screen), and run the Paint program. This will launch a new application called Paint. In the window that appears, use Edit -> Paste to paste what is on the clipboard (the image of your program's output) into the Paint window.

Once the copy of your bases.exe window appears there, you can use File -> Print to print the copy from within the Paint application.

You'll need a hardcopy of the output of each of your programs throughout this manual, so bookmark this page if you haven't already!

Back to the Main Exercise

You should go back to the main exercise now and apply the scientific method to the code that you've now compiled and executed. Remember to bookmark this page so that you can re-use these instructions throughout this lab manual.

Return to this page when you finish the main exercise so that you can clean up the project for this lab exercise.

Wrapping Up the Session

Let's examine the final things you will need to do in most sessions.

Cleaning Up Your Project

A program consists of source files and one or more libraries. In the course of writing and debugging a program, it may be necessary to compile the program dozens of times! Since all debugging is confined to the source files, recompiling the (unchanged) libraries each time would waste lots of time.

To avoid this inefficiency, once a file is compiled, a Visual C++ project stores a copy of the compiled version called an object file. The Build -> Execute ... command then recompiles only the files that have been changed, and it uses the existing object files of any files that have not been changed. By avoiding unnecessary recompilations, this saves lots of time.

Object files also consume disk space. To save disk space, the object files should be removed from a project any time you are done working on it. To remove these files, choose Build -> Clean, and Visual C++ will display as message that it's doing exactly that.

Quitting Visual C++

To quit Visual C++, just click on the close box of the Microsoft Visual C++ window or choose File -> Exit.

Terminology

IDE, compiler, hard copy, integrated development environment, linker, object file, opening comment, project, syntax error, typographical error

© 2003 by Prentice Hall. All rights reserved.
Report all errors to Jeremy D. Frens.