Expand|Select|Wrap|Line Numbers
- #include<iostream.h>
- #include<conio.h>
- #include<fstream.h>
- #include<string.h>
- #include<stdio.h>
- #include<process.h>
- fstream file;
- fstream file1;
- class donor
- { int num;
- char name[20];
- char dob[9];
- char address[30];
- int phone_no;
- char blood_gr;
- public:
- void accept();
- void display();
- void query();
- void modify();
- void del();
- char return_group()
- {return blood_gr;}
- int return_num()
- {return num;}
- }s1;
- void donor::accept()
- {
- file.open("Don.dat",ios::app|ios::in|ios::out|ios::binary);
- clrscr();
- cout<<"\t\t\tDONOR DETAILS";
- cout<<"\nEnter Donor no: ";
- cin>>num;
- cout<<"\nEnter Name: ";
- gets(name);
- cout<<"\nEnter Address : ";
- gets(address);
- cout<<"\nEnter Date of Birth : ";
- gets(dob);
- cout<<"\nEnter Phone Number : ";
- cin>>phone_no;
- cout<<"\nEnter Blood Group : ";
- cin>>blood_gr;
- file.write((char *)&s1,sizeof(s1));
- getch();
- file.close();
- };
- void donor::display()
- {
- file.open("Don.dat",ios::app|ios::in|ios::out|ios::binary|ios::beg);
- while(!file.eof())
- {file.read((char *)&s1,sizeof(s1));
- clrscr();
- cout<<"\t\t\tDONOR DETAILS";
- cout<<"\n\nDonor no : ";
- cout<<num;
- cout<<"\n\nNAME : ";
- puts(name);
- cout<<"\n\nAddress : ";
- puts(address);
- cout<<"\n\nDate Of Birth : ";
- puts(dob);
- cout<<"\n\nPhone Number : ";
- cout<<phone_no;
- cout<<"\n\n\nBlood Group : ";
- cout<<blood_gr;
- getch();
- }
- file.close();
- };
- void donor::modify()
- {
- file.open("Don.dat",ios::app|ios::in|ios::out|ios::beg|ios::binary);
- int recno;
- long pos;
- char nam[20];
- char dob1[9];
- char address1[30];
- int phone_no1;
- char blood_gr1;
- cout<<"\nEnter the record number you want to modify : ";
- cin>>recno;
- while(!file.eof())
- {pos=file.tellg();
- file.read((char *)&s1,sizeof(s1));
- if(s1.return_num()==recno)
- {
- cout<<"\nEnter Name: ";
- gets(nam);
- cout<<"\nEnter Address : ";
- gets(address1);
- cout<<"\nEnter Date of Birth : ";
- gets(dob1);
- cout<<"\nEnter Phone Number : ";
- cin>>phone_no1;
- cout<<"\nEnter Blood Group : ";
- cin>>blood_gr1;
- name[20]=nam[20];
- address[30]=address1[30];
- dob[9]=dob1[9];
- phone_no=phone_no1;
- blood_gr=blood_gr1;
- file.seekg(pos);
- file.write((char *)&s1,sizeof(s1));
- break;
- }
- }
- getch();
- file.close();
- }
- void donor::query()
- {
- file.open("Don.dat",ios::app|ios::in|ios::out|ios::beg|ios::binary);
- char bldgr;
- cout<<"Enter the blood group you want to query\n ";
- cin>>bldgr;
- while(!file.eof())
- {
- file.read((char *)&s1,sizeof(s1));
- if(s1.return_group()==bldgr)
- {
- s1.display();
- getch();
- break;
- }
- }
- file.close();
- };
- void donor::del()
- {
- file.open("Don.dat",ios::app|ios::in|ios::out|ios::beg|ios::binary);
- file1.open("temp.dat",ios::app|ios::in|ios::out|ios::beg|ios::binary);
- int rec;
- char found='f',confirm='n';
- cout<<"\nEnter the record number you wish to delete : ";
- cin>>rec;
- while(!file.eof())
- { file.read((char *)&s1,sizeof(s1));
- if(s1.return_num()==rec)
- { s1.display();
- found='t';
- cout<<"\nAre you sure you want to delete this record? (y/n) ";
- cin>>confirm;
- if(confirm=='n')
- file1.write((char *)&s1,sizeof(s1));
- }
- else
- file1.write((char *)&s1,sizeof(s1));
- }
- if(found=='f')
- cout<<"\nRecord not found";
- file.close();
- file1.close();
- remove("Don.dat");
- rename("temp.dat","Don.dat");
- }
- void main()
- {
- int ch;
- do
- {
- clrscr();
- cout<<"\t\t\tBLOOD BANK MANAGEMENT PROGRAM";
- cout<<"\n\n\tDONOR DETAILS\n";
- cout<<"\n\n\t1: Append Records";
- cout<<"\n\n\t2: Display Records";
- cout<<"\n\n\t3: Query on blood group";
- cout<<"\n\n\t4: Modify existing Records";
- cout<<"\n\n\t5: Delete existing Records";
- cout<<"\n\n\t6: End the program";
- cout<<"\n\nEnter your choice (1-6) : ";
- cin>>ch;
- switch(ch)
- {
- case 1:
- s1.accept();
- break;
- case 2:
- s1.display();
- break;
- case 3:
- s1.query();
- break;
- case 4:
- s1.modify();
- break;
- case 5:
- s1.del();
- break;
- case 6:
- cout<<"\nEnd of the program";
- getch();
- break;
- }
- }while(ch!=6);
- }
In the above program the query and the delete function goes on endlessly displaying the record.while int display function the same record gets displayed twice.also in modify function the new details are not being overwritten .they are being added as a new record.Please help me out.Thanks in advacne.This program was coded using TURBO C++ V3.0.