Math 155A Project #0 – Basic Introduction to using Microsoft Visual C++ 2010.

Task for project #0: Do all the steps below.  Send us an email as instructed in the main assignment sheet for Project #0 (available at the course web page).

This document gives basic information on how to run Microsoft Visual Studio 2010 for C++ programs, on the PC lab computers.  You will need to know how to do this, even if you program mostly on other computers, in order to turn in your assignments.


Creating a new C++ project (aka “solution”) from scratch.  Visual C++ groups source files into a “project”, and groups projects into “solutions”.  We do not need much of this infrastructure for our course applications.

Step 0: Start Visual C++.

-          Run the program

-          From Start Menu, run Microsoft Visual Studio 2010.  When prompted, select the Visual C++ settings. (and wait a while for Visual Studio to start up: it may take a couple minutes).

Step 1: Create the project/solution:

-          From menu bar, select File…, then New Project…, then Win32 Console Application

-          Set the project name, for instance HelloWorld.

-          Set a location for the project/solution folder.  For instance “MyProjects”, will then create or use the folder “MyProjects/HelloWorld”.   To make this happen, it is usually a good idea to uncheck the “Create directory for solution” box. 

-          In the next screen, choose Applications Settings or just Next.  (Do not just skip this step by selecting “Finish”.)  Then check the Empty Project box, then finally select Finish.

-          Allow Visual Studio to put the browsing/intellisense files where it wishes to.

Step 2.  Add new source files(s).  Usually these are .cpp or .h files.  This is only for brand new files.  If you have an existing file that you want to include in the project, see Step XX below.

-          From the menu bar: File, then New, then File….  

-          Choose Visual C++templates, then C++ File (.cpp) or other file type as desired.

-          Type your code into the source window. For instance

o    #include <stdio.h>

o    int main ()

o    {

o           printf( "Hello World.\n" );

o           getc(stdin);

o    }

-          Save the file (File  then Save Source.cpp As...) preferably into the folder( named “HelloWorld” for instance) where your project files are placed.  While doing this rename the file to be something more useful than “Source.cpp”, for instance, “HelloWorld.cpp”.

Alternate method for first item: Use Menu bar Project…, then Add New Item….

Step 3. Make the file part of the project/solution.  (A slightly silly step, but needed because of the infrastructure of Visual C++.)

-          Have the cursor focus  in the file to be added to the project, for instance HelloWorld.cpp.

-          From menu bar: File, then Move HelloWorld.cpp into ...l, then HelloWorld (or other project name).

Alternate method: After file has been saved (say, in Step 2), use Menu bar Project, then Add Existing Item….  This is also discussed in Step XX below.

Step 4. Compile the program.

-          From Menu bar Build, then Build Solution.  Or press the F7 key.

-          Check the compile window at the bottom of the screen. 

-          Fix any compile errors and repeat.

Step 5.  Run the program.

-          From the menu bar: Debug, then Start Debugging.  Or press the F5 key.

-          See what happens.  Hit return to end the HelloWorld program shown above.

Question A: What happens if you omit the getc(stdin); command?  Why?

Step 6. Add existing files to the project.  If you have an existing file you want to start with, instead of typing your code from scratch.

-          Consider whether you want to make  a new copy and place it in your project directory.  This is almost always a good idea!!!

-          From the menu bar: Project, then Add Existing Item…, then select the files you wish to include in the project (usually only .c, .cpp, and .h files).  Please note this does not make a copy of the file.  It just includes it from wherever it is.  (See the previous bullet item.)

Step 7.  Debugging example.

-          Update the HelloWorld program with a simple loop:

o    #include <stdio.h>

o    int main ()

o    {

o           printf( "Hello World.\n" );

o           int i = 0;

o           for ( int j=0; j<10; j++ ) {

o                  i += j*j;

o           }

o           printf( "i = %d.\n", i );

o           getc(stdin);

o    }

-          Run the program. 

-          Now in the window for the source code, find the grey column to the left side.  Click there, to put a red circle inside that column.  This is a breakpoint.  Click again to remove the breakpoint.

-          Insert breakpoints on the line “int i = 0;” and on the line “getc(stdin);”.

