Introduction
The purpose of this lab is to gain familiarity with the
Microsoft Visual C++ programming environment. In particular,
you will learn how to: create a workspace, enter and modify a program,
and compile, build, and execute a program.
- Starting Visual C++
- Creating a Workspace
- Creating a Program
- Compiling, Linking, and Building your Program
- Running your Program
- Saving your Output
- Dealing with Errors
- Summary
1. Starting Visual C++
Step 1:
First, start up Visual C++ from the
Start->Programs->Microsoft Visual C++ 6.0->Microsoft Visual C++
6.0 menu.
Was the description of the location of Visual C++ given above correct?
If not, record the correct location on your lab sheet..
Step 2:
You should be greeted by an invaluable
"Tip of the Day". After reading this tip, you can either choose "Close"
or continue fascinating yourself with more tips by clicking "Next Tip".
When you tire of reading tips, or perhaps you are ordered to turn out the lights
and go to sleep, choose "Close". You are then presented with the Microsoft
Developer Studio Visual C++ environment, as shown below:
The steps you will complete in the sections that follow are:
- create a workspace in which to develop and run programs;
- create a program;
- compile the program;
- run the program;
- debug the program.
On your lab sheet, describe any differences in appearance between the
picture shown and the Visual C++ application on your desktop.
2. Creating a Workspace
Step 1:
In order to create a workspace, select
"New" from the "File" menu. You will be presented with a dialog
box displaying a dizzying assortment of project types. The type you should select
is "Win32 Console Application", shown highlighted in the figure below.
On your lab sheet, record the names of at least 3 other types of
projects you can create.
Step 2:
Click the text box below "Project
name:" and type in a name for your project (e.g., "testproject"
as shown). Then click "OK".
You may be asked some questions regarding your choice. Make sure you select
the "empty project" option when asked. Otherwise, you can ignore the
dialogue widows and just hit "return" and watch them go by.
On your lab sheet, record the 3 other types of console applications
you can create.
Step 3:
A new window appears in the left of the
screen showing the "testproject" workspace you have created.
3. Creating a Program
Step 1:
Now, you are ready to create a program
in your workspace. To do this, click the 'page' icon
(this appears at the leftmost part of the toolbar beneath the "File"
menu). This creates a blank window to the right of the "testproject"
workspace. In this window, you will enter your program.
Step 2:
Click in the blank area, then enter the
program shown below. This program is syntactically correct as it appears, meaning
there are no errors in it such as spelling, capitalization, or improper use
of symbols. Type everything exactly as it appears. Pay special attention to
symbols such as semicolons (;), curly braces ({ }), slashes
(/) and backslashes (\). Use the tab key for
indentation.
|
IMPORTANT: Before typing anything, you might want to
turn on the Emacs-compatibility mode. You can do this by
going to the Tools->Options menu. Next, choose the
Compatibility tab and then from the Current source
editor emulation drop-down list, choose Epsilon.
Finally, click Yes when it asks if you're sure you want
to change the emulation and voila! You're now able to
use your Emacs key-bindings!
|
There are several things you should know about to help with typing
the program:
- You can move the cursor up by pressing CTRL-p, down by
pressing CTRL-n, left by pressing CTRL-b, or
right by pressing CTRL-f.
- When you indent with the tab key, the next
line you enter will automatically be indented to align with the
previous line. To remove indenting, you only need to backspace
once for each tab character that appears on the line.
- You can select an entire word by positioning the cursor inside
the word and double-clicking. You can also select an entire line
by positioning the cursor beside the line, inside the gray left
margin area, and clicking.
|
The Program:
|
//-----------------------------------------------
// Program Name: Sample Program 1
// Purpose: This program converts two
// English measurements to
// metric.
// Coder: Joe B. Hacking
// Date: 2003-09-08
// Last Modified: 2003-09-10 by Ricky J. Sethi
//-----------------------------------------------
#include <iostream>
using namespace std;
int main(void) {
const double INCHES_PER_METER = 39.37;
const double POUNDS_PER_KG = 2.24;
short height;
short weight;
cout << "METRIC CONVERTER" << endl << endl ;
cout << "Enter your height in inches " ;
cout << "(No fractions, please!) : " ;
cin >> height;
cout << "Enter your weight in pounds" ;
cout << "(No fractions, please!)" ;
cin >> weight;
cout << endl ;
double metric_height = height*INCHES_PER_METER;
double metric_weight = weight/POUNDS_PER_KG;
cout << "Your height is " << metric_height << " meters." << endl;
cout << "Your weight is " << metric_weight << " kilograms." << endl;
return 0;
}
|
Step 3:
As you are entering the program text,
you should save the file in one of several ways; e.g., either click the "disk"
icon on the toolbar, or select "File->Save As...". When you choose a
name for your program, use a ".cpp" suffix to indicate it is a C++ program,
for instance "metric.cpp". Navigate if necessary to the testproject
folder and save your file there.
Step 4:
Notice that after saving the file with
the ".cpp" suffix, syntax highlighting is in effect.
What do you suppose the significance is of blue-colored text and green-colored
text? Record your answer on your lab sheet.
4. Compiling, Linking, and Building your Program
Your next step is to compile the program. This translates your C++ code to
machine language so that the machine can understand what steps it must follow.
Step 1:
First, tell Visual C++ that the program
you have just created should be installed in your project. Do this by choosing
Project->Add to Project->Files..., then navigate to your "metric.cpp"
file in the testproject folder. Select metric.cpp and
click "OK".
Step 2:
To compile, build, and run your program, you can either choose commands from
the Build menu or select their equivalents from the toolbar:
.
You can learn what each icon stands for by positioning the cursor over it and
waiting (without clicking!) for a moment until a short description
appears below your mouse pointer. Determine the meanings of these
icons and record them on your lab sheet.
Step 3:
Now that your program has been installed
in the active project, you are ready to try to compile it. Do this by selecting
Build->Compile metric.cpp (or by clicking the Compile
button in the toolbar).
If all goes well, you should see the following text in the message
window at the bottom of the screen:
--------------------Configuration: testproject - Win32 Debug--------------------
Compiling...
metric.cpp
metric.obj - 0 error(s), 0 warning(s)
However, if you typed in something incorrectly, you may receive
one or more error messages. If the compiler cannot understand what
you meant, it tries to diagnose the error. Unfortunately, compilers
cannot always tell you what your mistake was. Rather, a compiler can
show you the point in the code where it first detected a mistake.
Sometimes, the error has occurred elsewhere in the code before that
point.
Sometimes, you may receive MANY error messages because of one
small error earlier in the program. (This happens, for instance, if a
variable is declared in the beginning of the program, then used in many
places throughout the program. If the name was misspelled in the
variable declaration statement, the compiler will deem every
subsequent usage of that name throughout the program as incorrect.)
If you do get error messages, record the first error message on
your lab sheet. Determine what the error was in your source code, and
correct it. Explain the error on your lab sheet.
Continue this process until the program runs correctly. You must
repair every syntax error before you can continue.
Step 4:
When you reach this point, your program
should have compiled successfully. If not, return to the previous step. Now
you are ready to build your program. It may seem like you are going through
a lot of unnecessary steps here, but the reason is that programs can contain
many separate files, precompiled libraries, and so on. Although our program
is simple, the compiler needs to be ready for such a situation. Building your
program can be accomplished by selecting Build->Build testproject.exe
(or by clicking the Build button in the toolbar).
If all goes well, you should receive a message
--------------------Configuration: testproject - Win32 Debug--------------------
testproject.exe - 0 error(s), 0 warning(s)
On your lab sheet, indicate whether this program compiled with no errors. If errors
exist, you must correct them before proceeding further.
5. Running your Program
Step 1:
Now you are ready to run (or execute) the
program. Do this by selecting Build->Execute
testproject.exe (or by clicking the Execute button in
the toolbar).
If all goes well, a console window will appear, containing
the results of the program, as in the sample run shown below:
Step 2:
You should see a prompt asking for your
height in inches. Respond by typing your height, followed by the Enter
key. After you respond, you should be prompted again for your weight in pounds.
Enter a value for this, again following with the Enter key.
The text that is printed
next should contain metric conversions of these measurements.
If you do not get the desired results, there may be a problem in
your program which was syntactically correct but
semantically incorrect. That is, your program had the
appropriate form, but the instructions meant something other than what
you intended. (The difference is similar to the difference between typing "there house" or
"they're house" instead of "their house"; a spelling checker would not detect
the error, but the first two expressions are incorrect; or the "Dog
bites man." vs. "Man bites dog." examples from lecture).
Carefully check your program for unexpected or incorrect results. This program
has been crafted with a deliberate error, so you should find at least one!
If you find an error, try to determine what caused it. Be sure to compare your program
with the one above - you may have introduced even more errors by not typing the
program in correctly.
Please note: If you find the command prompt disappears
immediately after execution instead of staying open, you can change
this behaviour of automatically closing the command prompt window. If
you choose Debug → Start Without Debugging (or press Ctrl + F5),
a "Press any key to continue" statement is added by Visual Studio and
prevents the command window from automatically closing before you are
able to view the program output. If, instead, you select Start from
the Debug menu, then the application runs, but the command window
automatically closes at the end of program execution, unless you set a
breakpoint on the last line of the program.
On your lab sheet, record any errors that you may have found, and attempt to
correct them.
6. Saving your Output
One of the greatest deficiencies in Visual C++ is that
there is no simple way to save the results of your program run in a
console (DOS) window. One way you can save a copy of the text in the
console window is by copying it and pasting it into a document in a
text editor.
Step 1:
Open a text editor program, such as Notepad
or WordPad.
Step 2:
Click your testproject output window so it
is active. Then, position the cursor in the output area and click and
drag over all text you wish to select (Note: you might have to
click the "mark" icon or choose it from the
Window-Menu).
Step 3:
Hit the Enter key (or, you might have to click the
"copy" icon to copy the text to the clipboard).
Note: Visual C++ may be slightly different depending on the machine
that you are using. The "mark" and "edit" buttons may not
be available on your display. Instead, you will find them as options under the
"Edit" menu. That is, you will select Edit --> mark and Edit
--> copy from the menu across the top of the window.
Step 4:
Return to the text editor program, then
choose Edit->Paste in order to copy your text from the clipboard to
a file. Save your output using the "File->Save As..." command.
Step 5:
Return to your testproject output
window so it is active, and terminate your program run by typing any key.
Print a copy of your output and attach it to your lab sheet.
7. Dealing with Errors
Diagnosing and correcting syntax errors can be a daunting process
for the uninitiated. For this reason, we will introduce deliberate
errors and learn what sort of error messages are produced by them.
Step 1:
Return to your source file in the Visual C++
environment.
Step 2:
Modify your file and put an intentional error:
remove a necessary semicolon. Run the program again. Because
Visual C++ is aware you made changes to your source code, it
will try to recompile and relink.
Step 3:
You should have gotten a compile-time error
message. Compare what the error message says to what the problem
really is. (You may have to scroll up in the "Build" window in order
to find the actual error message.) Not all error messages are very
accurate or descriptive. Double clicking on the error message will
cause a blue bullet to appear in the source window to the left of the
line of code where Visual C++ thinks the error exists in your
source code. Fix the error, but try other kinds of modifications (one
at a time) and see what happens. In each case, enter the results on
your lab sheet:
Try 1:
Delete a line declaring a variable -- What is the output?
Try 2:
Remove a "//" from the beginning of a comment line.
Try 3:
Add a "//" to the front of the int main(void) line, turning
it into a comment.
Try 4:
Delete the line containing the compiler directive #include
Try 5:
Change the constant 2.24 to 100.0
Try 6:
Change the spelling of height to hight in the
declaration line "int height;"
Try 7:
Change the spelling of metric_height to metric_weight
in the output line
cout << "Your height is " << metric_height << " meters." << endl;
Try 8:
Change one of the const double declarations to const int.
EVALUATION:
Which of the errors in the previous step
were syntax errors and which were semantic errors? Enter the answers
on your lab sheet.
8. Summary
In this lab, we have covered the basics of programming in the
Visual C++ environment: creating a workspace, entering a
program, adding the program to a project, compiling, building, and
executing a program, saving your results, and diagnosing errors. These
skills are necessary in order to program successfully. Subsequent
labs will expect you to know how to do these things, and deal more
with the C++ language than with the Visual C++ environment.
You may wish to consult with this lab in the future when you have difficulty
using the Visual C++ environment.
Hand In: This lab handout with the answers filled in
attached to a listing of your output
Ricky J. Sethi <rickys at sethi.org>
Last modified: Tue Jul 20 16:37:36 PDT 2004