// FILE: Payroll.cpp // CREATES A COMPANY EMPLOYEE PAYROLL FILE // COMPUTES TOTAL COMPANY PAYROLL AMOUNT #include // required for file streams #include // for definition of EXIT_FAILURE // & EXIT_SUCCESS #include "money.h" // process employee data // ASSOCIATE PROGRAM IDENTIFIERS WITH EXTERNAL FILE NAMES #define in_file "Emp_File.dat" // employee file #define out_file "Salary.dat" // payroll file // Functions used ... // PROCESS ALL EMPLOYEES AND COMPUTE TOTAL money process_emp (ifstream&, // IN: employee data stream ofstream&); // IN: payroll data stream int main() { // Local data ... ifstream eds; // input: employee data stream ofstream pds; // output: payroll data stream money total_payroll; // output: total payroll // Prepare files. eds.open (in_file); if (eds.fail()) { cerr << "*** ERROR: Cannot open " << in_file << " for input." << endl; return EXIT_FAILURE; // failure return } pds.open(out_file); if (pds.fail()) { cerr << "***ERROR: Cannot open " << out_file << " for output." << endl; eds.close (); return EXIT_FAILURE; // failure return } // Set precision and flags for floating point output. cout.precision (2); cout.setf (ios::fixed); // Process all employees and compute total payroll. total_payroll = process_emp (eds, pds); // Display result. cout << "Total payroll is " << total_payroll << endl; // Close files. eds.close (); pds.close (); return EXIT_SUCCESS; // successful return } // PROCESS ALL EMPLOYEES AND COMPUTE TOTAL PAYROLL AMOUNT #include "apstring.h" money process_emp (ifstream& eds, // IN: employee file stream ofstream& pds) // IN: payroll file stream // Pre: eds and pds are prepared for input/output. // Post: Employee names and salaries are written from eds to pds // and the sum of their salaries is returned. // Returns: Total company payroll { apstring first_name; // input: employee first name apstring last_name; // input: employee last name double hours; // input: hours worked money rate; // input: hourly rate money salary; // output: gross salary money payroll; // return value - total company payroll // set for floating point output pds.setf (ios::fixed); pds.precision (2); payroll = 0.0; // Read first employee's data record. eds >> first_name >> last_name >> hours >> rate; while (!eds.eof ()) { salary = hours * rate; pds << first_name << last_name << salary << endl; payroll += salary; // Read next employee's data record. eds >> first_name >> last_name >> hours >> rate; } // end while return payroll; } // end process_emp