//----------------------------------------------- // structDemo // // Demonstrate a record (struct) in C++. // // D. Searls // Asbury College // Oct 2009 //----------------------------------------------- #include <iostream> #include <iomanip> using namespace std; struct employeeRecord // Declare an employee record data type { string identifier; double hours; double wageRate; double grossPay; }; const double MAXHOURS = 40.0; const double OT_MULTIPLIER = 1.5; int main() { employeeRecord employee; // Get Employee Data cout << "Enter employee ID: "; cin >> employee.identifier; cout << "Enter hourly wage: "; cin >> employee.wageRate; cout << "Enter hours worked: "; cin >> employee.hours; cout << endl << endl; // Calculate Gross Pay if (employee.hours <= MAXHOURS) { employee.grossPay = employee.hours * employee.wageRate; } else { employee.grossPay = MAXHOURS * employee.wageRate + (employee.hours - MAXHOURS) * OT_MULTIPLIER * employee.wageRate; } // Display Employee ID and gross pay cout << fixed << setprecision(2); cout << "Gross pay for employee " << employee.identifier << " is " << employee.grossPay << endl << endl; return 0; } //----------------------------------------------- // structDemo2 // // Demonstrate using a record (struct) as a // parameter in C++. // // D. Searls // Asbury College // Oct 2009 //----------------------------------------------- #include <iostream> #include <iomanip> using namespace std; struct employeeRecord // Declare an employee record data type { string identifier; double hours; double wageRate; double grossPay; }; const double MAXHOURS = 40.0; const double OT_MULTIPLIER = 1.5; //----------------------------------------------- // getData // // Get the employee data from the keyboard. // // Out Parameter: empData //----------------------------------------------- void getData(employeeRecord& empData) { cout << "Enter employee ID: "; cin >> empData.identifier; cout << "Enter hourly wage: "; cin >> empData.wageRate; cout << "Enter hours worked: "; cin >> empData.hours; cout << endl << endl; } //----------------------------------------------- // calculateGrossPay // // calculate the gross pay of the employee. // // In/Out Parameter: employee //----------------------------------------------- void calculateGrossPay(employeeRecord& employee) { double regPay; // Regular pay double otPay; // Overtime pay if (employee.hours <= MAXHOURS) { regPay = employee.hours * employee.wageRate; otPay = 0.0; } else { regPay = MAXHOURS * employee.wageRate; otPay = (employee.hours - MAXHOURS) * OT_MULTIPLIER * employee.wageRate; } employee.grossPay = regPay + otPay; } //----------------------------------------------- // displayReport // // Display a report showing the employee ID and // gross pay. // // In Parameter: emp //----------------------------------------------- void displayReport(employeeRecord emp) { cout << fixed << setprecision(2); cout << "Gross pay for employee " << emp.identifier << " is " << emp.grossPay << endl << endl; } //*********************************************** // M A I N D R I V E R //*********************************************** int main() { employeeRecord employee; getData(employee); calculateGrossPay(employee); displayReport(employee); return 0; } //----------------------------------------------- // structDemo3 // // Demonstrate an array of records (structs) in C++. // // D. Searls // Asbury College // Oct 2009 //----------------------------------------------- #include <iostream> #include <iomanip> #include <fstream> using namespace std; struct employeeRecord // Declare an employee record data type { string identifier; double hours; double wageRate; double grossPay; }; const double MAXHOURS = 40.0; const double OT_MULTIPLIER = 1.5; const int CAPACITY = 100; //----------------------------------------------- // getData // // Get the employee data from a file. // // Out Parameters: empData, n //----------------------------------------------- void getData(employeeRecord empData[], int& n) { ifstream infile; string ident; double hours; double wage; infile.open("employeeData.txt"); n = 0; infile >> ident >> wage >> hours; while (!infile.fail() && n < CAPACITY) { empData[n].identifier = ident; empData[n].hours = hours; empData[n].wageRate = wage; n++; infile >> ident >> wage >> hours; } infile.close(); } //----------------------------------------------- // calculateGrossPay // // Calculate the gross pay of each employee. // // In/Out Parameter: empList // // In Parameter: n //----------------------------------------------- void calculateGrossPay(employeeRecord empList[], int n) { double regPay; // Regular pay double otPay; // Overtime pay for (int i = 0; i < n; i++) { if (empList[i].hours <= MAXHOURS) { regPay = empList[i].hours * empList[i].wageRate; otPay = 0.0; } else { regPay = MAXHOURS * empList[i].wageRate; otPay = (empList[i].hours - MAXHOURS) * OT_MULTIPLIER * empList[i].wageRate; } empList[i].grossPay = regPay + otPay; } } //----------------------------------------------- // displayReport // // Display a report showing the employee ID and // gross pay for each employee. // // In Parameters: emp, n //----------------------------------------------- void displayReport(const employeeRecord emp[], int n) { cout << fixed << setprecision(2); cout << " ID Gross Pay" << endl; cout << "---- ---------" << endl; for (int i = 0; i < n; i++) { cout << left << setw(4) << emp[i].identifier; cout << right << setw(11) << emp[i].grossPay; cout << endl; } cout << endl; } //*********************************************** // M A I N D R I V E R //*********************************************** int main() { employeeRecord employee[CAPACITY]; int n; getData(employee, n); calculateGrossPay(employee, n); displayReport(employee, n); return 0; }