OBJECTIVE
The
goal of this lab is to become familiar with the Microsoft Visual Studio .NET
IDE (Integrated Development
Environment),
to compile and run a C++ program, and to observe several types of common
compile-time errors. The IDE provides a broad set of development tools for
completing, testing, and refining your program.
STEP 1 – Create a Project
In
this step you will create a project that contains a single C++ source file.
1.
After you have logged into Windows 2000, start up Microsoft
Visual Studio .NET. It’s located under
Start à All Programs à Microsoft
Visual Studio .NET. Once it is loaded,
you may want to maximize it, forcing it to cover the entire screen.
2.
The first thing you will see is the “Start Page”. This page allows you to set some profile
information, open a new project or select a previously open one, and browse
on-line information about .NET. We will
not need to use this page.
3.
Before you can write a program, you must create a new
project. Create a new project by
selecting from the menu File à New à Project… The “New Project” dialog box will be
displayed.
A. Under “Project
Types:” select Visual C++ Projects folder.
B. Under
“Templates:” select the Win 32 Project icon.
C. Type “lab1”
for the Name of your project and “C:\” for the Location. This will place your project files off the
root directory on the hard drive. Later
when you are finished programming, you’ll want to copy your work to your MyDrive (drive M).
The hard drives are regularly cleaned-out, and you don’t want to leave
your work sitting around where someone else can see it.
Drive M: is called "MyDrive." It’s a network drive which contains 40 MB of
space for you to work with. It is
available to you (and only you) no matter what machine you login to. NOTE: If for some reason the network is down,
you may not have access to this drive.

D. Press the “OK”
button. The Win32 Apllication
Wizard dialog box will open. On the Application Settings page choose Console
Application and Empty Project.
E. Press the “OK”
button. This will close the dialog box
and create the lab1 project. It will
also create a lab1 “solution.” A
solution contains one or more projects.
In this case, the lab1 solution contains the lab1 project. Usually the project and solution names will
be the same.
4.
You will now create a C++ program file that will be added to
your project.
A. Right-click on the lab1
folder in the Solution Explorer. From
the pop-up menu, select Add à Add New Item… The “Add New Item” dialog box will pop up
with a variety of file types that can be created.
B. Select the
“C++ File (.cpp)” icon.
C.
Give the file a name by entering “dec2bin.cpp” in the Name
field.
D.
Click the Open button.
This will create the file, displaying an empty window entitled
“dec2bin.cpp”.

5.
Now if you click the “+” beside the Source Files folder in
the Solution Explorer, you will see the dec2bin.cpp file has been added to your
project. It should look something like
this:

STEP 2 - Compile the Program
Now
that you have an open project with a .cpp file in it,
you are ready to enter and compile a C++ program that converts decimal numbers to
binary as in the attached flow chart.
1.
At this point you should now have a Lab1 project open
containing the file dec2bin.cpp.
Attached to this lab is a printed copy of the dec2bin.cpp program
which you should now type into the dec2bin.cpp window. Type the program exactly as shown (use your own name though), comments and all. Note: Do not type in the numbers at the
beginning of each line; they are for reference only.
2.
Once you have entered the entire program, notice the
asterisk (*) next to the dec2bin.cpp in the title bar (blue area) of the
program window. The asterisk indicates
that the file has not yet been saved.
Select the “Save” option on the “File” menu to make sure your file is
saved. Once the file is saved, the
asterisk will disappear. It should be
noted that even if you do not save your program, Visual C++ will automatically
save it for you when you compile your program (in the next step).
3.
From the “Build” menu, select the “Build Solution”
option. This will do two things: 1)
compile the program and 2) build the executable if there were no compiling
errors. At the bottom of the VS .NET
IDE, you will see the compiler messages as it attempts to compile and build the
application. If you typed in the program
correctly, you will see the following message shortly:
Compiling...
dec2bin.cpp
Linking...
Build
log was saved at "file://c:\lab1\Debug\BuildLog.htm"
lab1
- 0 error(s), 0 warning(s)
----------------------
Done ----------------------
Build: 1 succeeded, 0 failed, 0 skipped
4.
If the dialog box says there are one or more errors,
then you have mistyped something.
Scrolling back up the compiler message window a little will reveal the
errors encountered when compiling the program.
Double-clicking on the error message will move the cursor in the
dec2bin.cpp window close to where the error occurred. Syntax errors almost always occur at or above
the line indicated by the compiler.
Sometimes one error will kick-off one or more additional errors. It’s always best to fix the first error and
recompile to see if the rest go away. If
you have difficulty correcting the error(s), seek assistance from the instructor
so that you may continue with the lab.
STEP 3 - Run the Program
In
this step you will run your program and make some observations about it.
1.
Once you have a program that compiles without errors, run
your program by pressing Ctr-F5. Your
program will then run in a black window.
2.
When the program asks you a yes/no question, you must
respond with a lowercase “y” character for the program to treat your answer as
a “yes.” Everything else, including “Y”,
is treated as a “no” response.
3.
Use the program to convert the decimal value 12345 to its
binary representation. Write the binary
number in the space below.
![]()
4.
Enter some smaller numbers to verify that your program is
working.
5.
When you are finished converting numbers, answer ‘n’ to the
question that asks if you would like to convert another number. The window will say, “Press any key to
continue.” Press a key to close the
window.
Note: Occasionally
when you make changes to your program and attempt to run it before building it,
the following dialog box will appear:

