Creating a file of Employees

The Problem

We want to write a program that will create a file of hours employees have worked. In addition to the number of hours worked you must store the name of the employee. We will check to see if our file was created successfully by writing a second program to read this file.

Class Description

Data Members

We need a class Employee to write this program. This class will contain two member data items:

Member Functions

The first part of the assignment requires that we create a file, so we must have a member function to create an employee and a function to write an employee to the file. Next we must have functions to complete the second part of the problem, i.e., read the file and print out the information. To do this we need a function to read an employee from a file and a function to print an employee to the screen.

Creating the Employee Class

The employee's name must be stored in a string, while the hours can be stored in an integer. Once we know this, we know that the parameters for the constructor will be a string followed by an integer. Notice that when you have a string parameter for a constructor, you must declare the parameter as a constant reference. Constant reference parameter x of type X is created by putting the identifier const in front of the type name and the ampersand (&) after the type name and before the variable.
const X &x

Class Definition

class Employee
{
public:
	Employee();
	Employee(const string &, const string &,int);
	void write (ostream&);
	void read(istream&);
	void print();
private:
	string _firstName;
                      string _lastName;
	int _hours;
};

The second, trivial constructor is almost always required, so we will include it in our definition.

The Code

Employee functions

The two constructors are very strait forward. In the trivial constructor just assign the two member variables to default values. The second simply initializes the member variables with values sent to it.

Employee::Employee()
{
	_firstName = "";
                     _lastName = "";
	_hours = 0;
}

Employee::Employee(const string &fname,const string &lname, int hours)
{
	_firstName = fname;
	_LastName = lname;
	_hours = hours;
}

You might think you could read the employee name by using the extract operator (>>). This is not possible since the first and second name are separated by a space. Whenever the extract operator sees a space it assumes this is the end of the string. To read two names on a line simply use extract twice.

void Employee::read(istream &in)
{
	in>>_firstName;
                     in>>_lastName;
	in>>_hours;
 }

Application functions

Once again the only nontrivial function in the application is run. In this case run takes two forms (one commented out). We use the first part (commented out) to create the file and the second to show the file created.

void MyApplication::run()
{
/*	ofstream out("exam2.dat");
	int size;
	cout<<"How many employees do you want to enter? >";
	cin>>size;
	out<< size;//file begins with the size of the file
	for(int i = 0;i< size;i++)
	{
		cout<< "First Name >";
		string fname;
		cin>>fname;
                                           cout<<"Last Name> ";
                                           cin>>lname;
		int hours;
		cout<< "Hours worked >";
		cin>>hours;
		Employee e(fname,lname,hours);
		e.write(out);
	}     */
	ifstream in("exam2.dat");
	if (!in)//no file found
		throw("File exam2.dat not in the current directory");
	int size;
	in>>size;
	for (int i = 0;i < size;i++)
	{
		Employee e;
		e.read(in);
		e.print();
		cout << endl;
	}
	
}

The Project

This project was created as using Console Wizard . You can get the entire project by clicking here. Back to week 1.