I think the pieces, taken separately, are okay, but they don't seem to play together well.
//lab12: This program requests input of ten employee names and salaries;
//a file is created and they are written into it, and the file is closed.
//The file is then reopened and the records displayed.
#include<iostream>
#include<fstream>
#include<string>
#include<stdlib>
using namespace std;
struct Employee
{
char name[81];
double salary;
};
int main()
{
const int NUMBEROFWORKERS = 10;
Employee e;
char again;
fstream employeeOutputFile("employee.dat", ios::out);
if (employeeOutputFile.fail())
employeeOutputFile.open("employee.dat", ios::out);
do
{
cout<<"Enter names and salaries for " << NUMBEROFWORKERS << " employees:\n";
for (int i = 0; i < NUMBEROFWORKERS; i++)
{
cout << "Enter name:" <<endl;
cin.getline(e[i].name,81);
cout << "Enter salary:" <<endl;
cin >> e[i].salary;
cout << endl;
//Write an employee record to the file pg
employeeOutputFile.write(e, sizeof(e));
}
cout << "Do you want to enter another record (y/n)?";
cin >> again;
cin.ignore();
} while(again =='Y' || again=='y');
employeeOutputFile.close();
Employee eArray[10];
int i =0;
fstream employeeInputFile("employee.dat", ios::in);
if (employeeInputFile.fail())
cout << "Problem opening file.";
//while(!eof() ) //Check for end of file
for (i = 0; i < NUMBEROFWORKERS; i++)
{
employeeInputFile.read(e, sizeof(e));
strcpy( eArray[i].name,e.name);//get name
strcpy( eArray[i].name,e.salary);//get salary
}
employeeInputFile.close(); //close the file
cout<<"EMPLOYEE RECORDS:"<<endl;
for (i = 0; i < NUMBEROFWORKERS; i++)
{
cout << eArray[i].name << endl;
cout << eArray[i].salary << endl << endl;
}
return 0;
}