CS1711/99 Lab 1 - September 28 & 30, 1999
Navigating the Development Environment
Introduction: This lab will introduce you to the IDE (Integrated
Development Environment) of Visual C++, explain how to compile and run
programs. The IDE is the program in which you write C++ programs. In some
systems, there is no IDE and the programmer must write a program using
an editor, save the file, then type a command to compile the program. For
those people who are using the environment at home (the student version
installed from the text CD), most of the comments made here will apply.
Getting started:
-
Connect to the Departmental Files Area ("T:" drive).
-
Start Visual C++: Click on the Start button, select Programs, Microsoft
Visual Studio, Microsoft Visual C++.
-
After a short wait you will see a copy of Visual C++ running.
-
If you want the environment to occupy your whole screen, click on the full
screen icon at the upper right corner of the application window.
-
READ, but don't do now! Remember when you are finished with the
lab, press the 'Start' button and select 'Shutdown...'. Then click on 'Close
all programs and log in as a different user'. Do not get in the habit of
simply pressing the power switch, this can cause your files to be erased!
Even if you don't care about losing your own files, turning off the power
will waste the time of the next user - that might be you!.
Loading and editing a program
-
When you first start the Visual C++ development environment, you will see
a `menus' and `tool bars' at the top of the application, and three windows
below. The left "resources" window will contain information on files in
use and will be gray on startup. The main edit window is at the right.
After we begin to compile a program a bottom window - the `output window'
- opens. It will report the results of your compiles and builds of a program.
You can resize the windows by clicking on and dragging the boundaries as
you like. Try this now.
-
The Menus: There are several pull-down menus at the top of the screen.
You should click on the File, Edit and Window menus to
see some of the commands available. We will be looking at the other menus
as we need them.
-
Creating a project
-
On the File menu click `New', then click the `Projects' tab.
-
select "Win32 Console Application" from the displayed list. Type "Hello"
in the Project Name box, and "M:\CS1711labs\Lab1\Hello" in the Location
box.
-
Click "ok", and "Finish" in the next window. Note the changes in the resources
window. This action has created a "project" Lab 1 within the "Lab 1" workspace.
-
Pay close attention to these steps. They will be common to the start
of each lab. Moreover, you will have to create a project for each
program you write.
-
Now you are going to add a C++ source file to the project.
-
Click File|Open (we use the vertical bar "|" to indicate a menu
selection). Then navigate to the Lab 1 folder on the T:
drive. Double click on hello.cpp. The file will open in the edit
window.
-
First save the file in M:\CS1711labs\Lab1\Hello (notice that there
are several other files here now). Use File|Save as...
-
To add the source file to your project, use Project|Add to project|Files
and then click on hello.cpp. Click OK. Look at the File View tab
in the Resource Window - opening the Sources folder will show hello.cpp.
Double click to open it.
-
The Edit window. With a file loaded, the Edit window works like
common text editors. But note the colour coding: green indicates comments,
blue is for reserved words and so on. You are in an editor, so if you type
something, it will insert the characters at the current cursor location
and so on. Try using the Backspace and Delete keys to see the difference.
Try it - but don't make any permanent changes to the file. (If you do you
can click on the file, press Delete to remove it and reload it as above.)
Compiling a Program
-
Click the Compile button (above the edit window). This will cause
the IDE to attempt to translate the C++ program into machine language (i.e.
compile) the program. Did the program compile? Look at the `Errors' on
the information window that has opened at the bottom.
-
If the number of errors at times looks unnerving, don't worry. In C++ one
small error near the top of the program will often result in several later
on. Fix the errors one at a time starting with the first error. Don't try
to fix the warnings until all the errors are looked after.
-
Double-click the first error in the Output Window and press Enter (you
may have to scroll backwards to find it). This will place the Edit window
cursor at the location the compiler has discovered the error. Note
that the compiler is just guessing where the error has occurred and sometimes
a bit of hunting will be necessary to track down the actual error.
-
After fixing the error, compile the program again by pressing the Compile
button (or Control+F7). This time there should be no errors and no warning.
-
Press the Build (or F7) button. After a short time while linking...
is displayed and then Hello.exe. Note that the compiling and linking
can be combined by just pressing F7, but this really is a two stage process,
and while creating a program you will have to compile it several times
while you correct syntax errors so it is a good idea to understand the
process and get in the habit of using the appropriate commands.
-
The file Hello.exe which has been created is the executable
version of the program. If you save it you will always be able to run
the program simply by clicking on it from NT Explorer.
Running a Program
-
Now click the Execute button (or what key combination? - you can find it
on the Build menu!) You should see a prompt from the program asking you
to enter an integer.
-
Enter a 2 then another 2.
-
Try running the program a few more times. You might test and see what happens
if you provide a real number or some characters as input. If you feel brave,
you can try removing something from the program or adding some things to
see different error messages.
The HELP System: The IDE contains a Help system which can sometimes
be useful. From the Help menu choose contents to see a large array
of resources, only some of which are for C++. Under Visual C++ Documentation
you might have a look at "Getting started with Visual C++".
Please note that the help files are not meant to be used by beginning
programmers. The help files are written to assist experienced programmers
by reducing the number of times they have to look information up in a paper
manual. If you read a help file on a topic and are still confused, don't
worry about. That is what the textbook and the instructor are for.
Exiting the system To leave the IDE, select File|Exit.
If you have worked on a file since the last time it was saved, you will
be asked if you want to save.
Lab Exercises
In this particular lab, you do not need to submit anything. When you
run across questions in future labs, you might write the answer on a piece
of paper and show it to the lab assistant before leaving, or you may be
asked to show your results to the lab assistant.
-
Create a new project called speed as above (on your M:
drive, in the Lab1 folder.) Add the file speed.cpp from the T:
drive to your new project.
-
Browse through the program and predict what the program will do.
-
Compile, build and run the program.
-
Create a new project called AreaPerim as above (on your M:
drive, in the Lab1 folder.) Add the file areaperim.cpp from the
T: drive to your new project.
-
Browse through the program and predict what the program will do.
-
This program requires the Header File named rect.h in order
for the Build to be successful. You can find rect.h on the T:
drive in the include folder. You can add it to your project. An
alternate way (which does not work consistently in the networked environment)
is to select Tools|Options and the Directories tab, then
add T:\comp_sci\cs1711\include to the list. You can also read
about rect.h by opening looking in the helpsys folder
on the T: drive - open the file index.html there with
Netscape.
-
Once again, Compile, Build and run the program.
-
Show the Lab instructor your projects on your M: drive before
leaving. Each Visual C++ project uses a large amount of space on your M:
drive, but most of that is in the Debug folder for the project...and
you can safely delete all of the files in that folder.
End of Lab
Normally you will have a program that will have to be shown to the
lab instructor. In the future, if you modify a program for the lab, make
sure you save it to your account.
Make sure you have signed the log (it is probably just a sheet of paper)
so that you receive credit for the lab.