Telephone Diary using C++

The project telephone diary using C++ stores contact information of person. The program telephone diary has used file system to store contact information in disk. It stores ID, Name, Address, Phone, Mobile and Email of a person. This project allows us to add, update, delete and search particular record of person. We can search contact information from person name and mobile number. Again, we can list out all contact in alphabetical order choosing appropriate option in program. Again, we can update and delete existing contact information on the basis of person ID. The program is using ID to identify a person uniquely.

Code of Telephone diary

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
#include<iomanip.h>
#include<string.h>
#include<stdio.h>
const char* fileName=”phone.dat”;
class Person
{
private:
int ID;
char Name[20],Address[50],Phone[10],Mobile[10],Email[30];
public:
void ReadData();
int GetID();
void DisplayRecord();
char* GetMobile();
char* GetName();
};
void Person::ReadData()
{
cout<<endl<<“ID:”;
cin>>ID;
cout<<“Name:”;
cin>>Name;
cout<<“Address:”;
cin>>Address;
cout<<“Phone:”;
cin>>Phone;
cout<<“<Mobile:”;
cin>>Mobile;
cout<<“Email:”;
cin>>Email;
}
void Person::DisplayRecord()
{
cout<<endl<<“______________________________________________”;
cout<<endl<<setw(4)<<ID<<setw(10)<<Name<<setw(10)<<Address<<setw(10)<<Phone<<setw(10)<<Mobile<<stew(15)<<Email;
}

int Person::GetID()
{ return ID; }

char* Person::GetMobile()
{ return Mobile; }

char* Person::GetName()
{ return Name; }

