
William S.
asked 02/28/21How Can I Write a Program For a Contact List Using Functions? (C++)
A contact list is a place where you can store a specific contact with other associated information such as a phone number, email address, birthday, etc. Write a program that first takes as input an integer N that represents the number of word pairs in the list to follow. Word pairs consist of a name and a phone number (both strings). That list is followed by a name, and your program should output the phone number associated with that name.
Ex: If the input is:
the output is:
Your program must define and call the following function. The return value of GetPhoneNumber is the phone number associated with the specific contact name.
string GetPhoneNumber(vector<string> nameVec, vector<string> phoneNumberVec, string contactName)
Hint: Use two vectors: One for the string names, and the other for the string phone numbers.
1 Expert Answer

Patrick B. answered 02/28/21
Math and computer tutor/teacher
using namespace std;
#include <iostream>
#include <vector>
#include <string>
#include <string.h>
typedef struct _TPhoneContactRec
{
string name;
string phoneNum;
} *TPhoneContactRec;
void InputPhoneRecs( vector<struct _TPhoneContactRec> & phoneContactVector , int N)
{
for (int iLoop=0; iLoop<N; iLoop++)
{
struct _TPhoneContactRec curPhoneContactRec;
cout << "Phone contact rec # " << (iLoop+1) << " of " << N << endl;
cout << "----------------------------------------------------------" << endl;
cout << "please input Name :>";
cin >> curPhoneContactRec.name;
cout << "please input phone # :>";
cin >> curPhoneContactRec.phoneNum;
phoneContactVector.push_back(curPhoneContactRec);
}
}
void DumpPhoneRecs( vector <struct _TPhoneContactRec> phoneRecVector, char * strMsg=NULL)
{
if (strMsg!=NULL)
{
cout << "*******************************************************" << endl;
cout << strMsg << endl;
}
cout << "**************************************************************" << endl;
int N = phoneRecVector.size();
cout << " Name Phone " << endl;
cout <<"----------------------------------------" << endl;
for (int iLoop=0; iLoop<N; iLoop++)
{
struct _TPhoneContactRec curPhoneContactRec;
curPhoneContactRec = phoneRecVector[iLoop];
cout << curPhoneContactRec.name << " " << curPhoneContactRec.phoneNum << endl;
}
}
typedef int (*CompareCallbackFuncPtr) (TPhoneContactRec phoneRec1, TPhoneContactRec phoneRec2);
ComparePhoneRecByName( TPhoneContactRec phoneRec1, TPhoneContactRec phoneRec2)
{
char name1[255];
char name2[255];
strcpy(name1,phoneRec1->name.c_str());
strcpy(name2,phoneRec2->name.c_str());
return(stricmp(name1,name2));
}
ComparePhoneRecByPhone( TPhoneContactRec phoneRec1, TPhoneContactRec phoneRec2)
{
char phone1[255];
char phone2[255];
strcpy(phone1,phoneRec1->phoneNum.c_str());
strcpy(phone2,phoneRec2->phoneNum.c_str());
return(strcmp(phone1,phone2));
}
int SearchMenu()
{
int iReturn=-1;
while ((iReturn<0) || (iReturn>3))
{
cout << "---------------------" << endl;
cout << "<1> SEARCH BY NAME " << endl;
cout << "<2> SEARCH BY PHONE " << endl;
cout << "<3> SHOW PHONE RECS " << endl;
cout << "----------------------" << endl;
cout <<" Input Selection or ZER0 to Quit :>";
cin >> iReturn;
}
return(iReturn);
}
int LinearSearch (vector <struct _TPhoneContactRec> phoneRecVector, TPhoneContactRec target, CompareCallbackFuncPtr compareFunc)
{
int N = phoneRecVector.size();
int iReturn=-1;
for (int iLoop=0; iLoop<N; iLoop++)
{
struct _TPhoneContactRec curPhoneRec = phoneRecVector[iLoop];
if (compareFunc(target,&curPhoneRec)==0)
{
iReturn = iLoop;
break;
}
}
return(iReturn);
}
int main()
{
int N;
cout << "How many ??? :>";
cin >> N;
vector<struct _TPhoneContactRec> phoneContactVector;
int iReturn=-1;
InputPhoneRecs(phoneContactVector,N);
//debugging only: you may comment it out
DumpPhoneRecs(phoneContactVector, (char*) "PHONE RECS INPUT");
struct _TPhoneContactRec targetPhoneRec;
while ((iReturn=SearchMenu())!=0)
{
switch (iReturn)
{
case 1:
{
cout << "Name? :>";
cin >>targetPhoneRec.name ;
int iIndexPos = LinearSearch(phoneContactVector,&targetPhoneRec,ComparePhoneRecByName);
if(iIndexPos>-1)
{
cout << phoneContactVector[iIndexPos].name << " " << phoneContactVector[iIndexPos].phoneNum << endl;
}
break;
}
case 2:
{
cout << "Phone # ???? :>";
cin >>targetPhoneRec.phoneNum;
int iIndexPos = LinearSearch(phoneContactVector,&targetPhoneRec,ComparePhoneRecByPhone);
if(iIndexPos>-1)
{
cout << phoneContactVector[iIndexPos].name << " " << phoneContactVector[iIndexPos].phoneNum << endl;
}
break;
}
case 3:
{
DumpPhoneRecs(phoneContactVector);
break;
}
} //switch
} //while
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
I apologize I sent the wrong one... be right with you...02/28/21