Search...

Tuesday, November 18, 2014

How to create a database in C++ using file handling

#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<stdlib.h>
#include<iomanip.h>

class date
{
int dd,mm,yy;
public:
void setdate()
{
cout<<"\nEnter date(dd): ";
cin>>dd;
cout<<"\nEnter month(mm): ";
cin>>mm;
cout<<"\nEnter year(yyyy): ";
cin>>yy;
}
void dispdate()
{
cout<<"Entered Date is "<<dd<<"-"<<mm<<"-"<<yy;
}
};
class patient
{
public:
char name[30];
date doa;
char disease[30];
char add[50];
long int phn;
date dod;
void patientdata()
{
cout<<"\n\t\t\tCreate New Patient Database";
cout<<"\n\t\t--------------------------------------------";
cout<<"\nEnter Name: ";
cin>>name;
cout<<"\nEnter Date of Admission:";
doa.setdate();
cout<<"\nEnter Disease ";
cin>>disease;
cout<<"Enter Address: ";
cin>>add;
cout<<"\nEnter Contact No. ";
cin>>phn;
cout<<"\nEnter Date of Discharge:";
dod.setdate();
}
void disp()
{
cout<<"\nPatient Name: "<<name;
cout<<"\nDate of Admission of Patient is:\n";
doa.dispdate();
cout<<"\nPatient Disease: "<<disease;
cout<<"\nPatient Address: "<<add;
cout<<"\nPatient Contact No.: "<<phn;
cout<<"\nDate of Discharge of Patient is:\n";
dod.dispdate();
}
};
class age:public patient
{
public:
int ag;
void page()
{
cout<<"\nEnter Patient Age: ";
cin>>ag;
}
void display()
{
cout<<"\nPatient Age: "<<ag;
}
};
void main()
{
clrscr();
patient p;
age a;
char s;
z:
clrscr();
fstream f;
f.open("patient.txt",ios::in|ios::out|ios::app||ios::ate);
cout<<"\n---------------------------------------------------";
cout<<"\n\t\tHospital Management";
cout<<"\n---------------------------------------------------";
cout<<"\n1. Enter Patient Data\n2. Display Patient Data\n3. Search by Disease\n4. Exit";
int ch;
cout<<"\nEnter your choice ";
f.seekg(0);
cin>>ch;
char x;
switch(ch)
{
case 1:clrscr();
       fstream ft;
       ft.open("patient.txt",ios::in|ios::out|ios::ate|ios::app);
       char ans;
       a.patientdata();
       a.page();
       ft.write((char *)&a,sizeof(a));
       getch();
       cout<<"\nDo you want to continue(y/n) ";
       cin>>ans;
       if(ans=='Y'||ans=='y')
       {
       goto z;
       }
       else
       {
       break;
       }
case 2:clrscr();
       fstream fs;
       fs.open("patient.txt",ios::in|ios::out|ios::ate|ios::app);
       char answ;
       fs.seekg(0);
       int ctr=0;
       while(fs.read((char *)&a,sizeof(a)))
       {
       a.disp();
       a.display();
       if(fs.eof()==1)
       {
       break;
       }
       }
       f.close();
       cout<<"Do you want to continue(y/n) ";
       answ=getchar();
       if(answ=='Y'||answ=='y')
       goto z;
       else
       {
       exit(1);
       }
       break;
case 3:clrscr();
       cout<<"\t\t-------------------------";
       cout<<"\n\t\t\tSearch Patient";
       cout<<"\n\t\t-------------------------";
       fstream sf;
       age aobj;
       char disease[30];
       char answer;
       sf.open("patient.txt",ios::in);
       cout<<"\nEnter patient disease ";
       cin>>disease;
       do
       {
       sf.read((char *)&aobj,sizeof(aobj));
       if(sf.eof()==1)
       {
       break;
       }
       if(strcmp(aobj.disease,disease)==0)
       {
       cout<<"\nPatient Name: "<<aobj.name;
       cout<<"\nPatient Address: "<<aobj.add;
       cout<<"\nPatient Phone: "<<aobj.phn;
       cout<<"\nPatient Age: "<<aobj.ag;
       sf.close();
       }
       else
       {
       cout<<"\nPatient does not exist";
       }
       }while(sf);
       cout<<"\nDo you want to continue(y/n) ";
       answer=getchar();
       if(answer=='Y'||answer=='y')
       {
       goto z;
       }
       break;
case 4:exit(1);
}
getch();

}

No comments:

Post a Comment