Using Microsoft Visual C++
If you wish to do your assignments on a PC, you will need either Windows 95/98/ME/2000/XP or NT 4.0, and Microsoft Visual C++ 6.0. You should be familiar with the Windows operating system and PCs in general, and to be willing and able to figure out the VC++ environment. The emphasis in class will be on general programming concepts, not on the workings of Visual C++, and the code you write will be identical to code written by students using a Macintosh.
Getting help
While the LaIR does not have PCs for general use, there are two special PCs in the Section Leader area that you can use if you need VC++ help. Please ask the section leaders on duty to help you out – most of them are very knowledgeable about how VC++ works. If you have a question that is platform-independent (e.g., how to tackle an assignment, or a general C++ question that would pertain to any compiler), then you should send it to your section leader, TA, or instructor. Questions about VC++ itself or the PC version of the libraries should be sent to the TA (jmanus@cs.stanford.edu). You will also find much useful PC information on the compilers web site at: http://www.stanford.edu/class/cs/compilers .
Getting started
As we noted before, to use the PC version of the libraries, you must:
- Be familiar with your PC and with the Windows operating system.
- Own a copy of Microsoft Visual C++ Version 6. Any "Edition" of this software will work, as long as it is Version 6. If you don't already own the compiler, you will be able to download a free copy, courtesy of Microsoft Corp. This can be obtained at http://www.stanford.edu/class/cs/compilers.
Your first job will be to install the compiler according to the instructions that come with it. The development environment is called Visual Studio (aka Developer Studio). You will probably want to place a shortcut to it on your desktop. If you can't find it, search for the file msdev.exe. Important note: don’t try to install MSDN (the Microsoft Developer’s Network) – you don’t have the correct files for it. The MSDN is a lot of documentation of Windows-specific features that you will not use in 106X, so don’t worry about it.
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. If you have questions about this process, please email cs106-pchelp@cs.stanford.edu.
You can obtain the CS Libraries from http://www.stanford.edu/class/cs/compilers/106libs/pc. The file is a ZIP archive that contains an installer for the library files. Here are the steps to follow:
- Use your web browser to download PCLibs.zip from the location above. You will be asked what to do with the file PCLibs.zip; you should save it to a directory on your computer.
- Unzip the file PCLibs.zip. Make sure you use the option to expand using original directories. If you do not have an unzip program, a good one is available at
http://www.winzip.com/. This will leave you with a few files and a directory called CS106inc (which contains a bunch of .h files). Make sure that you extract with directories intact. For Winzip, this means that you select the "Use folder names" option (it should be selected by default).
- Run (and quit) Visual Studio. It is important that you have done this at least once before proceeding to the next step.
- Double click the file setup.exe that was unzipped in step 2. This will install the libraries on your system. If the setup fails, email the PC help line.
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 a project
Sooner or later, you’ll have to do an assignment or two. The following instructions should aid you in getting started. (For some assignments, your instructor may provide a starter project that simplifies a few of these steps. Either way, you should know how to do the following:)
Start Visual Studio, and from the File menu choose New
Select the Projects tab.
The type of application that you want to create is "CS106B/X C++ Assignment Wizard"; select this from the list on the left, and type a name for the project (avoid using spaces or hyphens). Be sure you have selected "CS106B/X C++ Assignment Wizard" as the type of the application! Most of the difficulties that students have when setting up projects in this course come from a failure to follow the instructions in this step. If you select another project type, the libraries will not work properly. Here is what you should see after carrying out steps 1 and 2 and selecting " CS106B/X C++ Assignment Wizard" as your project type by clicking on it:

