#include <iostream>
#include <fstream>
#include <conio.h>
#include <string.h>
#include <stdio.h>
using namespace std;
class insertmenu
{
private:
char name[ 25 ];
int age;
char bg[4];
char lastddt[20];
char adress[30];
char phno[20];
public:
insertmenu();
char umtid[4];
void insertdonor();
void showdonor();
};
insertmenu :: insertmenu()
{
strcpy (umtid ,"Nill");
strcpy (name ,"Nill");
int name=0;
strcpy (bg ,"Nill");
strcpy( lastddt, "nill");
strcpy (adress ,"Nill");
strcpy (phno ,"Nill");
}
void insertmenu :: insertdonor()
{
cout<<"\n\n ************************************* \n";
cout<<" Donor Directory: Insert Menu\n";
cout<<" *************************************\n";
cin.ignore();
cout<<"\n Enter Your name: ";
gets (name);
fflush(stdin);
cout<<"\n Enter UMT ID : ";
gets(umtid);
fflush(stdin);
cout<<"\n Enter your age: ";
cin>>age;
fflush(stdin);
cout<<"\n Enter Blood Group :";
gets(bg);
fflush(stdin);
cout<<"\n Enter Last Donation Date :";
gets(lastddt);
fflush(stdin);
cout<<"\n Enter Adress :";
gets( adress);
fflush(stdin);
cout<<"\n Enter Phone number: ";
gets( phno);
fflush(stdin);
cout<<"\n\n Record Save Successfully ";
}
void insertmenu :: showdonor()
{
cout<<"\n UMT ID: "<<umtid;
cout<<"\n Donor Name : "<<name;
cout<<"\n Age : "<<age;
cout<<"\n Blood group : "<< bg;
cout<<"\n Last Date of Donation : "<<lastddt;
cout<<"\n Adress : "<<adress;
cout<<"\n Phone Number : "<<phno<<endl<<endl;
}
/* Function Declaration For Handling File Processing By Shahzad khan*/
void write_donor();
void display_all();
void search_record( char *);
void delete_record(char *);
void update_record(char *);
/* Blood Donor project */
int main()
{
int a;
char ch[10];
while(1){
cout<<" \t \t Blood Donor Directory For GCT \t\n";
cout<<"\t\t\t MAIN MENU \n\n";
cout<<"1. Insert a New Donor "<<endl<<endl;
cout<<"2. Update an existing donor"<<endl<<endl;
cout<<"3. Delete a Record of Donor (Using UMT ID) "<<endl<<endl;
cout<<"4. Search a Donor (Using UMT ID) "<<endl<<endl;
cout<<"5. Display All Existing Donor "<<endl<<endl;
cout<<"6. Press 6 for Exit "<<endl<<endl;
cout<<"Select your option (1-6): ";cin>>a;
cout<<endl<<endl;
switch (a)
{
case 1:
write_donor();
break;
case 2:
cout<< "Enter UMTID of Donor : ";cin>>ch;
update_record( ch);
break;
case 3:
cout<< "Enter UMTID of Donor : ";cin>>ch;
delete_record(ch);
break;
case 4:
cout<< "Enter UMTID of Donor : ";cin>>ch;
search_record(ch);
break;
case 5:
display_all();
break;
case 6:
cout<<endl <<endl;
cout<<"\t\t<<<<Thank You For>>>>\n\n\t <<<<Using This Desktop Application >>>> \n\n\n"<<endl;
return 0;
default:
cout<<" Please Enter Value Form 1 to 5 "<<endl<<endl;
}
cin.ignore();
}
}
// Function defination for Insert a New Donor
void write_donor()
{
insertmenu ac;
ofstream fout;
fout.open("file1.dat",ios::binary | ios::app);
ac.insertdonor();
fout.write ((char*) (&ac), sizeof(insertmenu));
fout.close();
}
//Function defination for Display All Donors Accounts
void display_all()
{
insertmenu ac;
ifstream fin;
fin.open ("file1.dat", ios::in | ios :: binary);
if (!fin)
{
cout<<"File is not exist..";
return;
}
else{
fin.read((char*) (&ac), sizeof(insertmenu));
while (!fin.eof())
{
ac.showdonor();
fin.read((char*) (&ac), sizeof(insertmenu));
}
fin.close();
}
}
// Function Defination for Search of Donor Record
void search_record(char *t)
{
{
insertmenu ac;
int counter=0;
ifstream fin;
fin.open("file1.dat", ios :: in | ios :: binary );
if (!fin)
{
cout<<" File is not Found ";
}
else
{
fin.read((char*) (&ac) , sizeof (insertmenu) );
while(!fin.eof() ){
if ( ! strcmp( t, ac.umtid ) )
{
ac.showdonor();
counter++;
}
fin.read((char*) (&ac) , sizeof (insertmenu));
}
fin.close();
if (counter==0)
{
cout<<"\n\nOpsss......Record is not Found."<<endl<<endl;
}
}
}
}
// Function Defination For Delete a Donor Account
void delete_record( char * t)
{
insertmenu ac;
ifstream fin;
ofstream fout;
fin.open("file1.dat", ios:: in | ios:: binary);
if (!fin)
{ cout<<"File doesnt find";}
else
{
fout.open("temp.dat", ios:: out | ios:: binary);
fin.read((char*) (&ac), sizeof(insertmenu));
while (! fin.eof())
{
if (strcmp(ac.umtid,t))
fout.write((char*)(&ac), sizeof(insertmenu));
fin.read((char*) (&ac), sizeof(insertmenu));
}
fin.close();
fout.close();
remove("file1.dat");
rename("temp.dat","file1.dat");
cout<<" \n\n\t\tSuccessfully Deleted\n\n";
}
}
// Function Defination For Update A Donor
void update_record( char * t)
{
insertmenu ac;
fstream obj;
obj.open("file1.dat", ios :: ate | ios :: in | ios :: out | ios :: binary );
obj.seekg(0);
obj.read((char *) (&ac), sizeof(insertmenu));
while (! obj.eof())
{
if ( ! strcmp(t , ac.umtid))
{
cout<<"\t\t\n\n <<Enter New Record>> \n\n ";
ac.insertdonor();
obj.seekp(obj.tellp()-sizeof(insertmenu));
obj.write( (char*)(&ac), sizeof(insertmenu) );
}
obj.read((char *) (&ac), sizeof(insertmenu));
}
}
Post a Comment