Question is:
Create a class Employee with following constraints in mind:
- Declare three private data members named id,name,NIC of int,string,long respectively.
- Overload stream insertion and stream extraction operators.
Implement a global function named saveData, that'll allow the user to enter any number of employee's data and store it into a file named "edata.dat".
Implement a global function named readData, that'll read all the contents of "edata.dat" file and display them on console.
I have tried this one but confused a little bit how to arrange my code...
class Employee..
#include<iostream>
#include<string>
#include<fstream>
#include<cstdlib>
#include<iomanip>
using namespace std;
enum choices{write=1,read,end};
void saveData(fstream&);
void readData(fstream&);
void outputLine(int , const string, long);
class Employee
{
private:
int ID;
string name;
long NIC;
public:
Employee ( int I_D, string n, long N_I_C) //parameterized constructor
{
ID=I_D;
name=n;
NIC=N_I_C;
} //end constructor
int enterChoice()
{
int menuChoice;
cout<<"1. Write data in file. \n"<<
"2. Read Data from file. \n"<<
"3. Exit. \n";
cout<<"enter your choice: "<<endl;
do //input user's request
{
cin>>menuChoice;
} while(menuChoice<write && menuChoice>end);
return menuChoice;
} //end function enterChoice
implementation(int getChoice)
{
while(menuChoice!=end)
{
switch(getChoice)
{
case write:
saveData(xyz);
break;
case read:
readData(xyz);
break;
} //end switch
} //end while
} //end function implemenetation
//prototypes for overloaded operator ...
friend ostream &operator<<(ostream &, const Employee &); //overloading stream insertion operator
friend istream &operator>>(istream &, Employee &); //overloading stream extraction operator
}; //end class Employee
Definition of overloaded operators:
istream &operator>>( istream &input,Employee &emp)
{
input>>emp.ID>>' '>>emp.name>>' '>>emp.NIC;
return input;
} //end function
ostream &operator<<(ostream &output, const Employee &emp)
{
output<<left<<setw(15)<<"employee's ID"<<right<<setw(20)<<"Name"<<right<<setw(15)<<"NIC"<<endl;
output<<left<<setw(15)<<emp.ID<<right<<setw(20)<<emp.name<<right <<setw(15)<<emp.NIC<<endl;
return output;
} //end function
For writing data in file:
//writing data in file
void saveData(fstream &insertInFile)
{
ofstream abc("edata.txt",ios::out);
//exits program if file is not found
if(!abc)
{
cerr<<"file not found."<<endl;
exit(1);
} //end if
cout<<"enter end-of-file to end input."<<endl;
int id;
string Name;
long nic;
while(cin>>id>>Name>>nic)
{
abc<<id<<' '<<Name<<' '<<nic<<endl;
cout<<"?";
} //end while
} //end saveData
For reading data from file:
void readData(fstream &read4rmFile)
{
ifstream def("edata.dat",ios::in);
//exits if file is not found
if(!def)
{
cerr<<"file is not found."<<endl;
exit(1);
} //ends if
int id;
string Name;
long nic;
read4rmFile<<left<<setw(15)<<"employee's ID"<<right<<setw(20)<<"Name"<<right<<setw(15)<<"NIC"<<endl;
while(!read4rmFile.eof())
outputLine(id, Name,nic);
} //end readData
And finally the main and the global function:
int main()
{
Employee obj;
fstream xyz("edata.dat", ios::in| ios::out );
int number=obj.enterChoice();
obj.Employee(number);
return 0;
} //ends main
void outputLine(int a,const string b, long c)
{
cout<<left<<setw(15)<<a<<right<<setw(20)<<b<<right<<setw(15)<<c<<endl;
} //end outputLine
Now my confusion is the function readData isn't working properly and where to put the overloaded functions?