Using Visual Studio at A&T
You can use Microsoft Visual Studio 2005 or Microsoft Visual C++ 2005 Express in the lab to create C++ programs. You can download a free copy of Microsoft Visual C++ 2005 Express for use on your own computer from http://msdn.microsoft.com/vstudio/express/visualc/
This guide explains how to get started using either version of the Microsoft IDE. Both systems are almost identical with the full Microsoft Visual Studio 2005 system providing several more options when creating a project. If you ignore the extra options available in Microsoft Visual Studio, you can create projects under both systems using these directions.
When you start Microsoft Visual C++ 2005 Express, you will get a screen similar to this.
Under File menu button in the upper left corner, select New and then Project ... You should see the following screen.
For most beginning student programs, you should click on Win32 under the Visual C++ project type. You can then select Win32 Console Application under Visual Studio installed templates. In the Name input box near the bottom, enter a name for the project. The name will be used to create a directory in the location specified under the Name box. The final executable program will have the name you enter. In the Location input box, enter the directory where you want the files to be saved. In the lab, you may wish to save the files on the Z:\ directory on the file server. Press OK when everything has been entered. A new window will appears that looks like the following.
For most student programs you will want to uncheck the box for Precompiled header and check the box for Empty project. Make sure that Console application is selected. Click Finish.
You now have any empty Visual Studio project. You will need to create at least one source file in your project. To create a new source file, click on Project on the top menu and select Add New Item...
Under Visual C++, select Code. You can then select C++ File (.cpp) if you want to create a new C++ file or you can select Header File (.h) to create a header file that will be #included in your program. Type the name you want for the new file in the Name box near the bottom. The proper file extension (i.e. .cpp or .h) will be automatically appended to the name of your file. Click on the Add button when you have entered the filename.
You can now type in your program using the Visual C++ editor as shown below. Note that keywords are shown in blue, comments in green, character strings in red and the rest in black.
After you have entered your program in the editor, select Build from the top menu and select Build Solution. This will compile your program. If there are no syntax errors in your program, you will see several messages in the lower part of the screen ending in
MyProject - 0 error(s), 0 warning(s)
==== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped =====
If your program has any compile errors, they will be shown in the bottom portion of the screen as shown below.
Click on the circled button to display the error. The error statement will be highlighted at the bottom and a small mark will be shown to the left of the line of code where the error was detected. Note that the actual error may not be on the line marked. If you omit a semicolon at the end of a line, the error will frequently be shown on the following line.
Once all errors have been resolved and the program compiles correctly, select Debug from the top main menu and click on Start Debugging. A separate window will open for the input and output of your program.
The code area shows the source code line of your program that is about to be executed. The line currently being executed will have a yellow arrow to the left. As the program executes, the yellow arrow will move from line to line. To the left of the source code is a faintly colored vertical bar. If you click on this bar left of a line of code, you will set a breakpoint for that line. The breakpoint is denoted by a small red circle to the left of the line of code. You can clear a breakpoint by clicking the small red circle.
The variables of the currently executing methods can be viewed in the Autos window. A variable that was changed in the previously executed statement will be shown in red. The values of other variables can be traced in the Watch window.
When the running program gets to a line with a breakpoint, it will temporarily stop execution. This allows you to inspect the state of the program at interesting places. It is generally a good idea to set a breakpoint on the return statement of the main program. When the main program returns, the execution terminates and the output window disappears. Setting a breakpoint on the return statement allows you to see any program output before it disappears.
The debugging area contains a row of buttons to control the execution of the program. Some of the buttons will be gray and disabled if they do not apply at that time (i.e. the Run button will be disabled if the program is already running.)
Run – This starts the execution of the program. The program will run at full speed. It will stop if it attempts to execute a line with a breakpoint. When you begin to debug a program, you will want to press run or one of the Step buttons.
Suspend – This will temporarily stop the execution of a running program.
Terminate – This will cause the program to quit. The output window will disappear and the contents of all variables will be discarded.
Restart – This will stop the current program and start it again from the beginning.
Step Into – This will execute the next line of the program and then pause. It can be useful to step through a program so you can trace how the variable are changed by each line of execution. When the program calls a function or method, the execution will show the execution within the function or method. If you accidently step into a method that you do not want to trace, use the Step Return button to run until it returns.
Step Over – Like Step Into, this will execute the next line of the program and then pause. If the program calls a method, the method will be executed, but the trace will not show the step by step execution of the method that you would see with Step Into. This is useful if the execution details of the method are not of concern. You should generally use Step Over when calling a system or library method instead of Step Into.
Step Return – This runs the program a full speed until it returns from the current method. Upon return the debugger will stop the program at the line after the method call. If you accidently Step Into a library method, you can press Step Return to get out.
When the program terminates, a number of messages about “No symbols loaded.” Will appear in the lower part of the screen usually ending with something like:
The program ' MyProject.exe: Native' has exited with code 0 (0x0).
These messages will occur when your program terminates normally.