//********************************************************* // GrossPay3.cpp // // Calculate and display the gross pay given the hourly // wage and the number of hours worked. // // D. Searls // Asbury College // Aug 2003 //********************************************************* #include <iostream> #include <fstream> #include <iomanip> using namespace std; const double MAXHOURS = 40.0; const double OT_MULTIPLIER = 1.5; void getEmployeeData(string& identity, double& hours, double& wageRate); double regularPay(double hours, double rate); double overtimePay(double hours, double rate); double grossPay(double hours, double rate); void displayHeader(); void displayLine(string employeeID, double hours, double wage, double regPay, double otPay, double grossPay); int main() { string empID; double hoursWorked; double hourlyWage; ofstream outputFile; getEmployeeData(empID, hoursWorked, hourlyWage); // Write report to computer monitor cout << endl; displayHeader(); displayLine(empID, hoursWorked, hourlyWage, regularPay(hoursWorked, hourlyWage), overtimePay(hoursWorked, hourlyWage), grossPay(hoursWorked, hourlyWage)); cout << endl; return 0; } //----------------------------------------------- // getEmployeeData // // Get the employee ID, hours worked, and hourly // wage from the user. // // Out Parameters: identity, hours, wageRate //----------------------------------------------- void getEmployeeData(string& identity, double& hours, double& wageRate) { cout << "Enter employee id: "; cin >> identity; cout << "Enter number of hours: "; cin >> hours; cout << "Enter hourly wage: "; cin >> wageRate; } //----------------------------------------------- // regularPay // // Preconditions: hours >= 0, rate >= 0 // // Given the hours worked and the hourly rate of // pay, the worker's regular pay for the week is // calculated and returned. // // In Parameters: hours, rate //----------------------------------------------- double regularPay(double hours, double rate) { double regPay; if (hours <= MAXHOURS) { regPay = hours * rate; } else { regPay = MAXHOURS * rate; } return regPay; } //----------------------------------------------- // overtimePay // // Preconditions: hours >= 0, rate >= 0 // // Given the hours worked and the hourly rate of // pay, the worker's overtime pay for the week is // calculated and returned. // // In Parameters: hours, rate //----------------------------------------------- double overtimePay(double hours, double rate) { double otPay; if (hours <= MAXHOURS) { otPay = 0.0; } else { otPay = (hours - MAXHOURS) * OT_MULTIPLIER * rate; } return otPay; } //----------------------------------------------- // grossPay // // Preconditions: hours >= 0, rate >= 0 // // Given the hours worked and the hourly rate of // pay, the worker's gross pay for the week is // calculated and returned. // // In Parameters: hours, rate //----------------------------------------------- double grossPay(double hours, double rate) { return regularPay(hours, rate) + overtimePay(hours, rate); } //----------------------------------------------- // displayHeader // // Display the report header //----------------------------------------------- void displayHeader() { cout << fixed << setprecision(2); cout << setw(10) << left << "Emp ID"; cout << setw(7) << right << "Hours"; cout << setw(7) << "Wage"; cout << setw(10) << "Regular"; cout << setw(10) << "Overtime"; cout << setw(10) << "Gross" << endl; cout << "---------- ----- ----- -------- -------- --------" << endl; } //----------------------------------------------- // displayLine // // Display the employee ID, hours, wage, regular // pay, overtime pay and gross pay on one line. // // In Parameters: employeeID, hours, wage, regPay, // otPay, and grossPay //----------------------------------------------- void displayLine(string employeeID, double hours, double wage, double regPay, double otPay, double grossPay) { cout << setw(10) << left << employeeID; cout << setw(7) << right << hours; cout << setw(7) << wage; cout << setw(10) << regPay; cout << setw(10) << otPay; cout << setw(10) << grossPay << endl; }