Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

in cplus plus You will design an online contact list to keep track of names and

ID: 666909 • Letter: I

Question

in cplus plus

You will design an online contact list to keep track of names and phone numbers.

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.

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.

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

< >

Explanation / Answer

Answer:

#include <iostream.h>

#include <fstream.h>

#include <string.h>

#include <iomanip.h>

#include <conio.h>

class contact_List {

char name1[20],phno1[15];

public:

void get_mydata();

void show_mydata();

char *getname1(){ return name1; }

char *getphno1(){ return phno1; }

void update1(char *nm1,char *telno1){

strcpy(name1,nm1);

strcpy(phno1,telno1);

}

};

void contact_List :: get_mydata(){

cout<<"NENTER NAME1 : ";

cin>>name1;

cout<<"ENTER PHONE NO. : ";

cin>>phno1;

}

void contact_List :: show_mydata(){

cout<<" ";

cout<<setw(20)<<name1;

cout<<setw(15)<<phno1;

}

void main(){

contact_List record;

fstream file;

file.open("ph.txt", ios::ate | ios::in | ios::out | ios::binary);

char ch,nm1[20],telno1[6];

int choice1,found1=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>>choice1;

switch(choice1){

case 1 :

//GET THE NEW RECORD

record.get_mydata();

cin.get(ch);

file.write((char *) &record, sizeof(record));

break;

case 2 :

//update1

cout<<" ENTER NAME : ";

cin>>nm1;

file.seekg(0,ios::beg);

found1=0;

int cnt1=0;

while(file.read((char *) &record, sizeof(record)))

{

cnt1++;

if(strcmp(nm1,record.getname1())==0)

{

found1=1;

break;

}

}

file.clear();

if(found1==0)

cout<<" ---RECORD NOT FOUND--- ";

else

{

int location = (cnt1-1) * sizeof(record);

cin.get(ch);

if(file.eof())

file.clear();

cout<<"ENTER NEW TELEPHONE NO : ";

cin>>telno1;

file.seekp(location);

record.update1(nm1,telno1);

file.write((char *) &record, sizeof(record));

file.flush();

}

break;

case 3 :

//display

file.seekg(0,ios::beg);

cout<<" Records in Phone Book ";

while(file){

file.read((char *) &record, sizeof(record));

if(!file.eof())

record.show_mydata();

}

file.clear();

getch();

break;

case 4 : //Search Tel. no. when person name1 is known.

cout<<" ENTER NAME : ";

cin>>nm1;

file.seekg(0,ios::beg);

found1=0;

while(file.read((char *) &record, sizeof(record)))

{

if(strcmp(nm1,record.getname1())==0)

{

found1=1;

record.show_mydata();

}

}

file.clear();

if(found1==0)

cout<<" ---RECORD NOT FOUND--- ";

getch();

break;

case 5 : goto out;

}

}

out:

file.close();

}