Sei sulla pagina 1di 3

#include<conio.

h>
#include<fstream.h>
#include<stdlib.h>
#include<string.h>
#include<iomanip.h>
#include<stdio.h>
class hospital
{char name[20];
int patId;
int patPhn;
char bloodGroup[4];
public:
void getdata();
void display();
void modify(hospital);
int getid(){return patId;}
};
void hospital::getdata()
{cout<<"\nEnter Name: ";cin>>name;
cout<<"\nEnter Patient ID: ";cin>>patId;
cout<<"\nEnter Phone numebr: ";cin>>patPhn;
cout<<"\nEnter Blood Group: ";cin>>bloodGroup;
};
void hospital::display()
{cout<<"\nName: "<<name;
cout<<"\nPatient ID: "<<patId;
cout<<"\nPhone Number: "<<patPhn;
cout<<"\nBlood Group: "<<bloodGroup<<"\n\n\n";
};
void hospital::modify(hospital h1)
{h1.display();
int id,ph;char bg[4],nm[20];
cout<<"\nEnter New details: \n";
cout<<"\n New Name(Enter '.' to retain): ";cin>>nm;
cout<<"\n New Phone Number(Enter '-1' to retain): ";cin>>ph;
cout<<"\n New Patient ID(Enter '-1' to retain): ";cin>>id;
cout<<"\n New Blood Group(Enter '.' to retain): ";cin>>bg;
if (strcmp(nm,".")!=0)
strcpy(name,nm);
if (strcmp(bg,".")!=0)
strcpy(bloodGroup,bg);
if (id!=-1)
patId=id;
if (ph!=-1)
patPhn=ph;
};
void main()
{int choice;
char ch1;
do{
cout<<"\n1-Enter data";
cout<<"\n2-Search";
cout<<"\n3-Show all";
cout<<"\n4-Modify";

cout<<"\n5-Delete";
cout<<"\nEnter your choice: ";
cin>>choice;
switch(choice)
{case 1:{ofstream fout; hospital h;char ch;
fout.open("hospital.dat", ios::app,ios::binary);
do{
cout<<"\nEnter Details: \n";
h.getdata();
fout.write((char*)&h,sizeof(h));
cout<<"\nDo you want to enter more? \n";
cin>>ch;
}while((ch=='y')||ch=='Y');
fout.close();
break;
}
case 2:{ifstream fin;hospital h;int pat_id;
cout<<"\nEnter Patient Id to be searched: \n";cin>>pat_id;
fin.open("hospital.dat",ios::in,ios::binary);
fin.seekg(0);
while(!fin.eof())
{fin.read((char*)&h,sizeof(h));
if(h.getid()==pat_id)
{if(fin.eof()) break;
h.display();}
}
fin.close();
break;
}
case 3:{ifstream fin;hospital h;
fin.open("hospital.dat",ios::binary|ios::out|ios::in);
while(!fin.eof())
{fin.read((char*)&h,sizeof(h));
if (fin.eof()) break;
h.display();
}
fin.close();
break;
}
case 4:{ hospital h1;
fstream fio("hospital.dat",ios::in|ios::binary|ios::out);
int pid;long pos;char found='f' ;
cout<<"\nEnter the Patient id: \n";
cin>>pid;
while(1)
{pos=fio.tellg();
fio.read((char*)&h1,sizeof(h1));
if(h1.getid()==pid)
{h1.modify(h1);
if(fio.eof()) break;
fio.seekg(pos);
fio.write((char*)&h1,sizeof(h1));
found='t';
break;

}
}
if(found=='f')
{cout<<"\nRecord Not found!";}
fio.seekg(0);
cout<<"\nNew File Contains\n";
while(1)
{fio.read((char*)&h1,sizeof(h1));
if (fio.eof())break;
h1.display();
}
fio.close();
break;
}
case 5:{hospital h1;
ifstream fin("hospital.dat",ios::in);
ofstream fout("temp.dat",ios::out);
int ID;char found='f',confirm='n';
cout<<"\nEnter the ID of the Patient you want to delete: ";
cin>>ID;
while(!fin.eof())
{fin.read((char*)&h1,sizeof(h1));
if(h1.getid()==ID)
{h1.display();
found='t';
cout<<"\nAre you sure you want to Delte this entry? ";
cin>>confirm;
if (confirm=='n')
fout.write((char*)&h1,sizeof(h1));
}
else fout.write((char*)&h1,sizeof(h1));
}
if (found='f')
cout<<"\nRecord Notfound!\n";
fin.close();
fout.close();
remove("hospital.dat");
rename("temp.dat","hospital.dat");
fin.open("hospital.dat");
cout<<"\n Now the file contains";
while(!fin.eof())
{fin.read((char*)&h1,sizeof(h1));
if (fin.eof())break;
h1.display();
}
fin.close();
}
}
cout<<"\nDo you want to continue? ";
cin>>ch1;
}while((ch1=='y')||(ch1=='Y'));
}

Potrebbero piacerti anche