#include<iostream>
#include<conio.h>
#include<fstream>
#include<process.h>
using namespace std;

fstream file("emp.txt",ios::in|ios::binary);
//int no;

struct Employee
{
       int id;
       char name[30], address[60];
//       string id, name, address;
};

Employee Emp1;

void ReadData()
{
   fstream file("emp.txt",ios::in|ios::binary);

   file.read((char*)&Emp1, sizeof(Emp1));
   while(!file.eof())
   {
      file.read((char*)&Emp1, sizeof(Emp1));
   }

}

void WriteData()
{
   fstream file("emp.txt",ios::out|ios::app|ios::binary);
   file.write((char*)&Emp1, sizeof(Emp1));

}

bool CheckFile()
{
     bool Ok = false;
   
   if(!file)
     Ok = false;
   else
     Ok = true;
   
   return Ok;
}

void DisplayEmp()
{  
   fstream file("emp.txt",ios::in|ios::binary);
   cout<<"\nID#\t\tName\t\tAddress";
   cout<<"\n---\t\t----\t\t-------";
   file.read((char*)&Emp1, sizeof(Emp1));
   while(!file.eof() and Emp1.id!=0)
   {
     cout<<"\n"<<Emp1.id<<"\t\t"<<Emp1.name<<"\t\t"<<Emp1.address;
     file.read((char*)&Emp1, sizeof(Emp1));
   }
   
   cout<<"\n\nPress any key to continue ... ";
     getch();
}

void AddEmp()
{
   cout<<"\nEmployee ID# : ";
     cin>>Emp1.id;
   cout<<"\nEmployee Name : ";
     cin>>Emp1.name;
   cout<<"\nEmployee Address : ";
     cin>>Emp1.address; 
   WriteData();   
}

void UpdateEmp(int nu)
{
  
}

void SearchEmp(int nu)
{
//     int no;
     bool found=false;
     
     fstream file("emp.txt",ios::in);
     file.read((char*)&Emp1, sizeof(Emp1));
     while(!file.eof())
     {
        if(nu == Emp1.id)
        {
          found = true;
          cout<<"\nID#\t\tName\t\tAddress";
          cout<<"\n---\t\t----\t\t-------";
          cout<<"\n"<<Emp1.id<<"\t\t"<<Emp1.name<<"\t\t"<<Emp1.address;
          break;     
        }
        else
          file.read((char*)&Emp1, sizeof(Emp1));
     }      

     if(found == false)
       cout<<"\nEmployee does not found ...";
     getch();
}

void DelEmp(int nu)
{
/* Wrong Code
     //SearchEmp(nu);
     bool found=false;
     
     fstream file("emp.txt",ios::in);
     file.read((char*)&Emp1, sizeof(Emp1));
     while(!file.eof())
     {
        if(nu == Emp1.id)
        {
          found = true;
          cout<<"\nID#\t\tName\t\tAddress";
          cout<<"\n---\t\t----\t\t-------";
          cout<<"\n"<<Emp1.id<<"\t\t"<<Emp1.name<<"\t\t"<<Emp1.address;
           
//          Emp1.id=0;
//            Emp1.name='';
//              Emp1.address='';
          break;     
        }
        else
          file.read((char*)&Emp1, sizeof(Emp1));
     }      

     if(found == false)
       cout<<"\nEmployee does not found ...";
     getch();
*/     
}

int main()
{
    int sel, no;

    ReadData();
    if(!CheckFile())
    {
      cout<<"\n\n\n\n\n\n\n\n\n\t\t\tFile does not found !!!";
      goto Exit;
    }
    else
    do{
        system("cls");
       cout<<"\n\n\n\n\t\t\t==========================";
       cout<<"\n\t\t\t= 1. Add Employee        =";
       cout<<"\n\t\t\t= 2. Update Employee     =";
       cout<<"\n\t\t\t= 3. Delete Employee     =";
       cout<<"\n\t\t\t= 4. Search for Employee =";
       cout<<"\n\t\t\t= 5. Display Employee    =";
       cout<<"\n\t\t\t= 0. Exit                =";
       cout<<"\n\t\t\t==========================";
       cout<<"\n\n\t\t\tEnter your Choice : ";
         cin>>sel;
       switch(sel)
       {
          case 1:
               AddEmp();
               break;
          case 2:
               cout<<"\nEnter the Employee ID# : ";
                 cin>>no;
               UpdateEmp(no);
               break;
          case 3:
               cout<<"\nEnter the Employee ID# : ";
                 cin>>no;
               DelEmp(no);
               break;
          case 4:
               cout<<"\nEnter the Employee ID# : ";
                 cin>>no;
               SearchEmp(no);
               break;
          case 5:
               DisplayEmp();
               break;
          case 0:
               exit(0);
               break;
          default:
                  system("cls");
       }
    }while(sel != 0);
    
    Exit:
    getch();
    return 0;
}