Just to repeat, it is essential that you select the "Projects" tab and highlight "CS 106B/X C++ Assignment Wizard". Then type the name you would like to use for your project in the "Project name" edit field.
Choose a location for your project. The default location is a good choice, but it is up to you. The name of your project will automatically be added to the end of the location you choose. If you would like to browse around to find the place you want to put the project, click the little button with "..." in it. Note that the location name will be probably be too long to show all at once in the Location field. To see all of it, click in the field and use the arrow or Home/End keys to move back and forth. When everything is set, press OK.
Most assignments will come with a set of starter files. This will usually include a mostly-empty file for the main program, some include (.h) files, libraries used for that particular assignment, and maybe some other C++ files (.cpp). These will come in the form of a .zip file, and at this point you should leave Visual Studio (you needn't shut the program down) and unzip the files into the folder (directory) for your project. Note the order here: first you create the new project (steps 1-4 above), then you unzip the files into the folder created by Visual Studio for the project.
Now return to Visual Studio, because it is time to add some files to your project (just having them in the directory is not sufficient). Pull down the Project menu, select Add to Project, then select Files. 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. Note that you never add .h files to the project.
You will probably need to create some new .cpp files of your own. Select New from the File menu and click the Files tab of the dialog. Select Text File as the type, type the file name (such as mycode.cpp), be sure Add To Project is checked, and click OK. The file will be added and you will be taken to an empty editor window, ready to type in your code. (Note: you will repeat this step for each module (.cpp file) in your project.) Be sure you provide the .cpp extension to the file name, as in mycode.cpp .
When you are finally ready to compile your code, one thing that you need to realize is that VC++ creates two "configurations" of your project, known as Debug and Release. You will always want to deal with the Debug configuration in this course, so that you can use the debugger to test and debug your program. To be sure that you are working with the Debug configuration, select Set Active Configuration from the Build menu, then choose Debug.
To compile and link all the code in your project, select Rebuild All from the Build menu, or click the Rebuild icon on the toolbar (the menu shows what it looks like). If you select Build projectname.exe from the Build menu (or press F7), just the files you have changed since the last build will be recompiled.
To run your program, select Execute from the Build menu, or click its icon on the toolbar, or press Ctrl+F5. However, unless you’re already sure the program works correctly, you may want to run it with the debugger. To use the debugger, set a breakpoint where you would first like the program to stop by placing the cursor in the desired line of code and pressing F9. Then pull down the Build menu, select Start Debug, and select Go from the submenu, or press F5.
Bug Hunting
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. As you know, 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 logic errors or, more commonly, bugs.
Debugging logic errors is a skill that comes only with practice. Here is the most important rule:
In trying to find a program bug, it is far more important to understand what your program is doing than to understand what it isn’t doing.
Most people who come upon a problem in their code go back to the original problem and try to figure out why their program isn’t doing what they wanted. Such an approach can, in some cases, be helpful, but it is more often the case that this kind of thinking can make you blind to the real problem. If you make an unwarranted assumption the first time around, you may make it again, and be left in the position that you just can’t see why your program isn’t doing the right thing.
When you reach this point, it often helps to try a different approach. Your program is doing something. Forget entirely for the moment what it was supposed to be doing, and figure out exactly what is happening. Figuring out what a wayward program is doing tends to be a relatively easy task, mostly because you have that computer right there in front of you. If you can’t understand what is happening in your program, go back into the editor and add cout calls in those areas that seem to be implicated in the problems. These statements may be as simple as
cout << "I got to here" << endl;
or they may be used to display the values of variables, as in
cout << "The value of total is " << total << endl;
Run your program again and watch what happens. Often, the text generated by the cout calls gives you a significant insight into what is going on. And, once you understand what is going on, it is usually not too hard to figure out how this deviates from the intended purpose of the program.
Fortunately for us, Visual C++’s debugger is very good. You can set breakpoints, view the values of variables, step through code one line or one function at a time, and all the normal debugging features, all with a great interface. Try holding the mouse cursor over any variable in your source code for a few seconds -- up pops the value. Read the manual and play around. Just remember to set at least your first breakpoint before starting the program.
Helpline notice
Some of the CS106 staff are not familiar with Windows or the VC++ environment, so if you have questions that are PC-specific (installation questions, VC++ questions, reports of bugs in the libraries, etc.) please refer them to jmanus@cs.stanford.edu.
To maximize the efficiency of the helpline, please observe the following:
- Please read the list of frequently asked questions (FAQ), available at
http://www.stanford.edu/class/cs/compilers/106libs/pc , to see if your question has already been asked and answered. Note that the main FAQ page gives you a brief answer to each question; click on the Answer link for more details.
- Please refer questions on the starter files provided with some assignments, general programming questions, and questions that are not specific to PCs to your section leader, TA, or instructor.
Closing Reminder
The code you write is not dependent on whether you are using a PC or a Mac. The interfaces in the .h's are the same -- that's why this handout doesn't mention them. Good luck, and happy coding!