MCC - CIS162AB - C++ Level I
P02 Weekly Pay - 10 points (turn in source code and sample output)
   cpp.gif

This document is very similar to P01 Hello World, except the source code to enter is different, and it doesn't have all of the pictures. You may want to have P01 Introduction to Visual Studio .Net available to use as a reference.

All program files are stored in Projects. This document will walk you through the process of creating a Project, and adding source files and text files to it.

  1. Put your name on your storage device (flash disk).

  2. Insert your storage device into the appropriate drive or connection on the computer.
  3. Start Microsoft Visual Studio 2008 or Visual C++ 2008 Express Edition.

    In Mesa Community College's computer lab open Microsoft Visual Studio 2008.
    The program to open on your home computer will depend on which version was installed,
    Microsoft Visual Studio 2008 or Visual C++ 2008 Express Edition.
  4. Each assignment must be stored in a separate project.
    • Click on File on the menu bar, scroll down and mouse over New, then slide over and select Project....
    • The list of Project Types and Templates will vary based on the software version being used (Professional or Express).
    • The Templates on the right side can be displayed either in large or small icons. Select small icons by click on the small icon button which is the button on the far right above the Templates.
    • From the list of Frameworks on the upper right side select .Net Framework 3.5 in the dropdown list.
    • From the list of Project Types on the left side click on Visual C++.
    • In the list of Templates scroll down and click on Win32 Console Application .
      Note: Selecting anything else will cause your program not to work as described in this document.
    • Enter Name: P02 (0 is the number zero and not the letter O)
    • To set the Location: use the Browse... button to navigate to the drive assigned to your storage device.
    • The Solution Name should be the same as the application Name (P02). It may already be grayed out if Create a directory for solution does not have a check mark as instructed on the next step.
    • Click on the box in front of Create a directory for solution to remove the check mark.
    • Click on OK.

  5. After clicking on OK, the Win32 Application Wizard will appear.
    • Click on Application Settings on the left size of the window.
    • Verify that the radio button before Console application is selected.
    • Click on the box in front of Empty project to select it.

      Always select Console application and Empty project
      Selecting anything else will cause your programs not to work as described in this document.

    • Click on Finish to create the project.

  6. The new project should appear in the Solution Explorer window.
    If your workspace window does not display the Solution Explorer,
    click on View on the menu bar and select Solution Explorer.
  7. Create a Text File for the sample output of P02.
    This process will vary based on the software version being used (Professional or Express).

    Microsoft Visual Studio 2008 (Professional):
    • Click on Project on the menu bar, and select Add New Item...
    • On the left side under Categories, Visual C++ should be selected.
    • On the right side under Templates, scroll down and click on Text File (.txt).
    • Enter file Name: output
      The extension .txt will automatically be added to the file name.
      Just about every project will require a text file for the sample output.

    • The value for Location: will be automatically set based on the values entered when the project was created.
    • Click on Add.
    • The output.txt file should now be listed in the Solution Explorer and opened in the workspace window. The sample output produced by the program will be copied and pasted into output.txt in a later step.
    • The folders and files within a project can be hidden in the Solution Explorer by clicking on the minus sign - in front of a project's name. Click on the plus sign + to expand the project folder and see the various folders and files.

    Visual C++ 2008 Express Edition:

    • Click on Project on the menu bar, and select Add New Item...
    • On the left side under Categories, Visual C++ should be selected.
    • On the right side under Templates, scroll down or down and click on C++ File (.cpp).
    • Enter file Name: output.txt
      Include the .txt extension to force the creation of a text file.
      Just about every project will require a text file for the sample output.

    • The value for Location: will be automatically set based on the values entered when the project was created.
    • Click on Add.
    • The output.txt file should now be listed in the Solution Explorer and opened in the workspace window. The sample output produced by the program will be copied and pasted into output.txt in a later step.
    • The folders and files within a project can be hidden in the Solution Explorer by clicking on the minus sign - in front of a project's name. Click on the plus sign + to expand the project folder and see the various folders and files.

  8. The last item that must be created is a C++ Source File.
    • Click on Project on the menu bar, and then scroll down and select Add New Item....
    • On the left side under Categories, Visual C++ should be selected.
    • On the right side under Templates, scroll up or down and click on C++ File (.cpp).
    • Enter File name: P02
      The extension .cpp will automatically be added to the source file name.
      Every project will require a C++ source file

    • The value for Location: will be automatically set based on the values entered when the project was created.
    • Click on Add.
    • The P02.cpp file should now be listed in the Solution Explorer under Source Files and opened in the workspace window.

  9. Enter the source code for the program into the newly created C++ source file.
    • If the Source Files folder is not expanded, click on the plus sign (+) in front of the Source Files folder name to expand the folder and see the source file P02.cpp.
    • Double click on the file P02.cpp
    • Enter the Weekly Pay Calculator program in the editing window.
    • Be sure to change all occurrences of the instructor's name to your name.
    • 
      //P02 Weekly Pay Calculator - Juan Marquez     
      /*
         This program is used by employees to determine what 
         their weekly net pay would be based on their hourly rate 
         and number of hours worked.
      */
      #include <iostream>
      using namespace std;
      
      void main()
      {
      //Declare constants using the const modifier.
      //Constants are named using all capital letters.
      //There are two ways of assigning values; = and ()
          const double UNION_DUES   = 10.00,  FICA_RATE  =  0.06,
                       FEDERAL_RATE   (0.15), STATE_RATE = (0.05);
      
      //Define variables
      //Variables that change value are in lowercase
          int hours;
          double rate, gross, fica, federal, state, netpay, netHourly;
      
      //Set the decimal point to 2 positions
          cout.setf(ios::fixed);
          cout.setf(ios::showpoint);
          cout.precision(2);
      
      //Display name and the input prompt
          cout << "P02 - Juan Marquez \n\n";
      
          cout << "Enter the hourly rate and the number of hours \n"
               << "worked on the same line, but separated by a space. \n"
               << "Press the enter key after entering both values."
               << endl << endl
               << "Enter the hourly rate and hours worked: ";
      
      //Get the input values
          cin >> rate >> hours;
      
      //Calculate gross, taxes, netpay, and netHourly
          gross     = rate  * hours;
          fica      = gross * FICA_RATE;
          federal   = gross * FEDERAL_RATE;
          state     = gross * STATE_RATE;
          netpay    = gross - (fica + federal + state + UNION_DUES);
          netHourly = netpay / hours;
      
      //Display the results and echo the input (rate and hours).
      //Display the values stored in constants.
      //Use the space bar between : and \t in labels instead of
      //the tab key for accurate alignment.
          cout << endl
               << "Hourly Rate: \t" << rate  << endl
               << "Hours Worked:\t" << hours << endl
               << "Gross Pay:   \t" << gross << endl
               << "FICA Tax:    \t" << fica    << " at " << FICA_RATE << endl
               << "Federal Tax: \t" << federal << " at " << FEDERAL_RATE << endl
               << "State Tax:   \t" << state   << " at " << STATE_RATE << endl
               << "Union Dues:  \t" << UNION_DUES  << endl
               << "Net Pay:     \t" << netpay     << endl
               << "Net Hourly:  \t" << netHourly  << endl;
      
          cout << "\nThank you!\n\n";
      }
      //end of main
    • Click the Save All button.

  10. When you have completed entering or editing a program, it must be compiled, linked, and executed. The first step of the three is compile.
    • Compile the program by selecting Compile from the Build menu.

      The results from the compiler will appear in the Output window, and errors will be in the Error List window .

      If your program has syntax errors identified by the compiler, double click on a error on the Error List to highlight the error message and to move to the line where the error occured. Correct all the errors in the editing window and recompile by selecting Compile from the Build menu again. Keep editing and recompiling until all errors have been corrected.

  11. After the program compiles successfully, it must be linked. Linking is referred to as building within Visual Studio.
    • Link your program by selecting Build P02 from the Build menu.

      The results from the linker, including error messages, will appear in the output window.

      If your program has linking errors, you may need some assistance in resolving the problem. Typical errors include using the wrong #include statements, incorrect function names and parameters, or selecting a project type other than "Win32 console project". If you suspect a different project type was selected, it may be best to start over and name the second project attempt as P02a.

  12. After the program links successfully, it can be executed.
    • Execute your program by selecting Start Without Debugging from the Debug menu.

      If the program was entered correctly, the following output will appear in an MS-DOS window.
      P02 - Juan Marquez 
      
      Enter the hourly rate and the number of hours
      worked on the same line, but separated by a space.
      Press the enter key after entering both values.
      
      Enter the hourly rate and hours worked:
      
      The program will stop when it gets to the cin statement, because it is waiting for the user to enter a value for rate and hours. When developing and testing programs, the programmer plays both roles, the programmer and user.

      For the "Enter the hourly rate and hours worked:" prompt, enter 9.25, press the space bar, enter 25, and then press the Enter key.

      9.25 will be assigned to the first variable listed (rate), and 25 will be assigned hours.

      After a program actually produces output, you begin the logic testing. It is a good sign if the programs generates the expected output, but more testing should be conducted. If the correct output is not generated, you'll need to go back, find the error, and edit the program. Then you'll need to compile, link and execute it again to test the new logic.

  13. The program should generate the following ouput, including the spacing between the labels and values.
    If execution does not pause so that the output can be reviewed, be sure to select Start Without Debugging from the Debug menu.
  14. If the program matches the required output (including the spacing), capture the sample output.
    • Windows XP:

    • Right-click on output window's title bar.
      On the pop-up menu, scroll down to Edit, and then over to select Mark.
      This will allow you to select text in the window.
    • Click and drag over the text you need to copy. As you click and drag the background will turn white.
    • Press the enter key to copy the selection.
    • Press the enter key again to close the console window and return to Visual C++.
    • Double-click on output.txt to open the file in the editing pane.
    • Paste the sample output into the output.txt file.

  15. The program must be tested with more than one set of values. Continue testing the program by using the three additional cases listed below. Be sure to copy-and-paste the complete output for each case to the same output.txt file for P02.
  16. To save space on your storage device, clean the project:
  17. To exit Microsoft Visual C++:
  18. Submit online the source code and sample output for P02 when completed.
    See Submitting assignments online.
  19. Very Important: Eject your storage device.

Revised: 08/08/2008 - www.mc.maricopa.edu/~marquez/cis162ab/p02_weekly_pay.html