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;
}

In line #20 you declare a single object of type 'Employee'

In line #33 you attempt to dereferrence e using subscripts.. on your single 'Employee' object.

In lne #35 you again attempt to dereferrence your single e object using array subscripts.

You do not actually create an array of 'Employees' until line #48.

Edited 6 Years Ago by Clinton Portis: n/a

Thanks; I lost a day due to a corrupted IDE, but I'm trying a different one now. I tried quite a few changes, per your suggestions I think, but it's still not right. I hope I'm gaining on it; anyway, here's what I've gotten to:

//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>
using namespace std;

struct Employee
{ 
	char name[81];
	double salary;
};

int main()
{ 
	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 employees:\n";
		cout	<<	"Enter name:"	<<endl;
		cin.getline (e.name,81);
		employeeOutputFile.write("employee.dat");

		cout	<<	"Enter salary:"	<<endl;
		cin		>>	e.salary;
		cout	<< endl;
		employeeOutputFile.write("employee.dat");
				
		cout	<<	"Do you want to enter another record (y/n)?";
		cin.ignore(); 
		cin	>>	again;
	}	while(again =='Y' || again=='y');
	employeeOutputFile.close();
	
	fstream employeeInputFile("employee.dat", ios::in);
	if (employeeInputFile.fail())
		cout	<< "Problem opening file.";
	cout<<"EMPLOYEE RECORDS:"<<endl;
	while(!eof() )
	{ 
		employeeInputFile.read("employee.dat");
		cout	<< e.name	<< endl;
		cout	<< e.salary	<< endl	<< endl;
	}
	employeeInputFile.close();
	
	return 0;
}

I think your output and stuff was closer the first time. As of now your write statements aren't really doing anything (I can't even get them to compile like that and they have the wrong # of arguments). To be honest, I find it much easier to use ifstream and ofstream and use the >> and << operators (just like with cin and cout). Then you could use a string for employee name and all those strcpys live to fight another day. That's just my 0.015 dollars worth.

Edited 6 Years Ago by jonsca: n/a

Thanks - I finally got it to work (code follows), though I'm not sure I'm using all the features the way they're supposed to be used. For one thing, I did it without seeming to need strcpy or parameters like sizeof(e). So it works, but I'm not too proud of it!

//lab12: This program requests input of 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<cstdlib>
using namespace std;

//Create the employee structure before fn main
struct Employee
{ 
	char name[81];
	double salary;
};

int main()
{ 
	Employee e;
	char again;
	int i = 0;
	
	fstream employeeOutputFile("employee.dat", ios::out);
	if (employeeOutputFile.fail())
		employeeOutputFile.open("employee.dat", ios::out);
	
	do
	{ 
		cout	<<"Enter names and salaries for employees:\n";
		cout	<<	"Enter name: ";
		cin.getline (e.name,81);
		employeeOutputFile	<< e.name	<< endl;

		cout	<<	"Enter salary:"	<<endl;
		cin		>>	e.salary;
		employeeOutputFile	<< e.salary	<< endl; 

		i++; //counts how many employee records are entered into the file
		cout	<<	"Do you want to enter another record (y/n)?";
		cin	>>	again; //want to append another record to the file
		cin.ignore(); 
	}	while(again =='Y' || again=='y');

	//Close the file
	employeeOutputFile.close();

	//Read the records into a 1-D array
	fstream employeeInputFile("employee.dat", ios::in);
	if (employeeInputFile.fail())
		cout	<< "Problem opening file.";
		
	const int NUMBEROFEMPLOYEES = 10;
	Employee eArray[NUMBEROFEMPLOYEES]; //array of structures
	
	for (int counter = 0; counter < i; counter++)
	{
		employeeInputFile	>> eArray[counter].name;
		employeeInputFile	>> eArray[counter].salary;
	}
	
	//Close the file
	employeeInputFile.close();
	
	//Display the records of the array
	cout	<<	"EMPLOYEE RECORDS:"	<<endl;
	for (int counter = 0; counter < i; counter++)
	{
		cout	<< eArray[counter].name	<< endl;
		cout	<< eArray[counter].salary	<< endl;
	}	
	return 0;
}

What is sizeof(e) anyway given that name can have any size up to 79? If you wanted to use write() I would recommend using two separate calls, one using strlen(eArray[counter].name) as the second parameter and the other using sizeof(eArray[counter].salary) as the second parameter. So, all in all, I think the version you have is fine.

Comments
Good suggestions
This question has already been answered. Start a new discussion instead.