In C++, using a data structure and a header file, I am being asked to create and
ID: 3779990 • Letter: I
Question
In C++, using a data structure and a header file, I am being asked to create and maintain a list of contact information for your friends and family. Include the following functionality:
1) add a new contact
2) delete a contact
3) update contact info
4) display contacts in ascending order by last name
5) find the contact info for a particular person
For each person you should store name, phone number, and email address.
You must chose an appropriate data structure to use to implement this system, and design and implement a contactType class.
Can someone please point me into the right direction?
Explanation / Answer
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class telephone
{
char name[25];
char address[40];
char phoneno[15];
public:
telephone(){ }
telephone(char nam[25], char add[40], char phone[15])
{
strcpy(name,nam);
strcpy(address,add);
strcpy(phoneno,phone);
}
void search();
void delete_d();
void insert();
void update();
void display()
{
cout<<"
Name : "<<name;
cout<<"
Address : "<<address;
cout<<"
Phone Number: "<<phoneno;
}
int compare(char nam1[25])
{
if(strcmp(nam1,name)==0)
return 1;
else
return 0;
}
};
void telephone::insert()
{
telephone tel1;
fstream file;
file.open("Phone.txt",ios::app);
tel1.init();
file.write((char *) &tel1, sizeof(tel1));
file.close();
}
void telephone::init()
{
cout<<"
Enter Name : "; cin.getline(name,25);
cout<<"
Enter Address : "; cin.getline(address,40);
cout<<"
Enter Phone Number: "; cin.getline(phoneno,15);
}
void telephone::delete_d()
{
telephone tel1;
fstream file ;
char nam[25],nam1[25];
strcpy(nam1,"");
cout<<"Enter the Name to be deleted: ";
cin>>nam;
file.open("Phone.txt", ios::in) ;
fstream temp ;
temp.open("temp.txt", ios::out) ;
file.seekg(0,ios::beg) ;
while (file.read((char *) &tel1, sizeof(telephone)))
{
if(!tel1.compare(nam))
temp.write((char *) &tel1, sizeof(telephone)) ;
else
strcpy(nam1,nam);
}
file.close() ;
temp.close() ;
setcolor(7);
if(strlen(nam1)!=0)
{
file.open("Phone.txt", ios::out) ;
temp.open("temp.txt", ios::in) ;
temp.seekg(0,ios::beg) ;
while (temp.read((char *) &tel1, sizeof(telephone)))
{
file.write((char *) &tel1, sizeof(telephone)) ;
}
file.close() ;
temp.close() ;
}
}
void telephone::update()
{
telephone tel1;
fstream file ;
char nam[25],nam1[25];
strcpy(nam1,"");
cout<<"Enter the Name to be updated: ";
cin>>nam;
file.open("Phone.txt", ios::in) ;
fstream temp ;
temp.open("temp.txt", ios::out) ;
file.seekg(0,ios::beg) ;
while (file.read((char *) &tel1, sizeof(telephone)))
{
if(!tel1.compare(nam))
temp.write((char *) &tel1, sizeof(telephone)) ;
else
strcpy(nam1,nam);
}
file.close() ;
temp.close() ;
setcolor(7);
if(strlen(nam1)!=0)
{
file.open("Phone.txt", ios::out) ;
temp.open("temp.txt", ios::in) ;
temp.seekg(0,ios::beg) ;
while (temp.read((char *) &tel1, sizeof(telephone)))
{
file.write((char *) &tel1, sizeof(telephone)) ;
}
file.close() ;
temp.close() ;
char nam[25],add[40],ph[15];
file.open("Phone.txt",ios::app);
cout<<"
Enter Name : ";
cin.getline(nam,25);cin.getline(nam,25);
cout<<"
Enter Address : "; cin.getline(add,40);
cout<<"
Enter Phone Number: "; cin.getline(ph,15);
tel1=telephone(nam,add,ph);
file.write((char *) &tel1, sizeof(tel1));
file.close();
outtextxy(250,250,"Updating Record.....");
}
else
cout<<”Record not found";
}
void telephone::search()
{
fstream file;
telephone tel1;
int i=1;
char nam[25];
file.open("Phone.txt",ios::in);
cout<<"Enter name to be Searched: ";
cin>>nam;
file.seekg(0,ios::beg);
while(file.read((char *) &tel1, sizeof(telephone)))
{
if(tel1.compare(nam))
{
tel1.display();
i=0;
break ;
}
}
file.close() ;
if(i)
{
Cout<<"Record not found”;
}
getch();
}
void main()
{
Telephone ob1
Int ch;
do
{
Cout<<”Enter The Menu “<<” ”;
Cout<<” MENU “;
Cout <<” 1. ADD Contract”;
Cout <<” 2. Display Contract”;
Cout <<” 3. Update Contract”;
Cout<<” 4. Delete Contract”;
Cout <<” 5. Find The contact”;
Cout<<”Enter Your Choise”
Cin >>ch;
Switch(ch)
{
Case 1:
Ob1. insert();
Break;
Case 2:
Ob1. display();
Break;
Case 3:
Ob1. update();
Break;
Case 4:
Ob1.delete_d();
Break;
Case 5:
Ob1. search();
Break;
Default:
Cout<<”wrong choice”
}
}while(ch!0)
}
1) add a new contact
2) delete a contact
3) update contact info
4) display contacts in ascending order by last name
5) find the contact info for a particular person