P03_ex An Example Data Validation |
|
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 formulas used are:
if (quantity > 25)
subtotal = quantity * (price * DISCOUNT_RATE);
else
subtotal = quantity * price;
salesTax = subtotal * TAX_RATE;
total = subtotal + salesTax + SHIPPING;
netPrice = total / quantity;
Uses a while and do-while loop, and a if-else statement.
Sample Output:
P03_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:
//P03_ex Data Validation Loops - 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 item 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 a discount.
The discount is 2% off, which can also be stated as 98% of the price.
*/
#include <iostream>
using namespace std;
void main()
{
//Declare constants using the const modifier.
//Constant variables make programs easier maintain and read.
//When the values change, they only need to be changed one place.
//Constants are named using all capital letters.
//There are two ways of assigning values; = and ()
const double SHIPPING = 10.00, TAX_RATE = 0.05, DISCOUNT_RATE (0.98);
//Declare 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 << "P03_ex - Juan Marquez TR 1:00pm \n\n";
//Note:
//In Boolean expressions below, the Or operator is used.
//The Or operator is two pipe characters next to each other.
//Where is the pipe character on the keyboard?
//On most keyboards:
// It is right above the Enter key
// Shares the key with the back slash - \
// Must hold the shift key go get it
// Instead of a solid line, it is shown as a broken line
//For the Or operator, 2 pipe characters must be entered - ||.
//In a do-while loop, the condition is checked at the end of the loop,
//so the statements in the loop are executed at least one time.
do
{
cout << "Enter a value between $5 and $15.00 for the price: ";
cin >> price;
} while (price < 5 || price > 15);
cout << endl;
//In a while loop, the condition is check at the beginning of the loop,
//so the operand being tested must be initialized.
//Quantity is initialized to zero, so the condition for the while loop
//will evaluate to true, thus entering the loop.
quantity = 0;
while (quantity < 1 || quantity > 50)
{
cout << "Enter a value between 1 and 50 for the quantity: ";
cin >> quantity;
}
//After the two loops, we have a valid value for the price and quantity.
//Perform the calculations
//Discount applied when calculating subtotal
if (quantity > 25)
subtotal = quantity * (price * DISCOUNT_RATE);
else
subtotal = quantity * price;
salesTax = subtotal * TAX_RATE;
total = subtotal + salesTax + SHIPPING;
netPrice = total / quantity;
//Display the results and echo the input (price and quantity).
//Display the values stored in constants.
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
<< "Thank you!\n\n";
}
//end of program
Flowchart below:
Flowchart above: