5.1 Use the provision for Rrecords in C++ to create each employee’s record. The record should have the following fields: Name, Surname, Gender, Qualification (Education Level), Position, NoOfChildren, and Salary. All these must be captured by one cin statement, the values separated by spaces, which means that each field value should not have spaces.
(4 Marks)
5.2 Every record captured must be written into a file that is open for output while information is being captured from the keyboard. Each record must use a space as the field delimiter. Therefore, each record is saved on one line on the text file. Make sure the file is only closed just before it is opened for read. Keep it open until all records have been captured. (4 Marks).
5.4 The system should be a menu driven system with the options: Capture Employee, Print Payslips and Exit. When the Capture Employee menu is chosen, the user is asked how many employees they want to capture.
#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
using namespace std;
//Function Menu
void Menu()
{
cout<<endl;
cout<<"C - Capture Employee\n"
<<"P - Print Payslips\n"
<<"E - exit";
cout<<"\nYour choice: ";
}
//Structure with data
struct Employee
{
string Name;
string Surname;
char gender;
int qual;
string Pos;
int NoOfChildren;
double salary;
};
// operator to enter object Employee
istream& operator>>(istream& is, Employee& e)
{
is>>e.Name>>e.Surname>>e.gender>>e.qual>>e.Pos
>>e.NoOfChildren>>e.salary;
return is;
}
//Function for writing data to file
void CaptEmpl()
{
Employee temp;
ofstream fout("Records.txt",ios::app);
if(!fout.is_open())
{
cout<<"File can not be open";
return;
}
int numEmp;
cout<<"How many employees do you want to capture: ";
cin>>numEmp;
for(int i=0;i<numEmp;i++)
{
//Supposed input for instance: Jack Phillips M 5 worker 1 2500
cout<<"Please enter data for "<<i+1<<" employee: ";
cin>>temp;
fout<<temp.Name<<' ';
fout<<temp.Surname<<' ';
fout<<temp.gender<<' ';
fout<<temp.qual<<' ';
fout<<temp.Pos<<' ';
fout<<temp.NoOfChildren<<' ';
fout<<temp.salary<<'\n';
}
fout.close();
}
//Function for output Payslips from file
void PrintPays()
{
Employee temp;
ifstream in("Records.txt");
if(!in.is_open())
{
cout<<"File can not be open";
return;
}
string line;
while(getline(in,line,'\n'))
{
istringstream ss;
ss.str(line);
ss>>temp.Name;
ss>>temp.Surname;
ss>>temp.gender;
ss>>temp.qual;
ss>>temp.Pos;
ss>>temp.NoOfChildren;
ss>>temp.salary;
cout<<temp.Name<<" "<<temp.Surname
<<" "<<temp.salary<<endl;
}
}
int main()
{
char choice;
do
{
Menu();
cin>>choice;
switch(choice)
{
case 'C':case 'c':
{
CaptEmpl();
break;
}
case 'P':case 'p':
{
PrintPays();
break;
}
}
}while(choice!='e'&&choice!='E');
}
Comments
Leave a comment