Help me to delete a specific record in file.

Discussion in 'C++' started by Umairajmal, Feb 5, 2011.

  1. Umairajmal

    Umairajmal New Member

    Joined:
    Dec 12, 2010
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Hazro,Pakistan
    Home Page:
    http://www.facebook.com/m.umairajmal
    Hey guys i made a program on hospital management system.
    it can insert new record and show all record.
    all of work done in filing.
    But now i want to delete a specific record from file during execution.
    But i faced some problem.
    So i want yours help.


    Here is my code.



    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<string.h>
    #include<fstream.h>
    #include<iomanip.h>
    #include<stdio.h>
    
    void welcome_screen()
         {
    	clrscr();
    	gotoxy(25,10);
    	cputs("************** W E L C O M E **************");
    	gotoxy(40,12);
    	cputs(" P R O J E C T  " );
    	gotoxy(20,14);
    	cputs(" H O S P I T A L  M A N A G E M E N T  S O F T W A R E  ");
    	gotoxy(38,16);
    	cputs(" D O N E  B Y :  ");
    	gotoxy(50,18);
    	cputs(" Muhammad Furqan Arshad ");
    	gotoxy(50,20);
    	cputs(" Aman-ul-Haq ");
    	gotoxy(50,22);
    	cputs(" Umair Ajmal ");
    	gotoxy(20,25);
    	cputs(" *** PRESS ANY KEY TO CONTINUE***");
    	getch();
    }
    ///////////////////////////////////////////////////////////////////////////
    
    struct patient
    {
    char name[15];
    char id[15];
    };
    ///////////////////////////////////////////////////////////////////////////
    
    class queue
    {
    public:
    void deletepatient();
    void searchpatient(patient);
    void outputlist();
    };
    ///////////////////////////////////////////////////////////////////////////
    
    
    void queue::deletepatient()
    {
    char n[15];
    patient p,d;
    clrscr();
    cout<<"Enter name: ";
    cin>>n;
    ifstream del;
    ofstream neew;
    del.open("hospital.txt",ios::in);
    neew.open("host",ios::app);
    while(!del.eof())
    {
    del>>p.name>>p.id;
    if(strcmp(p.name,d.name)==0)
    continue;
    else
    neew<<d.name<<d.id;
    }
    del.close();
    neew.close();
    remove("hospital.txt");
    rename("host.txt","hospital.txt");
    getch();
    }
    /////////////////////////////////////////////////////////////////////////
    void queue::searchpatient(patient p)
    {
    cout<<"Enter Patient Id: ";
    cin>>p.d;
    int f=1;
    ifstream out("hospital.txt",ios::in);
    while(!out.eof())
    {
    out>>p.name>>p.id;
    	if(strcmp(p.id,p.d)==0)
    	{
    	clrscr();
    	cout<<"Name"<<setw(10)<<"Id"<<endl;
    	cout<<"==============="<<endl;
    	cout<<p.name<<setw(10)<<p.id;
    	}}
    if(f!=1)
    cout<<"Search complete record not found..";
    getch();
    }
    ///////////////////////////////////////////////////////////////////////////
    
    void queue::outputlist()
    {
    
    patient p;
    clrscr();
    	ifstream out("hospital.txt",ios::in);
           {
    	 cout<<"NAME"<<"           "<<"ID"<<endl;
    	 cout<<"===================";
    	 while(!out.eof())
    	 {
    	 out>>p.name>>p.id;
    	 cout<<"\n"<<setiosflags(ios::left)<<setw(15)<<p.name<<setw(10)<<p.id;
    	 }}}
    /////////////////////////////////////////////////////////////////////////
    
    patient inputpatient ()
    {
       patient p;
       cout << "\nPlease enter data for new patient \nPatient Name: ";
       cin>>p.name;
          cout << "\nN.I.C. number: ";
       cin>>p.id;
       ofstream add("hospital.txt",ios::app);
       add<< p.name <<' '<<p.id <<endl;
       cout<<"Patient added.";
       return p;
    }
    /////////////////////////////////////////////////////////////////////////
    
    void heart()
    {
    int choice=0;
    patient p;
    queue *q;
    while(choice!=5)
    {
    clrscr();
    gotoxy(25,3);
    cout<<"WELCOME TO HEART DEPARTMENT.";
    cout<<"\n1. Add patient.";
    cout<<"\n2. Search Patient.";
    cout<<"\n3. List Queue.";
    cout<<"\n4. Delete patient.";
    cout<<"\n5. Exit department.";
    cin>>choice;
    switch(choice)
    {
    case 1:
    inputpatient();
    cout<<"\nPress any key to continue.......";
    getch();
    break;
    case 2:
    q->searchpatient(p);
    getch();
    break;
    case 3:
    q->outputlist();
    cout<<"\nPress any key to continue........";
    getch();
    break;
    case 4:
    q->deletepatient();
    break;
    }
    }
    }
    ///////////////////////////////////////////////////////////////////////////
    
    void kidney()
    {
    int choice=0;
    patient p;
    queue *q;
    while(choice!=5)
    {
    clrscr();
    gotoxy(25,3);
    cout<<"WELCOME TO KIDNEY DEPARTMENT.";
    cout<<"\n1. Add patient.";
    cout<<"\n2. Search patient.";
    cout<<"\n3. List Queue.";
    cout<<"\n4. Delete patient.";
    cout<<"\n5. Exit department.";
    cin>>choice;
    switch(choice)
    {
    case 1:
    inputpatient();
    cout<<"\nPress any key to continue........";
    getch();
    break;
    case 2:
    q->searchpatient(p);
    getch();
    break;
    case 3:
    q->outputlist();
    cout<<"\nPress any key to continue........";
    getch();
    break;
    
    case 4:
    q->deletepatient();
    break;
    }
    }
    getch();
    }
    //////////////////////////////////////////////////////////////////////////
    
    void skin()
    {
    int choice=0;
    patient p;
    queue *q;
    while(choice!=5)
    {
    clrscr();
    gotoxy(25,3);
    cout<<"WELCOME TO SKIN DEPARTMENT.";
    cout<<"\n1. Add patient.";
    cout<<"\n2. Search patient.";
    cout<<"\n3. List Queue.";
    cout<<"\n4. Delete patient.";
    cout<<"\n5. Exit department.";
    cin>>choice;
    switch(choice)
    {
    case 1:
    inputpatient();
    cout<<"\nPress any key to continue.......";
    getch();
    break;
    case 2:
    q->searchpatient(p);
    getch();
    break;
    case 3:
    q->outputlist();
    cout<<"\nPress any key to continue........";
    getch();
    break;
    
    case 4:
    q->deletepatient();
    break;
    }
    }
    getch();
    }
    ///////////////////////////////////////////////////////////////////////////
    
    void main()
    {
    textbackground(YELLOW);
    textcolor(BLACK);
    welcome_screen();
    int a=0;
    while(a!=4)
    {
    clrscr();
    cout<<"\n1. Heart Department.";
    cout<<"\n2. Kidney Department.";
    cout<<"\n3. Skin Department.";
    cout<<"\n4. Exit.";
    switch(a)
    {
    case 1:
    heart();
    break;
    
    case 2:
    kidney();
    break;
    
    case 3:
    skin();
    break;
    }
    cin>>a;
    }
    getch();
    }
     
    Last edited by a moderator: Feb 5, 2011
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Read the link in my sig regarding accurate problem descriptions.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice