



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




CS106B Handout #5P Winter 07-08 January 14, 2008
Many thanks to Matt Ginzton, Robert Plummer, Erik Neuenschwander, Nick Fang, Justin Manus, Andy Aymeloglu, Pat Burke, and Tom Hurlbutt.
Using a PC
If you wish to do your assignments on a PC, you will need either Windows 2000 or XP, and Microsoft Visual Studio 2005. Note that Windows 98 or Me are not able to run Visual Studio
Getting started
In order to use your PC to write the programs for the class, you will need to install both a compiler and the special 106 libraries for your system.
Step #1: Getting and installing the Microsoft Visual Studio 2005 compiler For CS106B, we are using Microsoft’s Visual Studio 2005 compiler. To install VS, you will download disk images of the installer CDs, burn them to physical CDs, and then use those to install the program. The easy way to do this is to go to http://msdn- ee.stanford.edu/VisStud2005.html and click on “Visual Studio 2005 Professional Edition CD1” and “Visual Studio 2005 Professional Edition CD2” to download the disk images (Note that this download may take quite a while, as the files are large.). Note, however, that accessing this website requires a Stanford IP address (in other words, you have to be using a computer on Stanford campus). If you do not have access to a Stanford IP address, or if you would like access to additional Microsoft products as a side benefit, follow the instructions in Handout #5P-2. Either way, once you have the disk images, burn the first CD file to a CD, and then the second. Insert the CD with the first file on it. You should see a dialog box with several options. You should click on the first one, which will install Visual Studio 2005. Follow the resulting prompts, and wait for the installation to complete. Congratulations! You now have Visual Studio installed on your computer.
Windows Vista Users: If you are running Windows Vista, you will also have to download and install two service packs. You should first install Visual Studio 2005 Service Pack 1 ( http://msdn2.microsoft.com/en- us/vstudio/bb265237.aspx ), and then you should install Visual Studio 2005 Service Pack 1 Update for Windows Vista ( http://www.microsoft.com/downloads/details.aspx?FamilyID=90e2942d-3ad1-4873- a2ee-4acc0aace5b6&displaylang=en ). If you have any further problems, please let the head TA know.
Step #2: Getting the CS106 libraries Next you will need to get the CS106 libraries that we’ll be using throughout the course. Please use the following directions to install these libraries on your PC.
You will need access to Microsoft Visual Studio 2005 compiler in order to complete the programming assignments.
You can obtain the PC CS Libraries by going to the web address http://www.stanford.edu/class/cs106b/materials/libraries/ and clicking on the PCLIBS download. The file is a ZIP archive that contains an installer for the library files. Here are the steps to follow:
Once you’ve completed the above steps, you’re ready to roll. The rest of the handout deals with doing the actual assignments.
**Setting up projects ***
Every program written using Visual Studio has a project that indicates what different program files need to be compiled together in order for the complete program to work. We are going to create a project that uses a simple pre-written program, named add2.cpp. You can download the code for add2.cpp by using your web browser and visiting the following URL on the CS106B website: http://www.stanford.edu/class/cs106b/materials/examples/add2.cpp. Then, select the "Save As" option from the File menu to save the file to your own computer (if, instead, your web browser brings up a dialog box stating that it wants to open the file, choose the option to save the file to your own computer.) Once you have the .cpp file, you will need to create a Visual Studio project and add the .cpp file we provide. You will go through the basic process described below for each programming assignment that you do in this course, using your own code files.
http://see.stanford.edu/materials/icspacs106b/PCLibs-VS2005-ADT-Installer.zip
http://see.stanford.edu/materials/icspacs106b/add2.cpp
(steps 1-6 above), then you unzip the files into the folder created by Visual Studio for the project. For the Add2Example above, go ahead and copy the add2.cpp file to your project directory.
8. Now return to Visual Studio, because it is time to add the file(s) to your project (just having them in the directory is not sufficient). If you previously closed your project (or Visual Studio), make sure to open your project. From the Project menu, select the Add Existing Item… command. This brings up a dialog box that will let you add individual or groups of files to the project. Add the provided starter files with the extensions .cpp and .lib.
In the screenshot below, I've placed a breakpoint on line 23, the last cout statement of the program in my Add2Example project:
Running your program
Writing a program and typing it in is hardly the end of the programming process. You need to test your program and make sure that it works. When you pulled down the Debug menu and selected Start , the system translated the source program into the internal language of the machine. This process is called compiling. If you’ve done everything right, the compilation process will finish in a few seconds, and you can test your program to see if it works. If it does, awesome! If the program is for an assignment, print out a copy of the program and go on to the next problem.
Chances are, however, that on your real assignments you won’t be quite so lucky. Somewhere in typing in your program, you will have entered something incorrectly or made some other sort of mistake. Perhaps your program does not work as intended, or, worse yet, does not even make it through the compilation process. Making such errors seems to be an unavoidable part of programming. When errors do occur, it is time to sit down, find them, and fix them. This is the process of debugging.
Once the error is fixed and the project is rebuilt, it should compile without any errors. If, for some reason, the Output and Task List windows are not displayed by default, you can bring them up by going to the View menu, expanding the Other Windows option, and choosing either Output or Task List (you can also hit Ctrl+Alt+O or Ctrl+Alt+K to bring up the respective windows.)
Debugging logic errors
Eventually, you will get the last syntax errors out of your program, and you’ll be up and running. This is when the fun begins. The most serious errors in a program are the ones the compiler doesn’t catch—the errors that occur when your program is doing exactly what you told it to do, only that what you told it to do wasn’t at all what you really wanted it to do. These are the second class of errors, known as logic errors or, more commonly, bugs.
Since this is a larger topic, we have another handout devoted to debugging logic errors in Visual Studio that you should read and become familiar with.