P04_ex Example Bonus Calculator with Loops |
|
P04_ex Sample Output 1
P04_ex - Juan Marquez TR 1:00pm
Enter the number of salespersons to process.
Enter 0 (zero) to exit: 0
Report Totals: 0 0.00 0.00 0.00 0.00
Number of salespersons: 0
Average salesperson Bonus: 0.00
Press any key to continue
P04_ex Sample Output 2 - Error Messages
Enter the number of salespersons to process.
Enter 0 (zero) to exit: -1
Error: The number of salespersons to process must be 0 or greater. Try again.
Enter the number of salespersons to process.
Enter 0 (zero) to exit: 3
P04_ex - Juan Marquez TR 1:00pm Salesperson: 1
Enter a bonus rate between $5.00 and $10.00: 4
Error: The rate must be between $5.00 and $10.00. Try Again...
Enter a bonus rate between $5.00 and $10.00: 5
Enter three quantities between 0 and 200 separated by a space: 0 0 0
Error: All three quantities can not be zero. Try Again...
Enter three quantities between 0 and 200 separated by a space: 50 -10 100
Error: Negative values are not valid. Try Again...
Enter three quantities between 0 and 200 separated by a space: 50 100 300
Error: Values may not exceed 200. Try Again...
P04_ex Sample Output 3 - Actual Cases
Enter three quantities between 0 and 200 separated by a space: 50 100 150
Period Rate Qty 100% 103% 105% Total
P1 5.00 50 250.00 0.00 0.00 250.00
P2 5.00 100 250.00 257.50 0.00 507.50
P3 5.00 150 250.00 257.50 262.50 770.00
Salesperson 300 750.00 515.00 262.50 1527.50
Press Enter to process the next salesperson or Report Totals:
P04_ex - Juan Marquez TR 1:00pm Salesperson: 2
Enter a bonus rate between $5.00 and $10.00: 8
Enter three quantities between 0 and 200 separated by a space: 150 50 100
Period Rate Qty 100% 103% 105% Total
P1 8.00 150 400.00 412.00 420.00 1232.00
P2 8.00 50 400.00 0.00 0.00 400.00
P3 8.00 100 400.00 412.00 0.00 812.00
Salesperson 300 1200.00 824.00 420.00 2444.00
Press Enter to process the next salesperson or Report Totals:
P04_ex - Juan Marquez TR 1:00pm SalesPerson: 3
Enter a bonus rate between $5.00 and $10.00: 10
Enter three quantities between 0 and 200 separated by a space: 25 75 175
Period Rate Qty 100% 103% 105% Total
P1 10.00 25 250.00 0.00 0.00 250.00
P2 10.00 75 500.00 257.50 0.00 757.50
P3 10.00 175 500.00 515.00 787.50 1802.50
Salesperson 275 1250.00 772.50 787.50 2810.00
Press Enter to process the next salesperson or Report Totals:
Report Totals: 875 3200.00 2111.50 1470.00 6781.50
Number of salespersons: 3
Average salesperson Bonus: 2260.50
Press any key to continue
Source:
//P04_ex Bonus Calculator w/ Loops - An Example Juan Marquez TR 1:00pm
/*
This program allows a payroll clerk to determine what the bonus
will be for any number of salespersons.
The bonus is based on the quantity sold in each of the three periods.
The bonus rate for the three periods is the same, so the bonus rate
is entered once. It must be between $5 and $10.
A quantity between 0 and 200 must be entered for each period.
Enter the three quantities sold separated by a space.
Enter a 0 (zero) for any period with no sales.
The maximum quantity for the bonus is 200.
The more that is ordered, the more the salesperson earns.
Bonuses are given depending on the quantity sold:
Level 1: Quantities 1- 50 100% of the salesperson's bonus rate
Level 2: Quantities 51-100 103% of the salesperson's bonus rate
Level 3: Quantities 101-200 105% of the salesperson's bonus rate
*/
#include <iostream> // cin and cout
using namespace std;
#include <stdlib.h> // system()
void main()
{
//Declare constants
//LEVEL1 is not actually used in the program, but is declared here
//for clarification on the 3 different levels. LEVEL1 is not used
//because it is equal to one and multiplying by one gives the same value.
const double LEVEL1 = 1.00, LEVEL2 = 1.03, LEVEL3 = 1.05;
//Declare variables
int salespersonCount, loopCount;
int p1Qty, p2Qty, p3Qty, salespersonQty, reportQty = 0;
double rate, averageBonus;
double p1Bonus1, p1Bonus2, p1Bonus3, p1Total;
double p2Bonus1, p2Bonus2, p2Bonus3, p2Total;
double p3Bonus1, p3Bonus2, p3Bonus3, p3Total;
double salespersonTotal1, salespersonTotal2,
salespersonTotal3, salespersonTotal;
//Accumulators must be initialized to zero
double reportTotal1 = 0, reportTotal2 = 0,
reportTotal3 = 0, reportTotal = 0;
bool qtyError;
char prompt;
//Set decimal formatting
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "P04_ex - Juan Marquez TR 1:00pm \n\n";
//Get salesperson count
//do-while is processed until a valid salespersonCount is entered (0 or greater)
do
{
cout
<< "Enter the number of salespersons to process. \n"
<< "Enter 0 (zero) to exit: ";
cin >> salespersonCount;
if (salespersonCount < 0)
cout << "Error: The number of salespersons to "
<< "process must be 0 or greater. Try again.\n\n";
} while (salespersonCount < 0);
//Set the loopCounter to one
loopCount = 1;
//Process salespersons
//while loop is controlled by the number of salespersons to process
//if zero was entered, then we just skip the loop
while (loopCount <= salespersonCount)
{
//Display which salesperson we are currently processing
cout << "\nP04_ex - Juan Marquez TR 1:00pm \t\tSalesperson: "
<< loopCount << endl << endl;
//Get a valid rate
do
{
cout << "Enter a bonus rate between $5.00 and $10.00: ";
cin >> rate;
if (rate < 5.0 || rate > 10.00)
cout << "Error: The rate must be between $5.00 and $10.00. "
<< "Try Again...\n\n";
} while (rate < 5.0 || rate > 10.00);
/*
Get the quantities for each of the three periods and validate them.
All three cannot be zero.
None of them can be < zero.
None can be > 200
If any edit fails, display an error message and prompt for the quantities again
*/
do
{
qtyError = false; // assume correct values will be entered
cout << "Enter three quantities between 0 and 200 separated by a space: ";
cin >> p1Qty >> p2Qty >> p3Qty;
if ((p1Qty == 0) && (p2Qty == 0) && (p3Qty == 0))
{
cout << "Error: All three quantities can not be zero. "
<< "Try Again...\n\n";
qtyError = true;
}
if ((p1Qty < 0) || (p2Qty < 0) || (p3Qty < 0))
{
cout << "Error: Negative values are not valid. Try Again...\n\n";
qtyError = true;
}
if ((p1Qty > 200) || (p2Qty > 200) || (p3Qty >200))
{
cout << "Error: Values may not exceed 200. Try Again...\n\n";
qtyError = true;
}
} while (qtyError);
//Process the quantity sold each of the 3 periods.
//Calc Bonus for period 1
if (p1Qty < 51)
{
p1Bonus1 = p1Qty * rate;
p1Bonus2 = 0;
p1Bonus3 = 0;
}
else
{
if (p1Qty < 101)
{
p1Bonus1 = 50 * rate;
p1Bonus2 = (p1Qty - 50) * (rate * LEVEL2);
p1Bonus3 = 0;
}
else
{
p1Bonus1 = 50 * rate;
p1Bonus2 = 50 * (rate * LEVEL2);
p1Bonus3 = (p1Qty - 100) * (rate * LEVEL3);
}
}
//Calc Bonus for period 2
if (p2Qty < 51)
{
p2Bonus1 = p2Qty * rate;
p2Bonus2 = 0;
p2Bonus3 = 0;
}
else
{
if (p2Qty < 101)
{
p2Bonus1 = 50 * rate;
p2Bonus2 = (p2Qty - 50) * (rate * LEVEL2);
p2Bonus3 = 0;
}
else
{
p2Bonus1 = 50 * rate;
p2Bonus2 = 50 * (rate * LEVEL2);
p2Bonus3 = (p2Qty - 100) * (rate * LEVEL3);
}
}
//Calc Bonus for period 3
if (p3Qty < 51)
{
p3Bonus1 = p3Qty * rate;
p3Bonus2 = 0;
p3Bonus3 = 0;
}
else
{
if (p3Qty < 101)
{
p3Bonus1 = 50 * rate;
p3Bonus2 = (p3Qty - 50) * (rate * LEVEL2);
p3Bonus3 = 0;
}
else
{
p3Bonus1 = 50 * rate;
p3Bonus2 = 50 * (rate * LEVEL2);
p3Bonus3 = (p3Qty - 100) * (rate * LEVEL3);
}
}
//Sum the period total (going across the columns)
p1Total = p1Bonus1 + p1Bonus2 + p1Bonus3;
p2Total = p2Bonus1 + p2Bonus2 + p2Bonus3;
p3Total = p3Bonus1 + p3Bonus2 + p3Bonus3;
//Sum the salesperson totals (going down the columns)
salespersonTotal1 = p1Bonus1 + p2Bonus1 + p3Bonus1;
salespersonTotal2 = p1Bonus2 + p2Bonus2 + p3Bonus2;
salespersonTotal3 = p1Bonus3 + p2Bonus3 + p3Bonus3;
salespersonTotal = p1Total + p2Total + p3Total;
salespersonQty = p1Qty + p2Qty + p3Qty;
//Accumulate the report totals
//Shorthand notation += and -=
//+= means to add the value in the variable on the left to the one on the right
//and to store the result back in the variable on the left.
// reportTotal1 = reportTotal1 + salespersonTotal1;
reportTotal1 += salespersonTotal1;
reportTotal2 += salespersonTotal2;
reportTotal3 += salespersonTotal3;
reportTotal += salespersonTotal;
reportQty += salespersonQty;
//Display Headings
cout << endl
<< "Period \tRate \tQty \t100% \t103% \t105% \tTotal \n\n"
//Display Details
<< "P1 \t" << rate << "\t" << p1Qty << "\t" << p1Bonus1 << "\t"
<< p1Bonus2 << "\t" << p1Bonus3 << "\t" << p1Total << endl
<< "P2 \t" << rate << "\t" << p2Qty << "\t" << p2Bonus1 << "\t"
<< p2Bonus2 << "\t" << p2Bonus3 << "\t" << p2Total << endl
<< "P3 \t" << rate << "\t" << p3Qty << "\t" << p3Bonus1 << "\t"
<< p3Bonus2 << "\t" << p3Bonus3 << "\t" << p3Total << endl
<< endl
//Display Salesperson Totals
<< "Salesperson \t" << salespersonQty
<< "\t" << salespersonTotal1
<< "\t" << salespersonTotal2
<< "\t" << salespersonTotal3
<< "\t" << salespersonTotal
<< endl << endl
<< "Press Enter to process the next salesperson or Report Totals: ";
cin.ignore();
prompt = cin.get();
system("cls"); //clear screen for next salesperson
//Increment the count controlling the loop
loopCount++; //Shorthand: ++ increments the variable by one.
} //end of while (loopCount < salespersonCount)
//Before dividing by salespersonCount, make sure it is not equal to zero.
if (salespersonCount > 0)
averageBonus = reportTotal / salespersonCount;
else
averageBonus = 0;
//Display Report Totals
cout << "\nReport Totals: \t" << reportQty
<< "\t" << reportTotal1
<< "\t" << reportTotal2
<< "\t" << reportTotal3
<< "\t" << reportTotal
<< endl << endl
<< "Number of salespersons: \t" << salespersonCount << endl
<< "Average salesperson Bonus:\t" << averageBonus << endl << endl;
return;
} // end of main