-          Run the program again.  It will start running the program, and stop at the first breakpoint, showing with a yellow arrow what line the code is at.

-          Note that below the source code, there is a panel entitled “Autos” showing the value of the variable i.  

-          Use the F10 command (or menu bar, Debug, then Step Over) to single step the program exection to the next line.  Note the value of the variable i  changes in the Autos panel, and the value of j is added. 

-          Continue single stepping: Note how the values of i and j change, and how the arrow moves to the next line each time.

-          Press F5 again: the program continues running to the next breakpoint.

Other useful things to know about debugging.  When you have function calls, use F11 and Shift+F11 to step into a function call (instead of the next line in the current routine), and out of a function call.

There is an enormous range of options and features in debugging.  You may wish to explore these on your own as needed; or ask Janine or Professor Buss for pointers.  The commands above take care of most basic needs, however, and are a great help in debugging your code.


Often you will want to start a new Visual C++ project/solution  using a previous one as a base.   IMPORTANT: To begin, make a completely new copy of all your files, in order to avoid destroying your old files.  These files should be put in a separate directory.

The basic method is to then just open the .sln file, that is, the solution file.  If it is a solution file from an older version of Visual C++, Visual C++ will update to the newer version (Visual Studio 2010 on the computers in the computer lab.)    

To try this out, use the steps below:

Step 1. Download

Step 2. Open the file.  Do one of the following methods:

-          Double click on Solar.sln.  Allow Visual C++ to upgrade it to Visual Studio 2010 if needed.

-          Alternately, in Visual C++, choose from the menu bar choose File, then Open …, then Project/Solution, and browse to find the .sln file.

Note: If you are downloading a project/solution from a much older version of Visual C++ (e.g., Visual C++ 6.0 like at the textbook’s web page), the “Solution” was called a “Workspace” back then.  In this case, you should open the .dsw file, not the .sln file.  Otherwise, the procedure is the same.

Step 3.   Run the program. 

-          Build and then run the program.  (Use F7 and F5.)  For the Solar program, you should see a simple solar system with a yellow sun, blue earth, and green moon.   They are probably flying around rather fast.   Use the up and down arrow keys to adjust the speed of the motion.  Also note what happens when you resize the window.

-          Troubleshooting conversion problems:  If you get an error message about “Project not selected for this configuration”, try the following:  In the Solution Explorer panel  on the left, right-click on the project (Solar), then choose Properties.  In the Properties panel, click the Configuration Manager… button, and make sure that the project is selected in the check box to be built.
     If there are still conversion problems, another option is to create a project from scratch as described above in Part II, and then add in the .cpp and .h files manually.

If you not at the lab computers, you may not be able to compile and run Solar.cpp due to not have the needed OpenGL and GLUT libraries installed.  If so, refer to the course web page for more information on how to do install these items.

Step 5.  Play with the solar program a bit.

-          Examine the solar.cpp source code.  Note from the comments at the beginning that it takes several different key strokes as input.  These are “r”, “s”, up arrow, down arrow, and ESCAPE. 

-          These commands are to be typed into the graphics window, not the command window. 

-          Try out these commands and see how they work.  Note that if you use the down arrow enough times, you can slow down the blue earth so you can see its rotation.

-          Try changing the sun’s color from yellow to white.  For this, find the appropriate glColor command, and replace it with “glColor3f( 1.0, 1.0, 0.0 );”.  (The last 0.0 changed to a 1.0.)

-          Experiment with other changes to the source code.

-          Take a look at how the code handles key strokes.  This is a kind of interrupt-driven, or callback-based interface; it may be rather different from the way you are used to programs receiving input.

Question B: Does the code for receiving keystrokes as input make sense to you?  What would you need to do to add a new keystroke command that altered the color of the sun back-and-forth between yellow and white?  Would you want to add a new variable to make this work?  [Hint: Compare to the way spin mode is handled.]


Question C:  Do you have any questions about how or why things work in the above steps?  Did you any particular difficulties?  Were there any steps you could not make work successfully?

OPTIONAL PART IV:  We will learn many of the basic OpenGL features during the course.  In the meantime, if you want to get a head start, there are several sources for tutorials and sample OpenGL programs.  You may wish to explore a little bit on the internet to see what is available.