Lab 4 (10 points)
Laboratory Assignment #4 —
Using Microsoft Visual Studio
Due: at 11:59 pm on the day of your lab session
· To learn to use Microsoft Visual Studio for both C and C++ programs.
This week, you will learn how to create a project in Visual Studio from an existing C application, and then how to build it and debug it. In the second part, you will write your first C++ program in Visual Studio.
1. Sign the attendance sheet.
2. Create a directory in your Toaster drive to hold the files for Lab 4, and change to that directory now. Using your browser, download and unzip the same files we used for Lab 2 to this folder. These can be found at:–
Your directory should now contain the files intarray.c, intarray.h, sinewave.c, and makefile. Throw away the makefile! Visual Studio will create something else instead.
Open Visual Studio 2008. Select Create > New Project from existing code from File menu. You should see a dialog box like the following:–
Select ‘Visual C++’ from the pull-down menu asking what type of project to create, and then click ‘Next.’ (In Visual Studio, the C language is a subset of C++).
You will now see a dialog box like the one below. In this dialog, enter or browse to the folder where you copied the files in step 2.
Also, give the project a name, such as ‘Lab 4’. The folder containing the code files should automatically be added to the ‘Folders’ list. If it’s not add it. Click Next.
You should now have a new dialog box asking how you want to build the project. This dialog resembles the following. In the pull-down menu under “Project Type,” select Console Application Project. This means that your project will read standard input from and write standard output to a console window provided by Visual Studio.
You don’t need to check any of the other boxes. At this point, you may click Finish.
You should now be back in the main Visual Studio window. On the left side is a panel labeled Solution Explorer followed by your project name. This should resemble the following:–
Note: Panels and windows such as the Solution Explorer and most other windows can be docked or floating and can be arranged to suit your needs and the size of your display.
Right click on the name of your project and select Properties. In the next dialog box, select Configuration Properties > Linker > System. You should see a dialog resembling the following:–
Make sure that the first line on the right panel (i.e., Subsystem) says Console. If not, click on it to reveal a pull-down menu and select Console. This creates a typical command-line application that takes the normal command line arguments (argc and argv). Click okay.
Try to build the code by pressing the F6 button or clicking Build Solution from the Build menu. You should see the results of the build in the ‘Output’ window at the bottom of the Visual Studio screen.
To run your program, press the green button next to Debug or click F5. Your program should run without any errors. But did you notice that all it does is flash a console window for an instant before it quits?
To see what the output looks like, you might want to add getchar(); to the main() function in sinewave.c, right before return statement. Now your program will execute and wait for a character input before quitting. You should see something like the following in the console window:–
To finish execution, type any character in this window. The window will disappear, and the Output panel at the bottom will show the results of execution.
To set a breakpoint on any line, open an edit window on the code file (by double clicking on that file in the Solution Explorer window of step 5. Right click on the line where you want to insert the breakpoint, and select Breakpoint > Insert breakpoint.
Now again press F5 or the green button at the top, your code should break at the breakpoint inserted earlier. After it breaks, hover your cursor above a variable name to see what’s in that variable at that point during execution (as shown in the following screenshot).
This illustrates the tremendous power and time-saving ability of an Integrated Development Environment. You can simply point and/or click on variables while stopped at a breakpoint to see what that are.
You can also single-step through your code by click ‘Step into’ or ‘step over’ in the ‘Debug’ menu. Experiment with these, and see whether your program is doing what you think it should.
So far you were building your project in Debug mode, which is the normal default. When you’re done with debugging and are about to deploy your application, normally you would want to build it on Release mode.
Debug and Release are different configurations for building your project. As the name implies, you generally use the Debug mode for debugging your project (equivalent to the –g switch in gcc), and the Release mode for the final build for end users. The Debug mode does not optimize the binary it produces (as optimizations can greatly complicate debugging), and it generates additional data to aid the debugger. The Release mode enables optimizations and generates less (or no) extra debug data.
To build your project in Release mode, use this dropdown menu and select Release. You should be all set.
You can clean your project by selecting one of the Clean commands from the Build menu. Do this now and exit Visual Studio.
Open a window on to your Lab 4 folder. Notice that there are a bunch of new files and folders in addition to the three you originally copied. These new files were created by Visual Studio; they describe your project and how to build it. Select all of the files and folders, right click and select Send to > Compressed (zipped) folder. This zips all files together into a single one.
To turn in your lab assignment, use the following command from a PuTTY window:–
/cs/bin/turnin submit cs2301 LAB4 <your zip-file name>
For this part of the Lab, you will rewrite your C program from Lab #1, Part 2, to be a C++ program. Make a copy of that program (it was supposed to be called lab1b.c). Call the copy lab4b.cpp and put it in a new folder.
In Visual Studio, create a new project using the same steps as above. Be sure that the Linker > System > Subsystem property says Console (/SUBSYSTEM:CONSOLE).
Double-click lab4b.cpp in the Solution Explorer panel to open an edit window for this file. Edit this program so that it follows the style of Figure 18.1 of Deitel & Deitel. (You can also see a copy of this program on Slide 9 of the lecture of Monday, February 8). Use <iostream> and <cmath> as the include files, and use the std::cin and std::cout streams for input and output. Compile and debug your program until it builds correctly and works.
As before, clean the project by selecting one of the Clean commands from the Build menu or by right-clicking the project name in the Solution Explorer panel. Zip the directory and submit it using the following command from a PuTTY window.
/cs/bin/turnin submit cs2303 LAB4 <your zip-file name>
Note that this zip file should have a different name from that of Part 1 of the Lab. Also, for this to work, your folder should be on your Toaster drive so that you can reach it both from Visual Studio on a Windows PC and from a command shell on the CCC system.