Maintaining detail of employee

Kailash's Avatar, Join Date: Jul 2007
Go4Expert Member
This is code is not storing data in file can u help in correcting or finding error in this program. Plz help me.


Code:
//Create the File that contain detail of employee.
#include<fstream.h>
#include<string.h>
#include<stdio.h>
#include<conio.h>
class employee
{
private:
	 char ename[20];
	 char add[30];
	 float basic;
	 int hra,da;
	 int net_sal;
public:
	char emp_no[7];
	void getit()
		  {
		    cout<<"\nEmployee's name :";
		    gets(ename);
		    cout<<"\nAddress :";
		    gets(add);
		    cout<<"\nBasic salary :";
		    cin>>basic;
		    hra=(basic*20)/100;
		    da=(basic*10)/100;
		    net_sal=basic+da+hra;
		  }
	void showit()
		  {
		    cout<<"\nEmployee no. :"<<emp_no<<"\n";
		    cout<<"\nEmployee's name :"<<ename<<"\n";
		    cout<<"\nAddress :"<<add<<"\n";
		    cout<<"\nBasic salary :"<<basic<<"\n";
		    cout<<"\nHra :"<<hra<<"\n";
		    cout<<"\nDa :"<<da<<"\n";
		    cout<<"\nNet salary :"<<net_sal<<"\n";
		  }
	void search(employee&);
	void modify(employee&);
};
void employee::search(employee &ep)
		    {
		       clrscr();
		       int flag,ch;
		       ifstream in;
		       in.open("emp.dat",ios::in);
		       cout<<"\n******** 1: Search by name ********\n";
		       cout<<"\n******** 2: Search by empno. *******\n";
		       cout<<"\n\nEnter your choice :";
		       cin>>ch;
		       switch(ch)
			     {
			       case 1:
				       clrscr();
				       flag=0;  char nm[20];
				       cout<<"\nEnter the name to be searched :";
				       gets(nm);
				       in.read((char *)&ep,sizeof(ep));
				       while(in)
					    {
					      if(strcmp(ename,nm)==0)
						{
						  ep.showit();
						  flag=1;
						}
					      in.read((char *)&ep,sizeof(ep));
					    }
				       if(flag==0)
					 {
					    cout<<"\nRecord not found \n";
					 }
				       getch();
				       break;

				 case 2:
					  clrscr();
					  flag=0;  char num[7];
					  cout<<"\nEnter the empno. to be searched :";
					  cin>>num;
					  in.read((char *)&ep,sizeof(ep));
					  while(in)
					       {
						 if(strcmp(emp_no,num)==0)
						   {
						     ep.showit();
						     flag=1;
						   }
						 in.read((char *)&ep,sizeof(ep));
					       }
					  if(flag==0)
					    {
					      cout<<"\nRecord not found \n";
					    }
					  getch();
					  break;

				 default :
					   cout<<"\nYou have entered wrong choice \n";
			       }
		    }
void employee::modify(employee &ep)
		    {
		       clrscr();
		       int flag,n,s,ch; char num[7];
		       fstream fl;
		       fl.open("emp.dat",ios::in|ios::out);
		       cout<<"\n******** 1: Update record of specific name **********\n";
		       cout<<"\n******** 2: Update record after specific name *******\n";
		       cout<<"\n\nEnter your choice :";
		       cin>>ch;
		       switch(ch)
			     {
			       case 1:
					 clrscr();
					 flag=0;
					 cout<<"\nEnter the name to be searched :";
					 cin>>num;
					 fl.read((char *)&ep,sizeof(ep));
					 while(fl)
					      {
						if(strcmp(emp_no,num)==0)
						  {
						     n=fl.tellg();
						     flag=1;
						     break;
						  }
						fl.read((char *)&ep,sizeof(ep));
					      }
					 s=sizeof(ep);
					 if(flag==1)
					   {
					     fl.seekp(n-s);
					     ep.getit();
					     fl.write((char *)&ep,sizeof(ep));
					   }
					 if(flag==0)
					   {
					     cout<<"\nRecord not found \n";
					   }
					 break;

			       case 2:
					 clrscr();
					 flag=0;
					 cout<<"\nEnter the name to be searched :";
					 cin>>num;
					 fl.read((char *)&ep,sizeof(ep));
					 while(fl)
					      {
						if(strcmp(emp_no,num)==0)
						  {
						     n=fl.tellg();
						     flag=1;
						     break;
						  }
						fl.read((char *)&ep,sizeof(ep));
					      }
					 if(flag==1)
					   {
					     fl.seekp(n);
					     ep.getit();
					     fl.write((char *)&ep,sizeof(ep));
					   }
					 if(flag==0)
					   {
					     cout<<"\nRecord not found \n";
					   }
					 break;

			       default:
					  cout<<"\nYou have entered wrong choice \n";
			     }
		    }
void main()
{
employee emp;
fstream file;
char ch;  int n;
file.open("emp.dat",ios::in|ios::out|ios::app);
do
  {
   clrscr();
   cout<<"\n******************* Select the following ***********************\n";
   cout<<"\n******************* 1: Add record in file **********************\n";
   cout<<"\n***************** 2: Search record in file *********************\n";
   cout<<"\n***************** 3: Update record in file *********************\n";
   cout<<"\n***************** 4: Display record in file ********************\n";
   cout<<"\nEnter your choice :";
   cin>>n;
   switch(n)
	 {
	   case 1:
		    clrscr();
		    char eno[7];
	       x:   cout<<"\nEmployee number :";
		    cin>>eno;
		    file.read((char *)&emp,sizeof(emp));
		    while(file)
			 {
			    if(strcmp(emp.emp_no,eno)==0)
			      {
				cout<<"\nEmployee number already exist \n";
				goto x;
			      }
			   file.read((char *)&emp,sizeof(emp));
			 }
		    strcpy(emp.emp_no,eno);
		   emp.getit();
		   file.write((char *)&emp,sizeof(emp));
		   break;

	   case 2:
		   emp.search(emp);
		   break;

	   case 3:
		   emp.modify(emp);
		   break;

	   case 4:
		   clrscr();
		   file.seekg(0);
		   file.read((char *)&emp,sizeof(emp));
		   while(file)
			{
			  emp.showit();
			  file.read((char *)&emp,sizeof(emp));
			}
		   getch();
		   break;

	   default:
		     cout<<"\nYou have entered wrong choice \n";
	 }
   cout<<"\n\nDo you want to continue (y/n) :";
   cin>>ch;
  }while(ch!='n'&&ch!='N');
getch();
}

Last edited by shabbir; 19Sep2007 at 09:06.. Reason: Code block
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Please put your code in tags (see the "Before you make a query" thread). No one wants to read that unformatted stuff to look for your problem. I could do it for you, but I suspect you're a big boy and can handle it.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
I have done that for you but be sure to include the code in the code blocks.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Just realized you have the thread twice. Duplicate of Maintaining detail of employee. Thread closed.