Here's the code and the problem is described below
Code:
#include <fstream.h>
#include <string.h>
#include <iomanip.h>
class emp
{
char eno[4];
char ename[30];
float salary;
fstream f1;
public:
emp()
{
strcpy(eno,NULL);
strcpy(ename,NULL);
salary=0;
f1.open("d:\\exam.txt",ios::in | ios::out |ios::ate);
}
friend istream & operator>>(istream &din,emp &a);
friend ostream & operator<<(ostream &dout,emp &a);
};
ostream &operator<<(ostream &dout,emp &a)
{
a.f1.seekg(0,ios::beg);
cout<<"\n\n==================================================================";
cout<<"\n\nEmployee File Contains";
cout<<"\n\n==================================================================\n";
cout<<"Employee No\t\tEmployee Name\t\tSalary";
cout<<"\n\n==================================================================\n";
while(a.f1>>a.eno>>a.ename>>a.salary)
cout<<a.eno<<"\t\t\t"<<a.ename<<"\t\t\t"<<a.salary<<endl;
a.f1.clear();
return dout;
}
istream &operator>>(istream &din,emp &a)
{
a.f1.seekp(0,ios::end);
a.f1.seekg(0,ios::beg);
char eno[4];
int t=0;
cout<<"\n\nEnter Employee Number :";
din>>a.eno;
while(a.f1>>eno)
{
if(strcmpi(eno,a.eno)==0)
{
cout<<"\n\nDuplicate Record";
t=1;
break;
}
}
if(t==0)
{
cout<<"\n\nEnter Employee Name :";
din>>a.ename;
cout<<"\n\nEnter Salary :";
din>>a.salary;
a.f1<<a.eno<<" "<<a.ename<<" "<<a.salary<<"\n";
a.f1.clear();
}
return din;
}
int main(void)
{
emp e;
int ch;
do
{
cout<<"\n\nEmployee Management System";
cout<<"\n\n1 :Add Record";
cout<<"\n\n2 :Edit Record";
cout<<"\n\n3 :Delete Record";
cout<<"\n\n4 :Display Records";
cout<<"\n\n5 :Exit";
cout<<"\n\nEnter Your Choice :";
cin>>ch;
switch(ch)
{
case 1:
cin>>e;
break;
case 2:
break;
case 3:
break;
case 4:
cout<<e;
break;
case 5:
cout<<"\n\nBye-Bye";
break;
default:
cout<<"\n\nWrong Choice";
}
}while(ch!=5);
return 0;
}
kindly see the overloaded operator >>. I've tried to debug it but i can't trace the error. For some reason the input stream gets corrupted. Kindly tell me what's the problem behind that. The loop that's for duplicate record checking, that is creating problem. Please explain me why this problem is coming?