This tutorial describes how to use Visual Studio to create a new C++ program, compile/run a program, resume work on an existing program and debug a program. It is aimed at the beginning CIS student who is struggling to get their programs working. I work in the computer lab at the college I'm attending and I see many students who don't know how to use the IDE for best results.

Visual Studio automatically creates a number of files which will fill up a diskette quickly. For this reason work should be done on the C: drive or on a flash drive. When you are finished working, just copy from the temporary projects folder to your flash. If you decide to work directly from your flash drive no copying will is required. One quick note about working from a Flash drive is that for versions of Visual Studion greater than 2003 it will be slow. This seems to stem from Intellisense but even turning this off doesn't help much. Your best bet in this case is to create a temporary projects folder on the machine you are using for development and copy your project there.

Creating a Project

A project must be created before work on a program can take place. This is true for new programs and for those on which you have already begun work. The steps below describe how to create a project.
1. Start Microsoft Visual Studio .NET by selecting Microsoft Visual Studio .NET 2003 from the Start Menu. The screen below should appear:
2. Click the New Project button near the bottom of the screen on the Start Page.
3. Select the Visual C++ Projects folder under Project Types.
4. Select the Win32 folder under Project Types.
5. Select the Win32 Console Project icon under Templates.
6. Replace <Enter name> in the Name text box with MyProjectName1 where MyProjectName1 is some descriptive name for the program you are working on. For instance, if you are writing a program that computes income taxes type in MyTaxProgram or something similar.
7. Type C:\projects in the Location text box to save your work on the hard drive. Your files will be saved in the C:\projects\MyProjectName1 directory which will be created if it does not yet exist. If you are using a flash drive to store your programs on, just replace the C:\ in the example above for the drive letter of your flash drive.
8. Click OK.
9. Click on Application Settings on the Win32 Application Wizard window which appears.
10. Click the Empty project check box under Additional options.
11. Click the Finish button and the project will be created.

If you look in the Solution Explorer located on the far right of the Visual Studio window you should see the following:

Creating a New Source Code File

If you are working on a new program, a new source code file must be created after the project has been created. The steps below describe how to create the new source code file and add it to the project.
1. Select File from the menu and then Add New Item…
2. When the Add New Item dialog appears click on the Visual C++ folder under Categories. Then click the Code folder.
3. Click the C++ File(.cpp) icon under Templates.
4. Replace <Enter Name> in the Name text box with Source1 which is the name of your source code file. You don’t have to include the .cpp extension but if you do that is OK. In general terms use a descriptive name for the major function of the file, for instance, if the program is a tax calculator name the file taxcalculator.cpp or something similar. The Location text box should not have to be changed but if this is not the directory you want the source file in change it to the correct one for your project.
5. Click the Open button and a new, empty .cpp file will be created and opened into which you can type your code.
6. If the Source1.cpp file doesn’t appear in the Solution Explorer like the above screen then do the following to add your source code file to the project (which must be done before attempting to compile)
a) Select File from the menu. Select Move Source1.cpp into Project (where Source1.cpp is the name of your source code file) from the drop down menu.
c) Select MyProjectName1 (where MyProjectName1 is the name of your project) when it appears.

2. Select Build from the menu and then Build Solution. You could also press the Ctrl+Shift+B keys together as a shortcut instead. Messages from the compiler (error messages, warning messages and a successful compile/build message) will be displayed in the Output window at the bottom of the screen.

Running a Program

Once the compile/build is successful, select Debug from the menu and then Start Without Debugging from the drop down menu to run the program. You could also type Ctrl-F5 instead. If a window appears which states “These project configuration(s) are out of date:” and asks “Would you like to build them?” click Yes.

A new window, called a console window, will open and display the program’s output statements and also allow for program input.

Let’s add the following text to our code to make this program a little more interesting.
1. Add the following text before main:

Your Source1.cpp file should look like the following when you are done with all these changes:

4. Now build and run the solution as you did before. You should see the following screen:

Debugging a Program

It can sometimes be difficult to determine why a program does not run successfully. A debugger allows a programmer to step through a program line-by-line to watch it execute and fail. The values of variables can be watched as execution takes place as well.
Visual Studio has a built in debugger which can be used to step through a program. It is important to note that the program must compile first. Let’s add some more code to our Source1.cpp file (Note: this code has a logic problem in it to help you get used to debugging):

1. Add the following code before main after the show_copyright function prototype:

5. Build and run the solution as before and you should see the following screen:

6. Press 0 and instead of stopping the program like we asked our program continues. We have a bug here Watson. Press Ctrl+C to stop executing the program and close the window down. Now we’ll work on finding the bug in our code using the debugger in Visual Studio.

