How to use Visual
C++ in the Microsoft Developer Studio
-
Introduction
-
Creating a Workspace
-
Exploring the Workspace
-
Entering and Running a Program
-
The Runtime Window (program console)
-
Saving Your Work to Floppy Disk and Deleting Your Project
from the Computer
-
Returning to Work on a Previously Created Project
-
Creating a Second Project
-
Notes on Managing the Source Code Editing Window
Introduction
The tutorial describes how a C++ program is created, compiled, linked
and run using Visual C++. It also gives some pointers on managing your
projects.
The Microsoft Developer Studio is a development environment for creating
programs in a number of languages including Visual Basic, Visual C++, and
Java. Avoid using any commands not associated with the Visual C++ language.
This tutorial assumes that the reader has a basic knowledge of the
Windows 95 environment, including the concepts of files, folders (directories),
disks and file copying.
Starting Developer Studio
From the Windows 95 Start menu, search for the item Microsoft Developer
Studio:
This will bring up an interface such as is shown below in Figure A.
Figure A.
Creating a Workspace
To use the developer studio, you need to create a workspace.
In this workspace, you will have a project and all the source code
files (*.cpp and *.h files) for your program will be associated with this
project.
-
Click the "File" option in the upper left hand corner of the developer
studio window. Select "New" from the popup menu. This will bring up the
window shown in Figure B. Note that the "Projects" tab is selected indicating
that the developer studio is expecting that you will be creating a new
project (since you do not have one currently open). At other times (such
as when adding a new source code file to a project) the other tabs will
be selected.
-
Select Win32 Console Application (Figure B). If you accidentally
select the wrong project type, you will have to delete the project and
start over with a new project.
-
In the "Project name:" field, enter the name that you want to give to your
project. This name is important because it is how you will later identify
the files that make up your project. For this example, the project has
been named "demo project".
-
In the "Location:" field, indicate the folder in which you wish to have
the project created. For this example, the project will be created as a
subfolder of the folder "C:\tommy tiger".
You can also use the browse button (indicated by "..." to the right of
the "Location:" field) to look for your personal base directory. It is
always a good idea to place your projects in a folder with a personal name
so that you can easily locate your project later.
-
Then press "OK" to complete the creation of your new workspace and project.
The project created here and all source code added to that project will
be located in a folder called "C:\tommy tiger\demo project". Your projects
and code will be stored in a folder named with the project and folder names
you selected. Later, when you want to copy your project (say to a removable
disk) you will need to locate this file through the Windows 95 interface.
Figure B
Exploring the Workspace
The workspace window consists of a divided screen with workspace information
on the left-hand panel , a blank source file panel on the right, and an
output panel on the bottom (Figure A). The source panel will hold the C++
program, and the output panel will hold error messages. Change to FileView
in the Workspace panel (by clicking the "FileView" tab circled in Figure
A). You can then expand the file listing (by clicking on the '+'s) to view
all files associated with your project.
Creating and Inserting a Source File in the Project
-
Click on File at the upper left corner of the window and select "New".
This time, the New window will come up with the "Files" tab selected (see
Figure C).
-
Select C++ Source File.
-
The name of your project will already appear in the Project box along with
a check next to in the Add to project label. The name of your project
folder will already appear in the Location box. You must enter the name
of the C++ source file. In the example, the name "demo_code.cpp" is
used.
-
Click OK. The file demo_code.cpp is automatically added to the project.
(As indicated in Figure D, below).
-
The same process can also be used to create header files (*.h files) and
text files for program input data.
Figure C
Entering and Running a Program
After creating the source file demo.cpp, the workspace has an empty
window in the source file panel. Type your program code into the empty
window (see Figure D).
To compile and run the program (Figure D).
-
Save the source file using the "Save" icon on the tool bar (see Figure
A).
-
Build the project using the "Build" icon on the toolbar (see Figure A).
This will cause the developer studio to attempt to compile and link all
the source code files associated with the project. The results of the attempted
compile and link will appear in the output window (see Figure D).
-
If the program contains no syntax errors, you will see a message in the
output window such as is shown in Figure D (0 errors and 0 warning) indicating
your program has compiled and linked.
-
If there are syntax errors, you will need use the editor to debug and correct
your program. Use the scroll bar on the output window to back up and look
at the error messages generated by the compiler. If you double click on
an error message, an indicator will appear in your source code showing
where the compiler detected the error.
-
If there are no errors, run the program by clicking the "Execute" icon
(exclamation point) on the tool bar.
Figure D
The Runtime Window (program console)
The runtime environment will create a window that can accept input from
the user and display output (this is the program console, corresponding
to cin and cout in the C++ program). For the example program, the console
will look as shown in Figure E. If your program requires keyboard input,
type it in as needed. When the program completes, the line "Press any key
to continue" will appear in the console window. This message indicates
that the program console will wait at the end of the program until you
close the console by typing any key on the keyboard.
Figure E.
If you need to get a copy of the programs input and output (say to turn
in with a homework assignment) do the following:
-
Click the "Mark" icon in the runtime window and select the text that you
which to copy dragging the mouse with the left mouse button pressed from
the upper left hand corner of the console.
-
Click the "Copy" icon to copy the marked text to the Windows copy/paste
buffer.
-
Close the runtime window by hitting the Enter key.
-
You can now paste the copied text into any text editor.
For many assignments you will simply wish to copy the program console text
into the same file as your program code. To do this, return to the developer
studio window. Use the mouse to go to the place in your program text where
you wish to paste the console text. Type <Ctrl>-v or use (Edit/Paste
from the main menu bar) to paste the copied text. You should then enclose
the pasted text in C++ multi-line comment symbols /*. . . . */ or else
you will not be able to run the program again. Click the Save icon to save
your work.
Saving Your Work to Floppy Disk and Deleting Your
Project from the Computer
When you are done working, you will want to save your work by copying
it to a removable disk. You should also delete your project from the computer's
hard drive to prevent others from accessing your work once you leave the
computer. Exit from the Developer Studio by clicking the X in the
upper right-hand corner of the window. Us the Window interface to open
your project file (following the names you used when you first create the
project.
Assuming that you will be using a 3 1/4 inch floppy disk, the files
left in your project folder will likely be to large to fit on your disk.
Fortunately, most of these files are not needed, as they will be automatically
recreated the next time you build your project. Figure F shows the files
left in Tommy Tiger's project folder after the above example. The files
that need to be kept are:
The rest of the files containing information generated by the compiler
and linker that are need to run the program, but since these will be recreated
when the program is built again later, they can be deleted. Delete these
files from the folder as shown in Figure F.
Now you can copy your project folder to your removable disk. Once that
is done, delete the entire folder from the hard drive.
Figure F
Returning to Work on a Previously Created Project
When you ant to return to work on a project that has already been created
copy the folder that you have saved on your removable disk to the hard
drive of the machine you will be working on. There are two ways to start
working:
-
Locate the project folder from the Windows interface. Double-click on the
workspace file (*.dsw). This will launch the developer studio and instruct
it to load your project and workspace.
-
Start the developer studio from the Windows start menu. Chose "Open Workspace"
from the "File" menu. This will bring up the window shown in Figure G.
Locate your project directory, click on the workspace file (*.dsw) and
the select open.
Figure G
Creating a Second Project
When you begin to work on multiple projects, you have a couple of options.
You can reuse the same project and work space for all project by changing
the source code files associated with the project, or you can create new
projects for each job. At this point you will need to begin experimenting
with the developer studio to learn various ways to manage your projects.
It is important to distinguish between a workspace/project and the Windows
folder in which it is kept. It is possible to have multiple projects existing
in the same folder, and it is possible to have source code files in a folder
that are not part of the current project. The Windows folder simply indicates
which files are physically located in that folder. The files actually associated
with a project can be determined from the file view in the workspace window
(as was indicated in Figure A). The files listed in the file view are the
files that will be compiled and linked to attempt to create an executable
program.
Here are some things you will need to learn to do to manage your projects:
-
Adding a new source file to a project: Simply repeat the steps given
above to create an initial source code file (Figure C).
-
Removing a source file from a project: click on the file to be removed
in the file view of the workspace window. Hit the delete key (Del) on the
keyboard. The file will be removed from the list of project files. Note
that this does not delete the file from you project folder! To do
this you must go through the Windows interface.
-
Adding a preexisting source code file from somewhere else: (For
example, you may be reusing a class from a previous project, or adding
in code supplied by someone else such as the instructor). Using the Windows
interface, copy the new source code file into your project directory. Select
"Project/Add to Project/Files" from the menu bar at the top of the developer
studio window. This will bring up the window shown in Figure H. Select
the new file you want to add and select "OK".
Figure H
Using these three functions, you will find that you have a lot of flexibility
in determining how to manage your projects. Experiment with some toy projects
to get the feel for how to do this.
Notes on Managing the Source Code Editing Window
The default layout of the developer studio window does not leave much
room for editing your source code. There are a number of things you can
do to improve this.
First of all, you can have multiple files open in the editing window
simultaneously. To do this click on the multiple files button as shown
in Figure I. This will give up a view such as is shown in Figure J.
Figure I
Figure J
You can return to the single file view by selecting the button shown
in Figure J.
To get more space for editing, you can hide the workspace and output
windows. Do this by hitting the icons shown in Figure J. This will give
you a view as is shown in Figure K.
Figure K
This will allow you plenty of room to do your editing. When you try
to compile and run your program, or if you need to add or delete files
from the project, use the show workspace and output windows as shown in
Figure K.