title: merging two files into one in c++ and display it contents
body: <p>The program creates two files emp.txt and sal.txt.the contents of these two files are finally put in empdetails.txt.everything is working fine,but the display function:- void display() is displaying the last record TWICE.why the last record is displayed twice???</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;conio.h&gt;
  #include&lt;fstream&gt;
  using namespace std;
  
  class emp
  {
        int num,age;
        char name[20],dep[5];
  
        public:
            void getdata()
            {
               cout&lt;&lt;"\n\n  Name   = ";
               cin&gt;&gt;name;
               cout&lt;&lt;"\n Emp Num   = ";
               cin&gt;&gt;num;
               cout&lt;&lt;"\n Department= ";
               cin&gt;&gt;dep;
               cout&lt;&lt;"\n Age       = ";
               cin&gt;&gt;age;
            }
            void display1()
            {  
              cout&lt;&lt;"\n"&lt;&lt;name&lt;&lt;"\t"&lt;&lt;num&lt;&lt;"\t"&lt;&lt;dep&lt;&lt;"\t\t"&lt;&lt;age;
            }  
  
  };
  
  class sal
  {
        float gs,ns;
         public:
             void getsal()
             {
               cout&lt;&lt;"\n Gross sal = ";
               cin&gt;&gt;gs;
               cout&lt;&lt;"\n Net sal   = ";
               cin&gt;&gt;ns;
             }
             void display2()
             {
               cout&lt;&lt;"\t"&lt;&lt;gs&lt;&lt;"\t"&lt;&lt;ns;
             }          
  };
  
  void display()
  {
     emp e;sal s;
     ifstream fil1;
  
     fil1.open("empdetails.txt",ios::in);
  
     cout&lt;&lt;"\n\n Name \t Emp Num \t Dep \t Age \t Gross Sal \t Net Sal \n";  
  
    while(!fil1.eof())
    {
      fil1.read((char*)&amp;e,sizeof(e));
      e.display1();
  
      fil1.read((char*)&amp;s,sizeof(s));
      s.display2();
    }   
  }
  
  int main()
  {
      int n;
      emp e1;sal s1;
      ofstream fil1,fil2,fil3;
  
      fil1.open("emp.txt",ios::out);
      fil2.open("sal.txt",ios::out);
      fil3.open("empdetails.txt",ios::out);
  
      cout&lt;&lt;"\n How many employee details do you want to enter = ";
      cin&gt;&gt;n;
  
      cout&lt;&lt;"\n Enter the deatils one by one \n";
      for(int i=0;i&lt;n;i++)
      {
          e1.getdata();
          fil1.write((char*)&amp;e1,sizeof(e1));
  
          s1.getsal();
          fil2.write((char*)&amp;s1,sizeof(s1));
  
          fil3.write((char*)&amp;e1,sizeof(e1));
          fil3.write((char*)&amp;s1,sizeof(s1));
      }
      fil1.close();
      fil2.close();
      fil3.close();
  
      cout&lt;&lt;"\n\n\t\t Merged file contents \n\n\t\t";
      display();
      getch();
      return 0;
  } 
  </code></pre>