Breakpoints

A breakpoint tells the debugger to pause execution of a program until you tell it to continue. In order to step through a program (i.e. watch a program execute), a breakpoint should be set on the executable statement at which you want execution to pause. To set a breakpoint on a statement, click on the statement and then press F9. A red dot will appear on the left of the screen to indicate a breakpoint has been set. The breakpoint can be removed by pressing F9 again. Please note that breakpoints are not saved if you exit Visual Studio.
An executable statement is a line of code which performs an action when the program runs. Examples of statements which are not executable statements are comments, variable declarations, and the word do in a do loop. If a breakpoint is set on these types of statements, the debugger will not pause on them during execution. In addition, the breakpoint might automatically be moved to the next executable statement as determined by the compiler.

Determining where you want the breakpoint is a critical part of debugging. Put your breakpoint to soon in the execution of the program and you waste time stepping through code that you don’t need to. Put the breakpoint too late and you might just miss the bug altogether. In our sample program above we have to possible places in our code that could be causing the problem. Either the get_choice function isn’t returning the proper value or the is_valid_choice function isn’t correctly checking the value. Let’s set a breakpoint to check the return value of the get_choice function. There are two possible places to check the value, one being inside the function itself or in the assignment statement in main. We’ll set our breakpoint in main so we can check the return value. Place the cursor on the line that reads, choice = get_choice(); and either press F9 or click the right mouse button to show the context menu and then select Insert Breakpoint.
A red dot will appear on the left hand side of the screen like below:

Watching a Program Execute

Once a breakpoint is set, start the debugger by selecting Debug from the menu and then Start from the drop down menu. You could also type F5 instead. The program will begin to run but pause at the statement with the breakpoint prior to its execution. For our sample you should see something like the screen below:
At this point you have a number of choices (given below). The buttons mentioned can be found on the Debug toolbar. When you press F5 the toolbar should automatically appear but if not you can access the toolbar by doing the following:
1. Select View from the menu, then Toolbars from the drop down menu and then Debug.

Here’s what the toolbar looks like and what you can do with the buttons:
• Click the Step Over button to execute the current statement. Repeat this step to see the entire program execute.
• If you want to exit the debugger at any point, and also stop program execution, click the Stop Debugging button .
• If you want to restart execution of the program, from the beginning and using the debugger, click the Restart button .
If your program has functions, other than main, you have two additional choices.
• If the current statement is a function call, click the Step Into button to go into the function to watch it execute.
• If you are stepping through a function and do not want to watch the rest of it execute, click the Step Out button to exit the function. The debugger will still step through the rest of the program.

Value of a Variable

It is often helpful to know the value of a variable at a certain point in an executing program. The debugger allows you to look at the contents of variables as a program executes. When you press F5 the Locals Windows should automatically be available at the bottom of the screen but if not you can do the following:
1. Select Debug from the menu, Windows from the drop down menu and then Locals. A window at the bottom of the screen will display the values of the variables in the current function (e.g. in main). As you step through the program, the displayed values of the variables will change as the variables themselves are given new values.
For our example you should see the following in the Locals Windows at the bottom of the screen.
Notice the value for choice in the window above. The char variable choice was initialized with a NULL or in this case a 0. We’ll be double-checking it in the next section once the get_choice function returns a value.

Input and Output

The console window will still be used for program input and output. If you click the Step Over button on a cin statement, type the data through the console window. When you press Enter, execution of the program by the debugger will continue. If you click the Step Over button on a cout statement, the data will be displayed on the console window. For our example you’ll see the current console window for the program:
Since we haven’t executed the statement for the get_choice function only the “Hello World…” statement is showing so far. Click the Step Over button on the Debug toolbar and notice the following change in the console window for our program.
In the console window type in 1 at the prompt and then press Enter to continue the program. The console window should now look like this:
And the Locals Window at the bottom of the Visual Studio screen should now have the following values for choice.
Notice the change from before, 49 and ‘1’ instead of 0. The 49 is the ASCII value for the character 1, so the get_choice function is correctly returning a character value and choice is correctly storing that value. By stepping through the program we’ve discovered our bug isn’t in the get_choice function. Since our next statement in the program is the other function we need to check, is_valid_choice, and we know that a valid character is stored in choice and the is_valid_choice function should just keep looping with no error message. Use the Step Over button to execute the is_valid_choice function. Notice what happens to our console window.
This isn’t correct since we know that the character 1 was passed in the variable choice to the function is_valid_choice by the following statement in main: }while(!is_valid_choice( choice));. So the problem has to be in the way in which is_valid_choice is checking the value. So we’ll use the Step Into button to execute each statement in that function to find out where things are going wrong. But first you have to Step Over a couple of the statements get to the is_valid_function again. Notice that there is a yellow arrow on the left hand side of the Visual Studio window. That indicates the current statement.
In our example it’s the cout statement that displays the “Hello World…” message. Press the Step Over button to execute this statement. Press the Step Over button once again to execute the get_choice function statement. In the console window type in 1 at the prompt and then press Enter to continue the program. Now press the Step Into button to debug the is_valid_choice function. Notice that the yellow arrow is now inside the is_valid_choice function and that the Locals Window down at the bottom has variable called val that has the values passed in the variable choice, 49 and ‘1’, from main.
Press the Step Over button till the yellow arrow is positioned on the line with the switch statement. Press the Step Over button once more and notice that the arrow jumps all the way to the first statement inside the default: section of the switch. It should have hit the break statement after seeing

