You will design an online contact list to keep track of names and phone numbers
ID: 665924 • Letter: Y
Question
You will design an online contact list to keep track of names and phone numbers using c++.
·
1. Define a class contactList that can store a name and up to 3 phone numbers (use an array for the phone numbers). Use constructors to automatically initialize the member variables.
2. Add the following operations to your program:
Add a new contact. Ask the user to enter the name and up to 3 phone numbers.
Delete a contact by having the user enter the name.
Search the list by name and print the name and phone numbers for that one person.
Print all contacts and phone numbers for each contact.
3. Create a program to test your class. The program should have a menu like this one.
Add a contact
Delete a contact
Print all contacts
Search for a contact
Exit
Explanation / Answer
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <iomanip.h>
#include <conio.h>
class contactList {
char name[20],phno[15];
public:
void getdata();
void showdata();
char *getname(){ return name; }
char *getphno(){ return phno; }
void update(char *nm,char *telno){
strcpy(name,nm);
strcpy(phno,telno);
}
};
void contactList :: getdata(){
cout<<" Enter Name : ";
cin>>name;
cout<<"Enter Phone No. : ";
cin>>phno;
}
void contactList :: showdata(){
cout<<" ";
cout<<setw(20)<<name;
cout<<setw(15)<<phno;
}
void main(){
contactList 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<<" *****Contact List***** ";
cout<<"1) Add a contact ";
cout<<"2) Delete a contact ";
cout<<"3)Print all contacts ";
cout<<"4)Search for a contact ";
cout<<"5) 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 :
//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 3 ://Display All Records
file.seekg(0,ios::beg);
cout<<" Records in Phone Book ";
while(file){
file.read((char *) &rec, sizeof(rec));
if(!file.eof())
rec.showdata();
}
file.clear();
getch();
break;
case 4 : //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 5 : 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.