P06_ex An Example of call-by-reference |
|
Sample Output for P06_ex:
P06_ex - Juan Marquez TR 1:00pm
Enter a value between $5 and $15.00 for the price: 12.50
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 Code:
//P06_ex Call-by-reference Functions - 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 cost 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> // cin, cout
using namespace std;
// Function Prototypes
double inputPrice(); //call-by-value
void inputQuantity(int& quantity); //call-by-reference
void calcCost(double price, int quantity, double& subtotal); //mixture
void calcTax(double subtotal, double taxRate, double *taxAmount); //pointer
//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 << "P06_ex - Juan Marquez TR 1:00pm \n\n";
//Get the input
price = inputPrice(); //call-by-value
inputQuantity(quantity); //call-by-reference
//Process the order.
//After the two functions, we have a valid value for price and quantity.
//So we can now perform the calculations.
calcCost(price, quantity, subtotal); //mixture
calcTax(subtotal, TAX_RATE, &salesTax); //Use & to send the address
//of salesTax to taxAmount.
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
//Declare local variables in functions where needed.
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;
}
void inputQuantity(int& quantity)
{
quantity = 0;
while (quantity < 1 || quantity > 50)
{
cout << "Enter a value between 1 and 50 for the quantity: ";
cin >> quantity;
}
return;
}
void calcCost(double price, int quantity, double& subtotal)
{
//DISCOUNT_RATE is a global variable
if (quantity > 25)
subtotal = quantity * (price * DISCOUNT_RATE);
else
subtotal = quantity * price;
return;
}
//Use * to declare pointer.
//With pointers we also use the * to reference the value stored at
// the location the pointer points to.
void calcTax(double subtotal, double taxRate, double *taxAmount)
{
*taxAmount = subtotal * taxRate;
return;
}
//end of program
Structure Chart for P06_ex below:
Flowchart of module inputPrice in P06_ex: