rendered paste body#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.0, LEVEL2 = 1.5, LEVEL3 = 2.0;
//Declare variables
int employeesCount, loopCount;
int e1Qty, e2Qty, e3Qty, e4Qty, employeesQty, reportQty = 0;
double rate, averageBonus;
double p1Bonus1, p1Bonus2, p1Bonus3, p1Total;
double p2Bonus1, p2Bonus2, p2Bonus3, p2Total;
double p3Bonus1, p3Bonus2, p3Bonus3, p3Total;
double p4Bonus1, p4Bonus2, p4Bonus3, p4Total;
double employeesTotal1, employeesTotal2,
employeesTotal3, employeesTotal;
//Accumulators must be initialized to zero
double reportTotal1 = 0, reportTotal2 = 0,
reportTotal3 = 0, reportTotal4 = 0,
reportTotal = 0;
bool qtyError;
char prompt;
//Equation to fix the decimal to 2 points.
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "P04_ex - Juan Marquez TR 1:00pm \n\n";
//Get employees count
//do-while is processed until a valid employeesCount is entered (0 or greater)
do
{
cout
<< "Enter the number of employees to process.\n"
<< "Enter 0 (zero) to exit: ";
cin >> employeesCount;
if (employeesCount < 0)
cout << "Error: The number of employees to "
<< "process must be 0 or greater. Try again.\n\n";
} while (employeesCount < 0);
//Set the loopCounter to one
loopCount = 1;
//Process employees
//while loop is controlled by the number of employees to process
//if zero was entered, then we just skip the loop
while (loopCount <= employeesCount)
{
//Display which employees we are currently processing
cout << "\nP04_ex - Juan Marquez TR 1:00pm \t\tEmployees: "
<< loopCount << endl << endl;
//Get a valid rate
do
{
cout << "Enter a bonus rate between $5.25 and $30.00: ";
cin >> rate;
if (rate < 5.25 || rate > 30.00)
cout << "Error: The rate must be between $5.25 and $30.00. "
<< "Try Again...\n\n";
} while (rate < 5.25 || rate > 30.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 four quantities between 0 and 60 separated by a space: ";
cin >> e1Qty >> e2Qty >> e3Qty >> e4Qty;
if ((e1Qty == 0) && (e2Qty == 0) && (e3Qty == 0) && (e4Qty == 0))
{
cout << "Error: All three quantities can not be zero. "
<< "Try Again...\n\n";
qtyError = true;
}
if ((e1Qty < 0) || (e2Qty < 0) || (e3Qty < 0) || (e4Qty < 0))
{
cout << "Error: Negative values are not valid. Try Again...\n\n";
qtyError = true;
}
if ((e1Qty > 200) || (e2Qty > 60) || (e3Qty > 60) || (e4Qty > 60))
{
cout << "Error: Values may not exceed 60. Try Again...\n\n";
qtyError = true;
}
} while (qtyError);
//Process the quantity sold each of the 3 periods.
//Calc Bonus for period 1
if (e1Qty < 41)
{
p1Bonus1 = e1Qty * rate;
p1Bonus2 = 0;
p1Bonus3 = 0;
}
else
{
if (e1Qty < 51)
{
p1Bonus1 = 40 * rate;
p1Bonus2 = (e1Qty - 40) * (rate * LEVEL2);
p1Bonus3 = 0;
}
else
{
p1Bonus1 = 40 * rate;
p1Bonus2 = 40 * (rate * LEVEL2);
p1Bonus3 = (e1Qty - 50) * (rate * LEVEL3);
}
}
//Calc Bonus for period 2
if (e2Qty < 41)
{
p2Bonus1 = e2Qty * rate;
p2Bonus2 = 0;
p2Bonus3 = 0;
}
else
{
if (e2Qty < 51)
{
p2Bonus1 = 40 * rate;
p2Bonus2 = (e2Qty - 40) * (rate * LEVEL2);
p2Bonus3 = 0;
}
else
{
p2Bonus1 = 40 * rate;
p2Bonus2 = 40 * (rate * LEVEL2);
p2Bonus3 = (e2Qty - 50) * (rate * LEVEL3);
}
}
//Calc Bonus for period 3
if (e3Qty < 41)
{
p3Bonus1 = e3Qty * rate;
p3Bonus2 = 0;
p3Bonus3 = 0;
}
else
{
if (e3Qty < 40)
{
p3Bonus1 = 40 * rate;
p3Bonus2 = (e3Qty - 50) * (rate * LEVEL2);
p3Bonus3 = 0;
}
else
{
p3Bonus1 = 40 * rate;
p3Bonus2 = 40 * (rate * LEVEL2);
p3Bonus3 = (e3Qty - 50) * (rate * LEVEL3);
}
}
//Calc Bonus for period 4
if (e4Qty < 41)
{
p4Bonus1 = e4Qty * rate;
p4Bonus2 = 0;
p4Bonus3 = 0;
}
else
{
if (e4Qty < 51)
{
p4Bonus1 = 40 * rate;
p4Bonus2 = (e4Qty - 50) * (rate * LEVEL2);
p4Bonus3 = 0;
}
else
{
p4Bonus1 = 40 * rate;
p4Bonus2 = 40 * (rate * LEVEL2);
p4Bonus3 = (e4Qty - 50) * (rate * LEVEL3);
}
}
//Sum the period total (going across the columns)
p1Total = p1Bonus1 + p1Bonus2 + p1Bonus3;
p2Total = p2Bonus1 + p2Bonus2 + p2Bonus3;
p3Total = p3Bonus1 + p3Bonus2 + p3Bonus3;
p4Total = p4Bonus1 + p4Bonus2 + p4Bonus3;
//Sum the employees totals (going down the columns)
employeesTotal1 = p1Bonus1 + p2Bonus1 + p3Bonus1 + p4Bonus1;
employeesTotal2 = p1Bonus2 + p2Bonus2 + p3Bonus2 + p4Bonus2;
employeesTotal3 = p1Bonus3 + p2Bonus3 + p3Bonus3 + p4Bonus3;
employeesTotal = p1Total + p2Total + p3Total + p4Total;
employeesQty = e1Qty + e2Qty + e3Qty + e4Qty;
//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 + employeesTotal1;
reportTotal1 += employeesTotal1;
reportTotal2 += employeesTotal2;
reportTotal3 += employeesTotal3;
reportTotal += employeesTotal;
reportQty += employeesQty;
//Display Headings
cout << endl
<< "Period \tRate \tQty \t100% \t150% \t200% \tTotal \n\n"
//Display Details
<< "P1 \t" << rate << "\t" << e1Qty << "\t" << p1Bonus1 << "\t"
<< p1Bonus2 << "\t" << p1Bonus3 << "\t" << p1Total << endl
<< "P2 \t" << rate << "\t" << e2Qty << "\t" << p2Bonus1 << "\t"
<< p2Bonus2 << "\t" << p2Bonus3 << "\t" << p2Total << endl
<< "P3 \t" << rate << "\t" << e3Qty << "\t" << p3Bonus1 << "\t"
<< p3Bonus2 << "\t" << p3Bonus3 << "\t" << p3Total << endl
<< "P4 \t" << rate << "\t" << e4Qty << "\t" << p4Bonus1 << "\t"
<< p4Bonus2 << "\t" << p4Bonus3 << "\t" << p4Total << endl
<< endl
//Display employees Totals
<< "employees \t" << employeesQty
<< "\t" << employeesTotal1
<< "\t" << employeesTotal2
<< "\t" << employeesTotal3
<< endl << endl
<< "Press Enter to process the next employees or Report Totals: ";
cin.ignore();
prompt = cin.get();
system("cls"); //clear screen for next employees
//Increment the count controlling the loop
loopCount++; //Shorthand: ++ increments the variable by one.
} //end of while (loopCount < employeesCount)
//Before dividing by employees to Count, make sure it is not equal to zero.
if (employeesCount > 0)
averageBonus = reportTotal / employeesCount;
else
averageBonus = 0;
//Display Report Totals
cout << "\nReport Totals: \t" << reportQty
<< "\t" << reportTotal1
<< "\t" << reportTotal2
<< "\t" << reportTotal3
<< "\t" << reportTotal4
<< endl << endl
<< "Number of employees: \t" << employeesCount << endl
<< "Average employees Bonus:\t" << averageBonus << endl << endl;
return;
} // end of main