MCC - Marquez - CIS162AB - C++ Level I
P05_ex Call-by-value
   cpp.gif

The purpose of this C++ programming example is to familiarize students with designing and developing functions. This is a rewrite of program P03_ex using call-by-value functions.

Description:


This program will be used by customers to determine what the cost of their order 
would be based on the price of the item and quantity ordered.

Constant variables are used to store the following values:

    SHIPPING = 10.00      TAX_RATE = 5% (0.05)     DISCOUNT_RATE = 2% (or 0.98)

The user is prompted to enter the price of the item and the quantity ordered. 
The input values are validated.
   The price must be between $5 and $15.00 inclusive.
   The quantity must be between 1 and 50 inclusive.
The program then calculates the total cost using the user input and constants.
In addition, the net price is calculated, so that customers will know what the 
   cost of each item is after tax and shipping has been added on.
If more than 25 items are ordered, the customer receives the discount.
There is no sales tax on shipping.
The results are displayed on the screen, with an appropriate label 
   preceding the value. 
The output displays the tax rate that was used to calculate the tax.
The output is formatted to two decimal points. 
The function prototypes used are:

    double inputPrice();
    int    inputQuantity();
    double calcCost(double price, int quantity);
    double calcTax(double subtotal, double taxRate);

Sample Output:

P05_ex - Juan Marquez    TR 1:00pm

Enter a value between $5 and $15.00 for the price: 4
Enter a value between $5 and $15.00 for the price: 12.50

Enter a value between 1 and 50 for the quantity: 60
Enter a value between 1 and 50 for the quantity: 30

Price:          12.50
Quantity:       30
Subtotal:       367.50
Sales Tax:      18.38 at 0.05
Shipping:       10.00
Total Due:      395.88
Net Price:      13.20


Thank you!

Press any key to continue



Source:

//P05_ex call-by-value Functions - 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.

   The input values are validated.
   The price must be between $5 and $15.00 inclusive.
   The quantity must be between 1 and 50 inclusive.

   If more than 25 items are ordered, the customer receives the discount.
*/

#include <iostream>
using namespace std;

//Function Prototypes
double inputPrice();
int    inputQuantity();
double calcCost(double price, int quantity);
double calcTax(double subtotal, double taxRate);

//Declare global constants
    const double DISCOUNT_RATE = 0.98;

void main()
{
//Declare local constants
    const double SHIPPING = 10.00, TAX_RATE = 0.05;

//Declare local variables;
    int  quantity;
    double price, subtotal, salesTax, total, netPrice;

//Set the decimal point to 2 positions
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    cout << "P05_ex - Juan Marquez    TR 1:00pm \n\n";

//Get the input values
    price    = inputPrice();
    quantity = inputQuantity();

//Process the order.
//After the two functions, we have a valid value for price and quantity.
//Now we can perform the calculations.
    subtotal = calcCost(price, quantity);
    salesTax = calcTax(subtotal, TAX_RATE);

    total    = subtotal + salesTax + SHIPPING;
    netPrice = total / quantity;

//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
         << "Net Price:\t" << netPrice  << endl << endl

         << "\nThank you!\n\n";

    return;
} // end of main


//Function Definitions
//Must declare local variables in function header and/or body.

double inputPrice()
{
    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 (price);
}


int inputQuantity()
{
    int quantity = 0;
    while (quantity < 1 || quantity > 50)
    {
        cout << "Enter a value between 1 and 50 for the quantity: ";
        cin >> quantity;
    }
    return (quantity);
}


double calcCost(double price, int quantity)
{
//DISCOUNT_RATE is a global variable.
    double subtotal;

    if (quantity > 25)
        subtotal = quantity * (price * DISCOUNT_RATE);
    else
        subtotal = quantity * price;
    return (subtotal);
}


double calcTax(double subtotal, double taxRate)
{
    double taxAmount;
    taxAmount = subtotal * taxRate;
    return (taxAmount);
}
//end of program

Revised: 08/15/2003 - www.mc.maricopa.edu/~marquez/cis162ab/p05_ex_call_by_value.html