CS210 Lab: Introduction to Visual C++

Prelab Questions:

For a review of relevant topics click here.

Highlights of This Lab:

Lab Exercise:

Click the little computer above for a detailed description.
For this excercise you will debug a program and demonstrate the correct results to your lab instructor.

1. Introduction to Visual C++ .NET

Visual C++ comes within Microsoft Visual Studio .NET 2003. Visual Studio .NET 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.

Visual Studio .NET is a package that contains all the libraries, examples, and documentation needed to create applications for Windows. Instead of talking about programs (as we did in CS110 and 170), we talk about projects and solutions. Solutions can contain several projects and projects typically contain multiple items or files. For more information about projects, click here

1.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 first assume that you 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 that you have created with any external editors. 

1.1.1 How to start

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

1.1.2 Starting Your First Program

The first thing that 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, select the "Projects" tab, and click on the "New Project" button as illustrated below:

A "New Project" dialogue box similar to below will appear.

Follow the steps (demonstrated by red arrows above): 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 right-hand side you will see "Solution 'hello' (1 project)".

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

Select File --> Add New Item... from the main menu, and select C++ File (.cpp) from the "Add New Item" dialogue box under the "Templates" section on the right-hand side. Type in the file name: "hello.cpp" in the Name: box. Click on "Open". 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 in the following program in the source code editor:

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

#include <iostream>
using namespace std;

	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.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.

If you are familiar with makefiles, then it is easy to think of a project as a machine-generated makefile that has a very easy-to-understand user interface to manipulate it. For now we will create a very simple project file and use it to compile hello.cpp.


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.

Now, we will continue using Visual C++ in another project, so select File --> Close Solution. This will return you to the Start Page.

1.2 How to compile, link and execute a program with multiple files?

The preceding section showed the procedure for creating a C++ program with only one file component: hello.cpp, and we used the built-in VC++ source code editor to edit the file.  However, when you are working on a large program, you would break your program into smaller, more manageable parts.  In other words, your project would contain more than one C++ source files.  Moreover, you might not have used the VC++ source code editor to edit your files.

The following section demonstrates how to create, compile, and execute  a VC++ project with more than one file.


1.2.1 Creating the project

Step 1.   Select File --> New --> Project....
Step 2.   Select Win32 Console Project from the "Templates:" pane.
Step 3.   Leave the project location at D:\Workarea.
Step 4.   Set the project name to lab1.
Step 5.   Click on "OK" to create the project.
Step 6.   Under "Application Settings", select "Empty project".
Step 7.   Click on "Finish".

You have now created a project that has information stored in the following folder:
You can get to this folder through the "Workarea" icon on the desktop.

It is always a good practice to put the source files in this folder. Let's do that now

1.2.2 Get multiple files using anonymous FTP

Obtain the files you need for this lab by clicking here: CS Dept FTP Server Even though the files are in the appropriate directory, you must tell Visual Studio .NET that these files are part of the project.

1.2.3 Adding existing files to the project

1. Select File --> Add Existing Item....
2. You should be looking in the lab1 directory. Highlight the .h and .cpp files: date.cpp, date.h and main.cpp
3. Select "Open" to add the files to the project.

To view the contents of any of these files, double click on the file name in the "Solution Explorer".

1.2.4 Compile, build and execute your progam

1. Select Build --> Build lab1 (This will compile and link the files).
2. Select Debug --> Start Without Debugging

2. Debugging C++ Programs

Like most people, your program may not work as you expected it to the first time you run it. To find out what is wrong, you might use "cout" statement to follow the line of execution of your code. This works, but later, when things are fixed, you must remember to remove all the extra "cout" statements.

Visual Studio .NET provides a wonderful debugger for helping you find run-time errors in your code. However, before we get into the details of how to use this debugger, some terminology might be useful. Notice that the keywords below are links to pages (with the sources of this information)

Syntax Errors
  • Errors in code due to not following the rules of the language.
  • These errors are caught by the compiler or interpreter.
Semantic Errors
  • Code follows the rules, but it does not do as you intended.
  • These errors are NOT caught by the compiler or interpreter.
  • They can cause a program to crash or hang.
Logic Errors
  • Variables do not contain correct data or program doesn't go down right path.
  • Allows you to see what is happening when you run your program so that you can determine the location of semantic errors.
  • Can break (suspend) the execution of the program to examine code, view or change variable values, etc.
  • "A breakpoint is a signal that tells the debugger to temporarily suspend execution of your program at a certain point."
  • Allows you to suspend exececution so that your program runs until it reaches a breakpoint (in the form of a place or condition that you would like to examine in more detail). You can then walk through the code line by line or instruction by instruction.

When your program is stopped at a breakpoint, your program and debugger are said to be in break mode. Visual Studio has four types of breakpoints:
  1. Function breakpoint--program breaks at specified location within specified function
  2. File breakpoint--program breaks at specified location within a specified file
  3. Address breakpoint--program breaks when it reaches a specified memory address
  4. Data breakpoint--breaks when the value of a variable changes

Remember that your project must compile and link before you can use the debugger.

As a general overview to debug your project you must:

