Create a class Employee with name, Id and salary as data members. Provide appropriate
constructors, set, get and display methods in the class.
In the main program, use a do-while loop to enter data for employees as long as the user
desires and save all data to a file.
Once the user is done with data entry, read the data for employees from the file and
display the information of each employee.
// Lab_13_A.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<conio.h>
#include<string>
#include<fstream>
using namespace std;
class emp
{
private:
int id,sal;
string name;
public:
emp (int i=0,int sa=0,string s=""){id=i;sal=sa;name=s;}
void set(int i=0,int sa=0,string s=""){id=i;sal=sa;name=s;}
string getname(){return name;}
int getid(){return id;}
int getsal(){return sal;}
void display()
{
cout<<"\nName: "<<name<<endl;
cout<<"\nID: "<<id<<endl;
cout<<"\nSalary: "<<sal<<endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
int i,s;
string n;
char ch;
ofstream inf("abc.txt");
emp e;
do
{
cout<<"Enter Name: ";
cin>>n;
cout<<"Enter ID: ";
cin>>i;
cout<<"Enter Salary: ";
cin>>s;
e.set(i,s,n);
inf.write(reinterpret_cast<char *>(&e),sizeof(e));
inf.close();
cout<<"\nWant to add more: ";
cin>>ch;
}while(ch!='n');
ifstream outf("abc.txt");
while(!outf.eof())
{
outf.read(reinterpret_cast<char *>(&e),sizeof(e));
e.display();
}
outf.close();
getch();
}
The output is wrong.
When I Input more than one record, It shows just the first record in output.
Secondly It shows the same record 2 times, in the output.
Please Help people.
I have ( OOP/C++ ) semester exam in a week and I am very weak in file handling.