C++ File Handling 19

Example:2
C++ Program to maintain Employee details like
employee number,
employee name, and
employee salary.
(Menu Driven program)
Sol:

This program is menu driven and deals following operations,

1. Adding the record
2. Displaying all the records
3. Search by empno
4. Search by name
5. Del by empno
6. Del by name
7. Modify by empno
8. Random display of record
0. Exit

#include<iostream>
#include<fstream>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<iomanip>
#include<windows.h> //  header file for gotoxy
COORD coord={0,0}; // this is global variable
//center of axis is set to the top left cornor of the screen
using namespace std;
void gotoxy(int x,int y)
{
	coord.X=x;
 	coord.Y=y;
 	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
 }
void pass()
{
	char n[20],ch;
	int i=0,j;
	gotoxy(1,10);
	printf("Enter the password   : ");
	gotoxy(30,10);
	j=30;
	while((ch=getch())!='\r')
	{
		gotoxy(j,10);
		printf("*");
		j++;
		n[i]=ch;
		i++;
	}
	n[i]='\0';
	if (strcmp(n,"hello")==0)
	printf("\n\n\nthe password is correct\n");
	else
	{
		printf("\n\nInvalid password , try again\n");
		getch();
		exit(1);  //header file : stdlib.h
	}
}
class emp
{
private:
	int empno;
	char name[20];
	float sal;
public:
	void read();
	void show();
	void add();
	void disp_all();
	void menu();
	void search_empno();
	void search_empname();
	void del_empno();
	void del_empname();
	void modi_empno();
	void rand_disp();
	void heading();
	void line1();
	int  check_empno(int e);
	int check_sal(float s);

};
int emp::check_empno(int e)
{
  if(e<=0)
  return(0);
  else
  return(1);
}
int emp::check_sal(float s)
{
  if(s<=0)
  return(0);
  else
  return(1);
}
void emp::read()
{
  int z=0;
  while(1)
  {
    cout<<"Enter empno ";
    cin>>empno;
    z=check_empno(empno);
    if(z==0)
    cout<<"invalid empno, enter again"<<endl;
    else
    break;
  }
  cout<<"Enter name ";
  cin>>name;
  while(1)
  {
    cout<<"Enter sal ";
    cin>>sal;
    z=check_sal(sal);
    if(z==0)
    cout<<"Invalid salary , enter again"<<endl;
    else
    break;
  }
}
void emp::show()
{
  cout<<setw(10)<<empno<<setw(20)<<name<<setw(20)<<sal<<endl;
}
void emp::add()
{
  fstream abc;
  emp e;

  abc.open("emp1",ios::in);
  if(abc.fail())
  {
    cout<<"file does not exist"<<endl;
    abc.open("emp1",ios::out);
  }
  else
  {
    cout<<"File is present"<<endl;
    abc.close();
    abc.open("emp1",ios::app);
  }
  e.read();
  abc.write((char*)&e,sizeof(e));
  abc.close();
}
//line1
void emp::line1()
{
 int i;
 for(i=1;i<=60;i++)
 {
   cout<<"-";
 }
 cout<<endl;
}

//heading
void emp::heading()
{
	system("color 94");
	line1();
	cout<<"    E m p l o y e e  M a n a g e m e n t  S y s t e m"<<endl;
	cout<<"    ================================================="<<endl;
	line1();
	cout<<setw(10)<<"EMP NO"<<setw(20)<<"NAME"<<setw(20)<<"SALARY"<<endl;
	line1();
}

void emp::disp_all()
{
  fstream abc;
  emp e;
  abc.open("emp1",ios::in);
  if(abc.fail())
  {
    cout<<"unable to open the file"<<endl;
    return;
  }
  heading();
  abc.read((char*)&e,sizeof(e));
  while(!abc.eof())
  {
    e.show();
    abc.read((char*)&e,sizeof(e));
  }
  line1();
  abc.close();
}

void emp::search_empno()
{
  fstream abc;
  emp e;
  int te,z=0;
  cout<<"Enter empno to search ";
  cin>>te;
  abc.open("emp1",ios::in);
  if(abc.fail())
  {
    cout<<"unable to open the file"<<endl;
    return;
  }
  abc.read((char*)&e,sizeof(e));
  heading();
  while(!abc.eof())
  {
    if(te==e.empno)
    {
    z=1;
    e.show();
    //break;
    }
    abc.read((char*)&e,sizeof(e));
  }
  line1();
  abc.close();
  if(z==0)
  cout<<"rec is not present"<<endl;
}
void emp::search_empname()
{
  fstream abc;
  emp e;
  int z=0;
  char tn[20];
  cout<<"Enter empname to search ";
  cin>>tn;
  abc.open("emp1",ios::in);
  if(abc.fail())
  {
    cout<<"unable to open the file"<<endl;
    return;
  }
  abc.read((char*)&e,sizeof(e));
  heading();
  while(!abc.eof())
  {
    if(strcmpi(tn,e.name)==0)
    {
    z=1;
    e.show();
    }
    abc.read((char*)&e,sizeof(e));
  }
  line1();
  abc.close();
  if(z==0)
  cout<<"rec is not present"<<endl;
}
void emp::del_empno()
{
  fstream abc,pqr;
  emp e;
  int te,z=0;
  cout<<"Enter empno to delete ";
  cin>>te;
  abc.open("emp1",ios::in);
  pqr.open("temp",ios::out);
  if(abc.fail())
  {
    cout<<"unable to open the file"<<endl;
    return;
  }
  abc.read((char*)&e,sizeof(e));
  heading();
  while(!abc.eof())
  {
    if(te==e.empno)
    {
    z=1;
    e.show();
    }
    else
    pqr.write((char*)&e,sizeof(e));

    abc.read((char*)&e,sizeof(e));
  }
  line1();
  abc.close();
  if(z==0)
  cout<<"rec is not present"<<endl;
  else
  {
    remove("emp1");
    rename("temp","emp1");
  }
}

