Write an interactive, menu-driven program that will
access the file created in program No.17 and implement the following tasks:
i) To determine the telephone numbers
of the specified person.
ii) To determine the name if a telephone
number is given.
iii) To update the telephone number
whenever there is a change.
#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
class phone
{
public: long int no; char name[10]; void getdata()
{
cout<<endl<<"enter the name :";
cin>>name;
cout<<endl<<"enter the telephone
number :"; cin>>no;
}
void putdata()
{
cout<<endl<<setw(10)<<name<<endl<<setw(10)<<no<<endl;
}
};
int main()
{
phone ph; fstream f; clrscr();
int n; f.open("pp1.txt",ios::trunc|ios::in|ios::out|ios::ate);
cout<<"Enter the number of records :";
cin>>n;
for(int i=0;i<n;i++)
{
ph.getdata();
f.write((char *)&ph,sizeof(ph));
}
f.seekg(0);
cout<<endl<<"conents of the file are
as follows "; while(f.read((char *)&ph,sizeof(ph))) ph.putdata();
int choice; while(1)
{
cout<<"\n1.get name from telephone number";
cout<<"\n2.get telephone number from name"; cout<<"\n3.alter
the telephone number"; cout<<"\n4.EXIT";
cout<<"\nenter ur choice "; cin>>choice;
f.seekg(0);
f.clear();
switch(choice)
{
case 1:cout<<"\nEnter
the telephone number to get the name "; int no;
cin>>no;
f.seekg(0);
f.clear();
while(f.read((char *)&ph,sizeof(ph)))
{
if(ph.no==no)
{
ph.putdata();
}
}
break;
case 2:cout<<"\nEnter
the name to get the telephone number"; char search_name[10];
cin>>search_name;
while(f.read((char *)&ph,sizeof(ph)))
{
if(strcmp(ph.name,search_name)==0)
ph.putdata();
}
break;
case 3:cout<<"\nEnter
the name to modify the telephone number "; char searchname[10];
cin>>searchname;
while(f.read((char *)&ph,sizeof(ph)))
{
if(strcmp(ph.name,searchname)==0)
{
ph.putdata();
cout<<endl<<"Enter the new details
to modify :"; ph.getdata();
int loc=sizeof(ph);
int p=f.tellp(); cout<<endl<<p; f.seekp(p-loc);
f.write((char *)&ph,sizeof(ph));
}
}
cout<<endl<<"After
modification the content of the file is as follows ";
f.clear();
f.seekg(0);
while(f.read((char *)&ph,sizeof(ph)))
ph.putdata();
break;
case 4:exit(0);
}
}
}
******************************OUTPUT**********************************
Enter the number of
records :3 enter the name :aaa
enter the telephone
number :11111 enter the name :bbb
enter the telephone
number :22222 enter the name :ccc
enter the telephone
number :33333 conents of the file are as follows
aaa 11111 bbb 22222 ccc 33333
1.get name from telephone number 2.get telephone
number from name 3.alter the telephone number 4.EXIT
enter ur choice 1
Enter the telephone number to get the name 11111
found
aaa 11111
not foundnot foundnot found 1.get name from telephone
number 2.get telephone number from name 3.alter the telephone number 4.EXIT
enter ur choice 2
Enter the name to get the
telephone numberccc ccc
33333
1.get name from telephone number 2.get telephone
number from name 3.alter the telephone number 4.EXIT
enter ur choice 3
Enter the name to modify
the telephone number ccc ccc
33333
Enter the new details to modify : enter the name :ccc
enter the telephone number :44444 42
After modification the
content of the file is as follows aaa
11111 bbb 22222 ccc
44444
1.get name from telephone number 2.get telephone
number from name 3.alter the telephone number 4.EXIT
enter ur choice 4