case 1:
break;

But it didn’t for some reason.

What You Are Looking For

There are two ways in which a debugger is most helpful: 1) to verify statements are executed as expected, and 2) to verify that variables have their expected values. Look at the case statement very carefully we seem to be comparing an integer 1 with the character 1, which is 49 in ASCII, so our program is functioning correctly but our logic test isn’t right. Change the case statement in the is_valid_choice function to look for character data:

switch (val){
case '0':
case '1':
break;

After editing the code to correct the problem, save your changes, press the Stop button on the debug toolbar and rebuild the program. Now restart the debugger. Press the Step Over button till you get to the get_choice statement, then type a 1 again and press Enter as you did before in the console window. Notice that this time through, the yellow arrow jumps to the end of the program like it should.
Press the Stop button and remove the breakpoint by placing the cursor on the line with the red dot and pressing F9.
There is still one more logic problem in this program and you should use the same techniques to find it. (Hint: the program should continue printing “Hello World… and asking the user to continue if the user enters a 1 at the prompt.)

Saving Your Work

When you are finished working (either because your program is done or because you need to resume work later), copy your source code file from the work directory on the hard drive to your diskette. It is not necessary to copy any of the other files.
Before copying your source code file, close Visual Studio. If you are asked to save any files, say yes unless you know you have made changes to your program you do not want to save.
Once you are sure your source code file has been saved to your diskette, delete the work directory from the hard drive.

Resuming Work on an Existing Source Code File

If you want to resume work on a program, a project must first be created and then your existing source code file added to the project. This process is described below.
1. Create a directory named projects off the root directory on the C: drive.
2. Copy your source code file to the work directory.
3. Follow the steps under Creating a Project above.
4. Select File from the menu and then Add Existing Item
5. In the Add Existing Item window which appears, locate your source code file in the work directory and click it to select it. Click the Open button.
6. Select File from the menu and then select Move xxx.cpp into Project (where xxx.cpp is the name of your source code file) from the drop down menu.
7. Select the project name when it appears.
8. You can now continue working on your program.
Note: if you have exited Microsoft Visual Studio but have not gone any further, you can double-click the project file and Visual Studio and your program will be opened up for you to continue working. The project file will be in the work directory and will have a .vcproj file extension. Before attempting to compile, the source code file must be added to the project (steps 6 and 7 above).

Removing a Source Code File from a Project

It may sometimes be desirable to remove a source code file from an existing project without exiting Visual Studio. Note that the steps below will not delete the file from the work directory.
1. Select the Solution Explorer “tab” at the right of the screen.
2. Select the name of your source code file listed under the “folder” named Source Files. The folders listed do not really exist on the disk. They are solely used within the project to organize its files.
3. Press Delete to remove the file from the project.
Once a file is removed from a project, and Visual Studio not exited, an existing file can be added to the project by following the steps below.
1. Select File from the menu and then Add Existing Item...
2. In the Add Existing Item window which appears, locate your source code file in the work directory and click it to select it. Click the Open button.
3. Double-click your source code file in the “folder” named Source Files on the Solution Explorer “tab” to open it.
4. Select File from the menu and then select Move xxx.cpp into Project (where xxx .cpp is the name of your source code file) from the drop down menu. Select Solution Items when it appears.
5. Select File from the menu and then select Move xxx.cpp into Project from the drop down menu. Select the project name when it appears.
6. You can now continue working on your program.

I/O File Location

Place the file to read from, the input file, in the MyProjectName1 directory with your source code file. The file will be read from this directory when the program is run.
If your program creates a text file, an output file, the file will be created in the MyProjectName1 directory. Use Notepad to view the contents of the file or add the file to the project as a resource.