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
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.
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
A "New Project" dialogue box similar to below will appear.
Follow the steps (demonstrated by red arrows above):
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
Exercise:
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; 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.
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.
Exercise:
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.
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.
Exercise:
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".
D:\Workarea\lab1You 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
The three files are now copied so the next step is to place them in a folder on your PC.
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. Select Build --> Build lab1 (This will
compile and link the files).
2. Select Debug --> Start Without Debugging
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 |
|
Semantic Errors |
|
Logic Errors |
|
Debugger |
|
Breakpoints |
|
Remember that your project must compile and link before you can use the debugger.
As a general overview to debug your project you must:
Setting breakpoint (not under the Debug menu in the main menu) A breakpoint is any point in the executable code where the execution stops, allowing the programmer to see what is going on inside the program as it runs. Breakpoints allow you to run through portions of the code where there are no problems, so that you can spend your time focusing on the areas that need to be fixed.
Setting a breakpoint is easy. Click on the shaded bar to the left of the line of source code you want to stop on. You may also set a breakpoint by right-clicking the mouse and selecting "Insert/Remove Breakpoint" from the pop-up menu. A red dot will appear in the shaded bar to the left of the line of code to indicate the breakpoint has been set. If you click on the red dot, the breakpoint will be removed and the dot will disappear.
Step Into When Step Into is chosen, the debugger executes each C++ statement one by one. Pausing to let the programmer check the values of variables or make adjustments between statements. Step Into lets the programmer step into every function call, loop, or other code block in the program. In essence, no statement will be executed until the programmer is ready to execute it.
Step Out Step Out is used in conjunction with Step Into. It will immediately execute the remaining code in the current function call, loop, or other code block, then pause execution. The programmer can continue to debug the program.
Step Over This command is used to step through a program without stepping into function calls. When Step Over is used on a function call, loop, or other code block, the entire block is executed immediately.
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) . Another shows the arrow pointing into the braces (Step Into F11) . 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.
The location: ftp://ftp.cs.uregina.ca/pub/class/210/ftp/VCIntro The files: llist.cpp, llist.h
[an error occurred while processing this directive] |
|
|
|
|