The input file called IncomeRec.txt contains a list of Annual (Gross) income records of employees in a firm. You will assume that this firm contains at most 200 active staff. Each record is on a single line and the fields are separated by a single space. The names of the fields are: • Employee Lastname (initial) - char • FNPF# - integer • Age - integer • Gross_Income - integer • Resident - char An example record may have the following form: F 12345 24 40000 Y And the format of the actual file is shown below. (Note: This doesn’t show all records and for clarity, fields here are shown separated by tabs, but in the actual file they are separated by single spaces) Note: ▪ The first two lines are just header lines and can be discarded ▪ Lastname contains an initial, an alphabetical letter only; ▪ FNPF# contains a 5 digit number; ▪ Gross income contains a 3-7 digit number; ▪ Resident contains a ‘Y’ or ‘N’, representing resident or non-resident respectively
IncomeRec.txt:
Lastname(initial) FNPF# Age Gross_Incowe Resident
F 12345 24 40000 Y
B 12361 19 20000 Y
H 34763 47 100000 N
H 11224 50 35000 Y
R 54129 35 75000 N
B 10717 26 28000 Y
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
class Employee{
private:
char lastname;
int FNPF;
int age;
int gross_Income;
char resident;
public:
Employee(){}
Employee(char ln,int f,int a,int gi,char r){
lastname=ln;
FNPF=f;
age=a;
gross_Income=gi;
resident=r;
}
void display(){
cout<<lastname<<"\t\t\t"<<FNPF<<"\t\t"<<age<<"\t\t"<<gross_Income<<"\t\t\t"<<resident<<"\n";
}
};
int main (){
Employee employees[200];
int n=0;
char lastname;
int FNPF;
int age;
int gross_Income;
char resident;
string line;
string FILE_NAME="IncomeRec.txt";
ifstream ifstreamFile;
ifstreamFile.open(FILE_NAME);
if(ifstreamFile){
getline(ifstreamFile,line);
while (!ifstreamFile.eof()){
ifstreamFile>>lastname>>FNPF>>age>>gross_Income>>resident;
employees[n]=Employee(lastname,FNPF,age,gross_Income, resident);
n++;
}
}else{
cout<<"\nThe file does not exist.\n\n";
}
if(n>0){
cout<<"Last Name\t\tFNPF\t\tAge\t\tGross Income\t\tResident\n";
for(int i=0;i<n;i++){
employees[i].display();
}
}
ifstreamFile.close();
system("pause");
return 0;
}
Comments
Leave a comment