CS110 Lab 1: Introduction to Visual C++

1. Introduction to Visual C++T

Visual C++ comes within Microsoft Visual Studio 2008. Visual Studio 2008 also contains Visual Basic, Visual C#, and Visual J#. Using Visual Studio.NET, you can mix and match languages within one "solution". We will, however, focus on developing C++ code throughout these labs.

1 Creating Your First C++ Program

A console mode application is a character based program that runs in a DOS window.

For your first C++ program, you will build a console mode application that displays a greeting message. This (i.e. a console mode application) is the kind of VC++ programs that you will build for all your lab and class exercises/assignments. 

Console mode programs are often simpler to build than Windows applications, and this example will take you through the steps of creating, building and executing a program in Visual C++. We will use the built-in code editor in Visual Studio to edit your code; then we will show you how to build and run your C++ programs. 

1.1 How to start

Press  on your window desktop, choose All Programs from the popup menu, then choose Microsoft Visual Studio 2008, and Microsoft Visual Studio 2008

1.2 Starting Your First Program

If this is the first time Visual Studio 2008 being used, you will see the following window.

Select Visual C++ Development Settings, then click on Start Visual Studio.

The next thing you will see is the Start Page.

To get started on your first program, you must create a "project" that will keep track of all the parts of your program, including the C++ source code, the header files, etc. Therefore, click the "Create Project" link. A "New Project" dialogue box similar to the one below will appear.

Follow these steps: The "Win32 Application Wizard" will appear. As demonstrated below, click on "Application Settings" and select "Empty Project".

After this, click on "Finish". You will notice that it doesn't appear like anything has changed (you still see the "Start Page"). However, look at the "Solution Explorer" on the left-hand side you will see "Solution 'hello' (1 project)".

You want to add C++ source code to this project.

Select Project --> Add New Item... from the main menu, and select C++ File (.cpp) from the "Templates" section on the right-hand side. Type in the file name: "hello.cpp" in the Name: box. Click on "Add". This file will be added to the hello work space that we have just created, and a blank document will be opened for editing.

Type the following program in the source code editor:

// FILE:     hello.cpp
// PURPOSE:  An example of a simple I/O stream 

#include <iostream>
#include <string>
using namespace std;

int main()
	char name[50];
	cout << "Please enter your name" << endl;
	cin >> name;
	cout << "Hello, " << name << endl;
	return 0;

Save hello.cpp after you have finished editing it.

1.3 Building the hello Project

In order to compile any code in Visual C++, you have to create a project. A project holds three major types of information:

1) It remembers all of the source code files that combine together to create one executable. In this simple example, the file hello.cpp will  be the only source file, but in larger applications you often break the code up into several different files to make it easier to understand (and  also to make it possible for several people to work on it  simultaneously). The project maintains a list of the different source files and compiles all of them as necessary each time you want to create a  new executable. 

2) It remembers compiler and linker options particular to this specific application. For example, it remembers which libraries to link into the executable, whether or not you want to use pre-compiled headers, and so on.

3) It remembers what type of project you wish to build: a console application, a windows application, etc.

For now we will create a very simple project file and use it to compile hello.cpp.

Compile and Build:

1. Compile the hello project by selecting Build --> Compile from the main menu.
It simply compiles the source file and forms the object file (hello.obj) for it. It does not perform a link, so it is useful only for quickly compiling a file to check for errors.

2. Select Build --> Build hello from the menu bar to link the program.
It compiles all of the source files in the project that have been modified since the last build, and then links them to create an executable.

3. Choose Debug --> to run the program. A DOS window will popup.

If errors or warnings are displayed in the Build status window, there is probably an error in the source file. Check your source file again for missing semicolons, quotes, or braces.

Save your C++ programs on I (Novell) drive

Since you created your C++ program on C:\Workarea, your files will be erased when you logout. To prevent this, you can save your files on I drive. The I drive is yours.

To do this,click on My Computer on the desktop, under Network Drives, you will see an image with your username on it.


How to access my I drive (Novell) from home?

Follow the link Access Novell from Home

This page last modified:
Tuesday, 06-May-2008 16:05:13 CST
Accessed     times.

CS Dept Home Page
CS Dept Class Files
CS210 Class Files

Copyright: Department of Computer Science, University of Regina.