Just press the Yes button, and the project will be
re-compiled before running.
STEP 4 - Syntax Errors
In
this step you will introduce some errors into the program in a controlled
manner to observe the messages reported by the compiler. This will assist you in “debugging” your programs
when you encounter these error messages in the future.
1.
Remove the ‘#’ character from the beginning of line 10. The line number is displayed bottom of the
Visual C++ window (e.g. “Ln 10, Col 1”). That is, change line 10 from
#include
<iostream>
to include
<iostream>
How many errors does this simple “typo” generate when you
compile the program? Record your answer
here, and then restore line 10 to its original value before proceeding.
![]()
2.
Now comment out line 10 and recompile. That is, change line 10 so that it now
appears
//#include
<iostream>
Notice that this generates 19 errors. Record the first 2 errors generated by this
typo below.
![]()
Since iostream is the library
which defines cout, cin,
and endl, commenting-out the header file hides the
definition of these from the compiler.
Be sure to restore line 10 to its original state before proceeding.
3.
Delete the
semicolon character on line 16 and observe the results after recompiling. What line number does the compiler say the
error is on?
Sometimes the compiler errors are on the line(s) previous to the actual line
the compiler is complaining about. Put
the semicolon back before proceeding.
4.
Change the “<<” operator at line 21 to “>>” and
observe the error(s) generated after recompiling. Put line 20 back and then try reversing the
operator at line 21 and note the error generated. In both cases, almost the exact same error
messages are generated. Unfortunately
they are very cryptic and don’t give any hint as to what exactly is wrong. Just remember to check the direction of your
arrows if you see these types of errors on input or output statements.
5.
Change the variable “askuser” at
line 22 to “akuser” and recompile. Record the generated error messages, then change it back.
![]()
6.
Delete the ‘}’
character at line 62 and recompile.
Record the error message generated below.
Missing a
closing curly-brace is a very common although difficult to catch error if you
are not careful.
STEP 5 – Finishing Up
When
you are through programming, close your solution by selecting “Close Solution”
from the “File” menu. If you are asked to save changes to your project, click
“Yes”. Once all the windows have closed,
you may exit the application.
It’s
important that you transfer all of your work from the C: drive to your M:
drive, especially if you haven’t finished the lab. If you leave your work on the C: drive,
someone might erase your work.
Use
Windows Explorer to move the entire Lab1 directory to your M: drive. Then delete the Lab1 folder on the C:
drive.
If
you want to run your project again later, follow these steps:
Now that you
have written your first C++ program, type in and run the program from chapter
1, pg. 16 (Listing 1.5) of your text book.
This program determines the volume of a box with the given
coordinates. You will need to repeat the
steps of this lab for typing in and running this program. These steps are summarized below:
/*-------------------------------------------------------------------
PROGRAM: dec2bin.cpp
AUTHOR: Your Name
DATE WRITTEN: Today’s date
This program accepts
decimal (base 10) integers from the user and
outputs
their binary (base 2) representation.
-------------------------------------------------------------------*/
#include <iostream>
#include <math.h>
using namespace
std;
void main()
{
int decimal; // Number to be
read in from user
int p; // Used as
a power of 2 for converting
char
askuser; // To ask user if he would like to continue
// First, ask the
user if he would like to convert a number
cout
<< "Would you like to convert a number to binary (y/n)? ";
cin
>> askuser;
// As long as the
user says 'y' then convert numbers
while
(askuser == 'y')
{
// Get the number
from the user
cout
<< "Enter a positive decimal integer to convert to binary: ";
cin
>> decimal;
// Display a
short message preceding the binary output
cout
<< "The binary value is ";
// Find the
largest power of 2 that goes into the number
p = 0;
while
(pow(2, p) <= decimal)
{
p = p + 1;
}
p = p -1;
// While p is
non-negative, output the current bit
while
(p >= 0)
{
if (decimal >= pow(2, p))
{
cout
<< "1";
decimal =
decimal - (int) pow(2, p);
}
else
{
cout
<< "0";
}
p =
p - 1;
}
// Leave one
blank line before we prompt the user again
cout
<< endl << endl
<< "Would you like to convert another number (y/n)? ";
cin
>> askuser;
}