I'm pretty happy with this (I think, I need to check the numbers still) but for some reason endl isn't working and all the information is running together. Any tips on getting the next iteration to go to the next line?
Code:
#include <fstream> //required for file streams
#include <iostream>
#include <cstdlib> //for definition of EXIT_FAILURE
using namespace std;
#define inFileEmp "employees.txt" //employee file
#define inFileHour "hours.txt" //hour file
#define outFile "pay.txt" //payroll file
//Functions used
void processEmp (ifstream&, ifstream&, ofstream&); //process all employees and display name, pay, etc
int main()
{
ifstream eds; //input: employee data stream
ifstream hrds; //input: hour data stream
ofstream pds; //output: all info per employee
//Prepare files
eds.open(inFileEmp);
if (eds.fail())
{
cerr << "*** ERROR: Cannot open " << inFileEmp << " for input." << endl;
return EXIT_FAILURE; //failure return
}
hrds.open(inFileHour);
if (hrds.fail())
{
cerr << "*** ERROR: Cannot open " << inFileHour << " for input." << endl;
return EXIT_FAILURE; //failure return
}
pds.open(outFile);
if (pds.fail())
{
cerr << "*** ERROR: Cannot open " << outFile << " for output." << endl;
return EXIT_FAILURE; //failure return
}
processEmp(eds,
hrds,
pds);
//Close files
eds.close();
hrds.close();
pds.close();
return 0;
}
void processEmp
(ifstream& eds,
ifstream& hrds,
ofstream& pds)
{
string name; //input: employee name from inFileEmp
int id1; //input: employee id from inFileEmp
float rate; //input: employee pay rate from inFileEmp
int id2; //input: employee id from inFileHour
int hours; //input: employee hours worked from inFileHour
float pay; //output: pay
eds >> name >> id1 >> rate;
while (!eds.eof())
{
hrds >> id2 >> hours;
if (id1 == id2)
{
pay = rate * hours;
pds << name << " " << id1 << " " << rate << " " << hours << " $" << pay << endl;
eds >> name >> id1 >> rate;
}
else if (id1 != id2)
{
hrds >> id2 >> hours;
if (hrds.eof())
{
pay = 0;
pds << name << " " << id1 << " " << rate << " " << hours << " $" << pay << endl;
eds >> name >> id1 >> rate;
}
}
}
}
Problem:
Assume there are two input files for a payroll program. The first contains the employee’s name (a string), id number (an integer) an hourly rate (a real number). The second contains the employee’s id number and hours worked (a real number). The records in the first file are ordered by name and the records in the second file are ordered by id number. If a person did not work during the current time period, there will be no record in the second file for that person. Write a file of payroll records to an output file for all employees. Each record should contain the employee name followed by # followed by the hours worked, hourly rate, and pay amount. Use a space as a separator between items in the output file. There should be a record in the output file for every person including those who did not work in that pay period.