Program to generate Address book in C++.

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

class address_details
{
private:
struct person
{
char name[20];
char emailid[25];
int teleno;
char add[30];
}p;

fstream file;

public:
address_details();
void addrec();
void delrec();
void updaterec();
void printrec();
void exit();
};

void main()
{
char choice;
address_details g;

do
{

cout<<"\t\t\t1.Add Records"<<endl;
cout<<"\t\t\t2.Print Records"<<endl;
cout<<"\t\t\t3.Update Records"<<endl;
cout<<"\t\t\t4.Delete Record"<<endl;
cout<<"\t\t\t0.Exit"<<endl;
cout<<"\tYour Choice?\t";

cin>>choice;


switch(choice)
{
case '1':
g.addrec();
break;
case '2':
g.printrec();
break;
case '3':
g.updaterec();
break;
case '4':
g.delrec();
break;
case '0':
g.exit();
exit(1);
}
}while(choice!=0);
}


address_details::address_details()
//Zero Argument Constructor
{
file.open("REC.TXT",ios::binary|ios::in|ios::out);
if(!file)
{
cout<<endl<<"Unable to open file";
exit();
}
}

void address_details::addrec()
{
char ch;

file.seekp(0L,ios::end);

do
{
cout<<endl<<"Enter person's name :  "<<endl;
cin>>p.name;
cout<<endl<<"Email ID " <<endl;
cin>>p.emailid;
cout<<endl<<"Telephone no "<<endl;
cin>>p.teleno;
cout<<endl<<"Address "<<endl;
cin>>p.add;

file.write((char*)&p,sizeof(p));

cout<<"Add another Record?(Y/N)";
cin>>ch;

}while(ch=='y'||ch=='Y');

}

void address_details::printrec()
{
int j=1;

file.seekg(0L,ios::beg);
//Get pointer at the beginning of the file

while(file.read((char*)&p,sizeof(p)))
{                                      //as long as end of file is not reached read each record and display it


cout<<endl<<"Record #"<<j++                       //Post-increment of j
<<endl<<"\t\tName: "<<p.name
<<endl<<"\t\tEmail ID: "<<p.emailid
<<endl<<"\t\tTelephone no: "<<p.teleno
<<endl<<"\t\tAddress: "<<p.add;
}

file.clear();

if(j==1) cout<<"No record"<<endl;               //If the address book is empty

cout<<endl<<"Press any key...."<<endl;
getch();

}


void address_details::updaterec()
{
char name[20];
int count=0;
long int pos;

cout<<"Enter person's name : ";
cin>>name;

file.seekg(0L,ios::beg);

while(file.read((char*)&p,sizeof(p)))
{
if(strcmp(p.name,name)==0)
{
cout<<endl<<"Enter new record"<<endl;

cout<<endl<<"Enter person's name :  ";
cin>>p.name;
cout<<endl<<"Email ID ";
cin>>p.emailid;
cout<<endl<<"Telephone no ";
cin>>p.teleno;
cout<<endl<<"Address ";
cin>>p.add;

pos=count*sizeof(p);

file.seekp(pos,ios::beg);
file.write((char*)&p,sizeof(p));
return;
}
count++;
}

cout<<endl<<"No Record with name = "<<name;
cout<<endl<<"Press any key...."<<endl;
getch();

file.clear();
}


void address_details::delrec()
{
char name[20],ch;
cout<<"Enter the name of the person whose record you wanna delete : "<<endl;
cin>>name;

ofstream outfile;
//creates a temporary file of stream outfile
outfile.open("TEMP.TXT",ios::out);

file.seekg(0L,ios::beg);
//position get pointer at the beginning of the file


while(file.read((char*)&p,sizeof(p)))
{
if(strcmp(p.name,name)==0)
{
cout<<endl<<p.name
<<endl<<p.emailid
<<endl<<p.teleno
<<endl<<p.add;
cout<<endl<<"Confirm the record Deletion?(Y/N)";
cin>>ch;
}

if(ch=='Y'||ch=='y')
{
file.seekg(0L,ios::beg);
while(file.read((char*)&p,sizeof(p)))
{
if(strcmp(p.name,name)!=0)
outfile.write((char*)&p,sizeof(p));
}
cout<<endl<<"Record Deleted"<<endl;
outfile.close();
file.close();


remove("REC.TXT");
rename("TEMP.TXT","REC.TXT");

file.open("REC.TXT",ios::binary|ios::in|ios::out);

}

}

if (ch=='N'||ch=='n')
{
cout<<endl<<"Deletion Cancelled"<<endl;
//while(file.read((char*)&p,sizeof(p)))
//    outfile.write((char*)&p,sizeof(p));

outfile.close();
return ;
}
//file.close


//remove("REC.TXT");
//    rename("TEMP.TXT","REC.TXT");

//file.open("REC.TXT",ios::binary|ios::in|ios::out);

}


void address_details::exit()
{
file.close();
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s