void emp::del_empname()
{
  fstream abc,pqr;
  emp e;
  int z=0;
  char tn[20];
  cout<<"Enter empname to delete ";
  cin>>tn;
  abc.open("emp1",ios::in);
  pqr.open("temp",ios::out);
  if(abc.fail())
  {
    cout<<"unable to open the file"<<endl;
    return;
  }
  abc.read((char*)&e,sizeof(e));
  heading();
  while(!abc.eof())
  {
    if(strcmpi(tn,e.name)==0)
    {
    z=1;
    e.show();
    }
    else
    pqr.write((char*)&e,sizeof(e));

    abc.read((char*)&e,sizeof(e));
  }
  line1();
  abc.close();
  if(z==0)
  cout<<"rec is not present"<<endl;
  else
  {
    remove("emp1");
    rename("temp","emp1");
  }
}
void emp::modi_empno()
{
  fstream abc,pqr;
  emp e;
  int te,z=0;
  cout<<"Enter empno to modify ";
  cin>>te;
  abc.open("emp1",ios::in);
  pqr.open("temp",ios::out);
  if(abc.fail())
  {
    cout<<"unable to open the file"<<endl;
    return;
  }
  abc.read((char*)&e,sizeof(e));
  heading();
  while(!abc.eof())
  {
    if(te==e.empno)
    {
    z=1;
    cout<<"Old details"<<endl;
    e.show();
    cout<<"Enter new details "<<endl;
    e.read();
    }
    pqr.write((char*)&e,sizeof(e));

    abc.read((char*)&e,sizeof(e));
  }
  line1();
  abc.close();
  if(z==0)
  cout<<"rec is not present"<<endl;
  else
  {
    remove("emp1");
    rename("temp","emp1");
  }
}

//******
void emp::menu()
{
  cout<<"Main Menu"<<endl;
  cout<<"1. Add record"<<endl;
  cout<<"2. Display all records "<<endl;
  cout<<"3. Search by empno"<<endl;
  cout<<"4. Search by name"<<endl;
  cout<<"5. Del by empno"<<endl;
  cout<<"6. Del by name "<<endl;
  cout<<"7. Modify by empno"<<endl;
  cout<<"8. Random display of record"<<endl;
  cout<<"0. Exit"<<endl;
  cout<<"Enter your choice ";
}
  void emp::rand_disp()
  {
  fstream abc;
  emp e;
  long reclen=0,totrec=0,filelen=0;
  int n;
  abc.open("emp1",ios::in);
  abc.seekg(0,ios::end);
  reclen=sizeof(e);
  filelen=abc.tellp();
  totrec=filelen/reclen;
  cout<<"size is "<<sizeof(e)<<endl;
  cout<<"total file length is "<<filelen<<endl;
  cout<<"total records are "<<totrec<<endl;
  cout<<"Enter the rec number to display ";
  cin>>n;
  if (n<=0 || n>totrec)
  {
  cout<<"Invalid record number ";
  getch();
  }
  else
  {

  abc.seekg(0,ios::beg);
  abc.seekg((n-1)*sizeof(e),ios::beg);
  abc.read((char*)&e,sizeof(e));
 cout<<"empo  "<<e.empno<<endl;
 cout<<"name  " <<e.name<<endl;
 cout<<"sal   "<<e.sal<<endl;
   }
  }


int main()
{
	int op=1;
	emp e1;
	pass();
	while(op!=0)
	{
	  system("cls");
	  e1.menu();
	  cin>>op;
	  system("cls");
	  switch(op)
	  {
	    case 1:
		e1.add();
		break;
	    case 2:
		e1.disp_all();
		break;
	    case 3:
		e1.search_empno();
		break;
	    case 4:
		e1.search_empname();
		break;
	    case 5:
		e1.del_empno();
		break;
	    case 6:
		e1.del_empname();
		break;
	    case 7:
		e1.modi_empno();
		break;
	    case 8:
		e1.rand_disp();
		break;
	    case 0:
		//thanks();
		//
		getch();
		exit(0);
	    default:
		cout<<"Invalid choice"<<endl;
		break;
	   }//switch
	    getch();
	 }//while
}//main