MCC - CIS162AB - C++ Level I
P07 Using the .Net Debugger - 10 points
(submit corrected source code)
   cpp.gif

The purpose of this assignment is to use the Visual C++ .Net debugger tool to figure out why the program below displays some very strange amounts regardless of what is entered by the user. The debugger tool can help programmers find such logic errors. If you find the bug before completing the assignment, don't correct it. Wait until after you have completed all the steps in this assignment.

This assignment should also help you better understand call-by-value, call-by-reference, and the scope of local variables.

The debugger allows the programmer to view the intermediate results stored in variables as the program is executed one step at a time or several lines of code at a time. When executing several lines at a time, the programmer must tell the debugger when to stop by setting a breakpoint on a specific line of code.

This handout walks you through the process of using the debugger tool using a program similar to P05-ex and P06-ex.

  1. Startup Visual C++ and maximize the window size.
  2. Open your workspace.
    Create a Win32 Console Application projected named P07.
    Add a new a C++ Source file named p07.
  3. Startup Mozilla or Internet Explorer.
    Go to the course's website and click on P07 Using the Debugger on the course schedule.
  4. Copy and paste the following source code into the source code file p07.
    //P07 Using the Debugger - An Example  Juan Marquez   TR 1:00pm
    //
    //   This program is used by customers to determine what the cost 
    //   of their order would be based on the price and quantity ordered.
    
    #include <iostream>
    using namespace std;
    
    //Function Prototypes
    void getPrice(double& price);              //call-by-reference
    void getQuantity(int quantity);            //call-by-value
    
    double calcCost(double cost, int quantity);
    double calcTax(double subtotal, double taxRate);
    
    //Declare the global constants
        const double TAX_RATE = 0.05;
    
    void main()
    {
    //Declare the local constants
        const double SHIPPING = 10.00;
    
    //Declare local variables
        int  quantity;
        double price, subtotal, salesTax, total;
    
    //Set the decimal point to 2 positions
        cout.setf(ios::fixed);
        cout.setf(ios::showpoint);
        cout.precision(2);
    
        cout << "P07 - Juan Marquez    TR 1:00pm \n\n";
    
    //Get and validate values
        getPrice(price);
        getQuantity(quantity);
    
    //Calculate amounts
        subtotal = calcCost(price, quantity);
        salesTax = calcTax(subtotal, TAX_RATE);
    
        total = subtotal + salesTax + SHIPPING;
    
    //Display the results
        cout << endl
             << "Price:    \t" << price     << endl
             << "Quantity: \t" << quantity  << endl
             << "Subtotal: \t" << subtotal  << endl
             << "Sales Tax:\t" << salesTax << " at " << TAX_RATE << endl
             << "Shipping: \t" << SHIPPING  << endl
             << "Total Due:\t" << total     << endl;
    
        cout << "\nThank you!\n\n";
    
        return;
    } // end of main
    
    
    //Function Definitions
    void getPrice(double& price)
    {
        do
        {
            cout << "Enter a value between $5 and $15.00 for the price: ";
            cin >> price;
    
        } while (price < 5 || price > 15);
    
        cout << endl;
        return;
    }
    
    
    void getQuantity(int quantity)
    {
        do
        {
            cout << "Enter a value between 1 and 50 for the quantity: ";
            cin >> quantity;
    		
        }while (quantity < 1 || quantity > 50);
        return;
    }
    
    
    double calcCost(double price, int quantity)
    {
        double subtotal;
        subtotal = price * quantity;
        return (subtotal);
    }
    
    
    double calcTax(double subtotal, double taxRate)
    {
        double amount;
        amount = subtotal * taxRate;
        return (amount);
    }
    //end of program
    
  5. Select Build P07 to compile and link the program.
    This should verify that everything was copied correctly.
    There should be zero errors and 1 warning. Ignore the warning for now.
  6. Execute the program so that you can see the result of the bug.
    Click on Debug on the menu, and scroll down and select Start Without Debugging.
    P07 - Juan Marquez    TR 1:00pm
    
    Enter a value between $5 and $15.00 for the price: 10
    
    Enter a value between 1 and 50 for the quantity: 10
    
    Price:          10.00
    Quantity:       -858993460
    Subtotal:       -8589934600.00
    Sales Tax:      -429496730.00 at 0.05
    Shipping:       10.00
    Total Due:      -9019431320.00
    
    Thank you!
    
    Press any key to continue
    
  7. During the debugging process you may want to pause the execution of the program at a particular line code, so that you can observe the values in variables or to watch what happens as each line of code is executed. To cause the program to pause a breakpoint must be inserted.

  8. Run the program in debug mode.

  9. Set a watch on the variables price, quantity, total, and TAX_RATE as follows:
    Watch List. p07_5windows_net.jpg

  10. We will be controlling the execution of the program in debug mode using the function keys F5 and F11.
    F5 executes until the next breakpoint or end of program.
    F11 steps through the program one statement at time.
  11. Step through getPrice()
  12. Step through getQuantity()
  13. What is the error?
    How is the value in getQuantity() supposed to get back to main()?
  14. Complete debug session
  15. The values in variables can be changed during execution while in debug mode.

  16. Complete the assignment.
  17. Save your work, exit Visual C++, and eject your storage device.

Revised: 08/15/2006 - www.mc.maricopa.edu/~marquez/cis162ab/p07_debugger_net.html