The basic command to start the debugger can be found under Debug --> Start in the main menu. The Debugger will execute code from the current location until it reaches a breakpoint, the end of the program, or a point at which the program pauses to get input from the user. When the debugger is started, the "Debug" menu changes to have some additional commands which are also displayed on the toolbar. A few of these commands are summarized in the following section:

2.1 Useful commands

3. Sample Exercise--Debugging

The location:	ftp://ftp.cs.uregina.ca/pub/class/210/ftp/VCIntro 

The file:       debug.cpp 

Following these steps to debug debug.cpp program:

1) Set up a project for this program (follow the same procedure as discussed in Section 1 of this lab.).  Name this project debug.

2) Build debug.  If there are any compiler or linker errors, correct them. (Remember, your program should pass the compilation before you can use the debugger).  However, if you spot any logical errors in this program, DO NOT correct them at this point.

3) Run the program as usual to see how it works. Do not use the debugger yet.  Enter 1, 2, 3, 4, 5 as the input.  The first thing you will notice is that the five numbers printed out by the program are not the same numbers that you have just entered.  Therefore, something is wrong with the program.  We will use the debugger to find out what is wrong.

4)  Set up a break point. As we mentioned earlier, a breakpoint is any point in the executable code where the execution stops. A breakpoint pauses the execution of a program so that the programmer can take a look at what is going on inside the program when it runs. At this point, we do not know what caused the program to go wrong, so we set up a breakpoint after the first cout statement. To do this, click on the shaded bar to the left of the following for statement:

Now we need to RUN the code with debugging. Select Debug --> Start from the main menu. The program will start to execute, then stop at the breakpoint. Notice that the output window (at the bottom) splits into two windows with tabs. The VC++ work windows have changed, now showing the source code window across the top. A yellow arrow, which indicates the current line of code being executed, has appeared on top of the red "breakpoint" dot. (See the following figure:)

Next, we will look at some of the variables.

5) Use watch windows to trace the values of variables.  There are two kinds of watch windows in VC++ environment: the Locals Watch Window  and the Watch Window, as is illustrated in the above figure. The watch windows enable the programmer to watch the state of the variables as the program executes, and thus to see if the program is doing what was intended.

Locals Watch Window: Displays variables that are of immediate interest. For example, after we set up a break point right before the first for statement, two variables appear in the immediate watch window: i and Values. Although there are more than two variables in the program, these are the two variables that are of immediate interest. Notice that there is a small plus in a box to the left of the variable Values, and its value looks something like this: 0x0012fec4:  

The 0x indicates that it is a hexadecimal value. Since Values is an array of integers, the value being displayed is the memory location where the array begins. Click on the plus sign on the left of the variable name. The array expands below the name, showing the indices of the array and the values for each of the integers in the array. The plus sign has also become a minus sign. Click on the minus sign to hide the array.

Watch Window: This watch window becomes useful when you would like to watch any variables in your program at all times, regardless if it is being watched by the locals window or not.  To watch a variable (or an expression) in this window, simply select a variable from the locals watch window and "drag" it into the Watch Window, or type the name of the variable or expression that you would like to watch in the Watch Window.  In our example, we would like to watch i and Values at all times, (since the values of Values are not printed out correctly on the screen when we run the program in step 2)), so we drag these two variables into this watch window.

(Before we do this, we may have to undock "Watch 1" by double clicking on the tab)

6) Stepping through the code.   VC++ programming environment allows you to step through the code in two different ways.  You can a) execute one line at a time, stepping into each function call, or b) run the function without tracing what is going on inside the function.  We will look at examples of the first.

Right now, the execution of your program halts after the first cout statement is executed, because we set a breakpoint there.  This is indicated by a yellow arrow inside the red dot: . Let's step into the loop and see what happens.

On the Tool bar are three important icons to use with the debugger. One shows a blue arrow jumping over some lines of code (Step Over F10) Step Over Icon. Another shows the arrow pointing into the braces (Step Into F11) Step Into Icon. These are the buttons for single stepping through a program. There is also, the Step Out button, which allows you to skip large blocks of code in debugging. Let's use the Step Over Button first.

Press the Step Over button .  The yellow arrow will move to the next line.  Keep pressing the Step Over button until the the console window pops into the forground asking you for input:

At this point, the program is expecting some input from the user. Enter the number 5 as input and hit return.

Now click on the plus sign to the left of the variable Values in the Watch Window to expand that variable. We notice that something is wrong here: the first number we entered just now should be assigned to Values[0], but instead, it is assigned to Values[1]:

Examine the code in the source code window, and you will find that inside the first loop block, the statement
cin >> Values[1];
should read: cin >> Values[i];;
otherwise, all the keyboard input will go to Values[1]. Change this line of code; recompile and rerun the program and it should work properly.

We leave it to you to explore other features of the VC++ debugging utility.

If you are running into troubles, click here for additional comments and tips on using the Debugger.

4. Lab Exercise

The location:	 ftp://ftp.cs.uregina.ca/pub/class/210/ftp/VCIntro

The files:       llist.cpp, llist.h 

5. Postlab Exercises

For postlab exercices, click here.

This page last modified by Nova Scheidt:
Monday, 11-Jan-2016 15:13:58 CST
Accessed     times.

CS Dept Home Page
CS Dept Class Files
CS210 Class Files

Copyright: Department of Computer Science, University of Regina.