void main()
{
Person p,pl,temp,prs[100];
char option,ch,Mobile[10],Name[10];
int ID,isFound;
clrscr();
fstream file;
file.open(fileName,ios::ate|ios::in|ios::out|ios::binary);
do
{
cout<<“****Welcome to telephone diary system****”<<endl;
cout<<“**********Menu**********”;
cout<<endl<<“Enter your option”;
cout<<endl<<” 1 => Add a new contact”;
cout<<endl<<” 2 => Search contact information from name”;
cout<<endl<<” 3 => Search contact information form mobile”;
cout<<endl<<” 4 => Display all contact information”;
cout<<endl<<” 5 => Update particular contact on the basis of ID”;
cout<<endl<<” 6 => Delete contact on the basis of ID”;
cout<<endl<<” 7 => Exit from the program”<<endl;
cout<<“********************”<<endl;
cin>>option;
switch(option)
{
case ‘1’:
p.ReadData();
file.seekg(0,ios::beg);
isFound=0;
file.read((char*)&p1,sizeof(p1));
while(!file.eof())
{
if(strcmp(p1.GetMobile(),p.GetMobile())==0)
{
cout<<“The contact having this mobile no. already exists.”;
isFound=1;
break;
}
file.read((char*)&p1.sizeof(p1));
}
if(isFound==1)
break;
file.clear();
file.seekp(0,ios::end);
file.write((char*)&p,sizeof(p));
cout<<endl<<“New contact has been added successfully…”;
break;

case ‘2’:
isFound=0;
cout<<endl<<“Enter name to be searched:”;
cin>>Name;
file.seekg(0,ios::beg);
file.read((char*)&p1,sizeof(p1));
while(!file.eof())
{
if(strcmp(p1.GetName(),Name)==0)
{
cout<<endl<<setw(4)<<ID<<setw(10)<<Name<<setw(10)<<Address<<setw(10)<<Phone<<setw(10)<<Mobile<<stew(15)<<Email;
p1.DisplayRecord();
isFound=1;
break;
}
file.read((char*)&p1,sizeof(p1));
}
file.clear();
if(isFound==0)
cout<<endl<<“Contact not found with the name”<<Mane;
break;

case ‘3’:
isFound=0;
cout<<endl<<“Enter mobile number to know about this:”;
cin>>Mobile;
file.seekg(0,ios::beg);
file.read((char*)&p1.sizeof(p1));
while(!file.eof())
{
if(strcmp(p1.GetMobile(),Mobile)==0)
{
cout<<“The contact information for this mobile number”;
cout<<endl<<setw(4)<<“ID”<<setw(10)<<“Name”;
cout<<setw(10)<<“Address”<<setw(10)<<“Phone”;
cout<<setw(10)<<“Mobile”<<setw(15)<<“Email”;
p1.DisplayRecord();
isFound=1;
break;
}
file.read((char*)&p1,sizeof(p1));
}
file.clear();
if(isFound==0)
cout<<endl<<“Contact not found for mobile number”<<Mobile;
break;

case ‘4’:
cot<<endl<<“******All Contacts******”;
file.clear();
file.seekg(0,ios::beg);
int counter=0;
file.read((char*)&p1,sizeof(p1));
while(!file.eof())
{
prs[counter]=p1;
counter++;
if(counter==1)
{
cout<<endl<<setw(4)<<“ID”<<setw(10)<<“Name”;
cout<<endl<<setw(10)<<“Address”<<setw(10)<<“Phone”;
cout<<endl<<setw(10)<<“Mobile”<<setw(15)<<“Email”;
}
file.read((char*)&p1,sizeof(p1));

}
for(int i=0;i<counter;i++)
{
for(int j=i+1;j<counter;j++)
{
if(strcmp(prs[i].GetName(),prs[j].GetName())>0)
{
temp=prs[i];
prs[i]=prs[j];
prs[j]=temp;
}
}
}
for(i=0;i<counter;i++)
prs[i].DisplayRecord();
cout<<endl<<counter<<“record found…..”;
file.clear();
break;

case ‘5’:
int recordNo=0;
cout<<endl<<“EnterID of person to be updated:”;
cin>>ID;
isFound=0;
file.seekg(0,ios::beg);
file.read((char*)&p1,sizeof(p1));
while(!file.eof())
{
recordNo++;
if(p1.GetID==ID)
{
cout<<endl<<“The old record of person having ID”<<ID<< ” is:”;
p1.DisplayRecord();
isFound=1;
break;
}
file.read((char*)&p1,sizeof(p1));
}

if(isFound==0)
{
cout<<endl<<“Contact not found for ID#”<<ID;
break;
}
file.clear();
int location=(recordNo-1)*sizeof(p1);
file.seekp(location,ios::beg);
cout<<endl<<“Enter new record for person having ID”<<ID;
p1.ReadData();
file.write((char*)&p1,sizeof(p1));
break;

case ‘6’:
recordNo=0;
cout<<endl<<“Enter Person ID to be deleted:”;
cin>>ID;
isFound=0;
file.seekg(0,ios::beg);
file.read((char*)&p1,sizeof(p1));
while(!file.eof())
{
recordNo++;
if(p1.GetID()==ID)
{
cout<<endl<<“The old contact having ID”<<ID<< ” is:”;
p1.DisplayRecord();
idFound=1;
break;
}
file.read((char*)&p1,sizeof(p1));
}

char tempFile[]=”temp.txt”;
fstream temp(tempFile,ios::out|ios::binary);
if(isFound==0)
{
cout<<end;<<“Data not found for person ID#”<<ID;
break;
}
else
{
file.clear();
file.seekg(0,ios::beg);
file.read((char*)&p1,sizeof(p1));
while(!file.eof())
{
if(p1.getID()!=ID)
temp.write((char*)&p1,sizeof(p1));

file.read((char*)&p1,sizeof(p1));
}
file.close();
temp.close();
temp.open(tempFile,ios::in|ios::binary);
file.open(file.Name,ios::out|ios::binary);
temp.read((char*)&p1,sizeof(p1));
while(!temp.eof())
{
file.write((char*)&p1,sizeof(p1));
temp.read((char*)&p1,sizeof(p1));
}
}//end of else
temp.cloase();
file.close();
remove(tempFile);
file.open(fileName,ios::ate|ios::in|ios::out|ios::binary);
break;

case ‘7’
exit(0);
break;
default:
cout<<“Invalid option”;
}
cout<<“\nDo you want to continue….?y/n”;
cin>>ch;
}while(ch!=’n’);
}