Maintaining detail of employee

Discussion in 'C++' started by Kailash, Sep 18, 2007.

Thread Status:
Not open for further replies.
  1. Kailash

    Kailash New Member

    Joined:
    Jul 24, 2007
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    :confused: 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 a moderator: Sep 19, 2007
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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.
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I have done that for you but be sure to include the code in the code blocks.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
Thread Status:
Not open for further replies.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice