Note: Pl. check the path of the notepad file:
string FILE_NAME="H:\\IncomeRec.txt";
Contents of the file:
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
//Working Code:
//
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
class Employee{
private:
//Employee Lastname (initial) - char
char lastname;
//FNPF# - integer
int FNPF;
//Age - integer
int age;
//Gross_Income - integer
int gross_Income;
//Resident - char
char resident;
public:
Employee(){}
Employee(char lastname,int FNPF,int age,int gross_Income,char resident){
this->lastname=lastname;
this->FNPF=FNPF;
this->age=age;
this->gross_Income=gross_Income;
this->resident=resident;
}
char getLastname(){
return this->lastname;
}
int getFNPF(){
return this->FNPF;
}
int getAge(){
return this->age;
}
int getGrossIncome(){
return this->gross_Income;
}
char getResident(){
return this->resident;
}
void displayInfo(){
cout<<this->lastname<<"\t\t"<<this->FNPF<<"\t\t"<<this->age<<"\t\t"<<this->gross_Income<<"\t\t\t"<<this->resident<<"\n";
}
};
int main (){
Employee employees[200];
int totalEmployees=0;
//The file name
string FILE_NAME="H:\\IncomeRec.txt";
//Open the file
fstream IncomeRecFile;
IncomeRecFile.open(FILE_NAME.c_str());
if(IncomeRecFile)
{
//Employee Lastname (initial) - char
char lastname;
//FNPF# - integer
int FNPF;
//Age - integer
int age;
//Gross_Income - integer
int gross_Income;
//Resident - char
char resident;
string line;
//read the first line of the file
getline(IncomeRecFile,line);
while (!IncomeRecFile.eof()){
IncomeRecFile>>lastname>>FNPF>>age>>gross_Income>>resident;
employees[totalEmployees]=Employee(lastname,FNPF,age,gross_Income, resident);
totalEmployees++;
}
}else{
cout<<"\nThe file does not exist.\n\n";
}
if(totalEmployees>0){
cout<<"Last N\t\tFNPF\t\tAge\t\tGross Income\t\tResident\n";
for(int i=0;i<totalEmployees;i++){
employees[i].displayInfo();
}
}
//close files stream
IncomeRecFile.close();
system("pause");
return 0;
}
Comments
Leave a comment