I have got the program writing to the files, but i am unsure where to even begin to pull the information back out.
the information is saved like this:
15 Ray Allrich # 10.25 0 0
in one file the other file has the id number and hours worked
5 40
Any help would be greatly appreciated.
const int NUM_EMPS = 6;
struct Pay
{ double OverTime;
double GrossPay;
double NetPay;
double Tax;
double TaxRate = 0.15;
double TotalNet;
double TotalGross;
double DepCost = 20.00;
int count;
int Hours;
};
class Employee
{
private:
int id; // employee ID
string name; // employee name
double hourlyPay; // pay per hour
int numDeps; // number of dependents
int type; // employee type
public:
string getName();
double getHrpay();
int getDeps();
int getType();
int getId();
Employee( int initId=0, string initName="",
double initHourlyPay=0.0,
int initNumDeps=0, int initType=0 ); // Constructor
bool set(int newId, char newName[], double newHourlyPay,
int newNumDeps, int newType);
};
Employee::Employee( int initId, char initName[],
double initHourlyPay,
int initNumDeps, int initType )
{
bool status = set( initId, initName[], initHourlyPay,
initNumDeps, initType );
if ( !status )
{
id = 0;
name = " ";
hourlyPay = 0.0;
numDeps = 0;
type = 0;
}
}
bool Employee::set( int newId, string newName, double newHourlyPay,
int newNumDeps, int newType )
{
bool status = false;
if ( newId > 0 && newHourlyPay > 0 && newNumDeps >= 0 &&
newType >= 0 && newType <= 1 )
{
status = true;
id = newId;
name = newName;
hourlyPay = newHourlyPay;
numDeps = newNumDeps;
type = newType;
}
return status;
}
int Employee::getId()
{
return id;
}
string Employee::getName()
{ return name;
}
double Employee:: getHrpay()
{ return hourlyPay;
}
int Employee::getDeps()
{return numDeps;
}
int Employee::getType()
{
return type;
}
int main()
{
int newId;
string newName;
double newHourlyPay;
int newNumDeps;
int newType;
double gross;
// declare struct as an array and fill
Pay pay[NUM_EMPS];
Employee EmpInfo[6];
Employee employee[NUM_EMPS];
ofstream outputfile;
ofstream hoursfile;
//open file
outputfile.open("employeeinfo.txt");
if (!outputfile)
{
cout << "Error opening file!" << endl;
}
hoursfile.open("employeehours.txt");
if (!hoursfile)
{
cout << "Error opening file!" << endl;
}
// get information from user
for (int index =0; index < NUM_EMPS; index++)
{
cout << "Please enter ID Number: " << endl ;
cin >> newId;
cin.ignore();
cout << "Please enter Name: " << endl ;
cin.width(20);
getline (cin, newName);
cout << "Please enter hourly pay: " << endl ;
cin >> newHourlyPay;
cout<< "Please enter number of dependents: " << endl ;
cin >> newNumDeps;
cout << "Please enter employee type: " << endl ;
cin >>newType;
employee[index].set( newId, newName, newHourlyPay, newNumDeps, newType );
}
// write to file.
for (int index = 0; index<NUM_EMPS; index++)
{
outputfile << employee[index].getId() << " " << employee[index].getName()<< setw(6)
<< " " << setw(2) << employee[index].getHrpay() << setw(2) << employee[index].getDeps() <<
setw(2) << employee[index].getType() << endl;
}
// Ask for hours.
for (int index = 0; index < NUM_EMPS; index++)
{cout << "Hours worked for " << employee[index].getName() << ": ";
cin >> pay[index].Hours;
}
for (int index=0; index < NUM_EMPS; index++)
{hoursfile << employee[index].getId() << setw(6) << pay[index].Hours << endl;
}
//close file
outputfile.close();
hoursfile.close();
return 0;
}