Use C++ to i mplement a project to maintain telephone directory of several peopl
ID: 3768101 • Letter: U
Question
Use C++ to implement a project to maintain telephone directory of several people. The directory should
maintain record of each person associated with following information:
-Name
-Contact no
-Age
-State
-Zipcode
The project which you have developed should contain following features:
-Add a record of a specific person to the telephone directory
-Delete a record of a specific person to the telephone directory
-Search a contact number of a person in the directory based on the name
-Print the details of a person based on the name
-Update the contact no of a person existing in the directory
-Sort the information of each person in the directory based on their last names
-Display the size of telephone directory
For your convenience create an input text file and store the information of each person in a systematic manner. Then use file i/o concepts to read the input text file an store the contents. The results should be printed to console and output text file.
Note To implement this project you have to use class
Explanation / Answer
//Telephone Directory
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <iomanip.h>
#include <conio.h>
class phoneBook{
char name[20],contact_no[15],state[15];
int age,zipcode;
public:
void getdata();
void showdata();
char *getname(){ return name; }
char *getContactno(){ return contact_no; }
char *getState(){return state;}
int *getAge() {return age;}
int *getZipcode(){return zipcode;}
void update(char *nm,char *telno){
strcpy(name,nm);
strcpy(phno,telno);
}
};
void phoneBook :: getdata(){
cout<<" Enter Name : ";
cin>>name;
cout<<"Enter Phone No. : ";
cin>>contact_no;
cout<<”Enter state:”;
cin>>state;
cout<<”Enter age”;
cin>>age;
cout<<”Enter zipcode”;
cin>>zipcode;
}
void phoneBook :: showdata(){
cout<<" ";
cout<<setw(20)<<name;
cout<<setw(15)<<contact_no;
cout<<setw(15)<<state;
cout<<”%d”<<age;
cout<<”%d”<<zipcode;
}
void main(){
phoneBook rec;
fstream file;
file.open("d:\phone.dat", ios::ate | ios::in | ios::out | ios::binary);
char ch,nm[20],telno[6];
int choice,found=0;
while(1){
clrscr();
cout<<" *****Phone Book***** ";
cout<<"1) Add New Record ";
cout<<"2) Delete particular Record ";
cout<<"3) Search Telephone No.based on name ";
cout<<"4) print details of person based on name ";
cout<<"5) Update Telephone No. ";
cout<<"6) Sort the information based on name ";
cout<<”7) Exit”;
cout<<"Choose your choice : ";
cin>>choice;
switch(choice){
case 1 : //New Record
rec.getdata();
cin.get(ch);
file.write((char *) &rec, sizeof(rec));
break;
case 2 : //delete record
cout<<" Enter Name : ";
cin>>nm;
file.seekg(0,ios::beg);
found=0;
while(file.read((char *) &rec, sizeof(rec)))
{
if(strcmp(nm,rec.getname())==0)
{
found=1;
rec.removedata();
}
}
break;
case 3 : //Search Tel. no. when person name is known.
cout<<" Enter Name : ";
cin>>nm;
file.seekg(0,ios::beg);
found=0;
while(file.read((char *) &rec, sizeof(rec)))
{
if(strcmp(nm,rec.getname())==0)
{
found=1;
rec.showdata();
}
}
file.clear();
if(found==0)
cout<<" ---Record Not found--- ";
getch();
break;
case 4 : //Display All Records
cout<<" Enter Name : ";
cin>>nm;
file.seekg(0,ios::beg);
found=0;
while(file.read((char *) &rec, sizeof(rec)))
{
if(strcmp(nm,rec.getname())==0)
{
found=1;
rec.showdata();
}
cout<<” The size of directory is”<<sizeof(rec);
}
getch();
break;
case 5 : //Update Telephone No.
cout<<" Enter Name : ";
cin>>nm;
file.seekg(0,ios::beg);
found=0;
int cnt=0;
while(file.read((char *) &rec, sizeof(rec)))
{
cnt++;
if(strcmp(nm,rec.getname())==0)
{
found=1;
break;
}
}
file.clear();
if(found==0)
cout<<" ---Record Not found--- ";
else
{
int location = (cnt-1) * sizeof(rec);
cin.get(ch);
if(file.eof())
file.clear();
cout<<"Enter New Telephone No : ";
cin>>telno;
file.seekp(location);
rec.update(nm,telno);
file.write((char *) &rec, sizeof(rec));
file.flush();
}
break;
case 6 : // Sort the information based on name
while(file.read((char *) &rec, sizeof(rec)))
{
if(strcmp(rec.getname(),rec.getname())<0)
{
found=1;
rec.showdata();
}
}
case 7 : gotoout;
}
}
out:
file.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.