
William S.
asked 03/01/21How Do I Output the Corresponding Contact Phone Number for an Inputted Contact Name? (C++)
Previously Queried Background Info:
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.
My current issue is that the program does not output the phone number but instead outputs nothing.
This assignment has had me stumped for a while now, so any help would be greatly appreciated.
1 Expert Answer

Patrick B. answered 03/01/21
Math and computer tutor/teacher
using namespace std;
#include <string>
#include <vector>
#include <iostream>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
string ToUpperCase( string str)
{
int n = str.length();
char * strUppercase = (char*)malloc(n+1);
char * cstr = (char*)malloc(n+1);
strcpy((char*)cstr,(char*)str.c_str());
for (int iLoop=0; iLoop<n; iLoop++)
{
strUppercase[iLoop] = toupper(cstr[iLoop]);
}
string upperCaseStr(strUppercase);
free(cstr);
free(strUppercase);
return(upperCaseStr);
}
string GetPhoneNumber(vector<string> nameVec, vector<string> phoneNumberVec, string contactName)
{
int N = nameVec.size();
int iIndexPos=-1;
string strPhoneNumReturn;
string strTargetContactName = ToUpperCase(contactName);
cout << "target name = " << strTargetContactName << endl;
for (int iLoop=0; iLoop<N; iLoop++)
{
string strName = nameVec[iLoop];
string CurStrName = ToUpperCase(strName);
if (strTargetContactName.compare(CurStrName)==0)
{
iIndexPos=iLoop;
break;
}
}
if (iIndexPos>-1)
{
strPhoneNumReturn = phoneNumberVec[iIndexPos];
}
return(strPhoneNumReturn);
}
int main()
{
vector<string> strVectorNames;
vector<string> strVectorPhoneNumbers;
int N=-1;
while (N<1)
{
cout << "How many ??? :>";
cin >> N;
}
string targetName;
string targetPhoneNum;
string curName;
string curPhoneNum;
for (int iLoop=0; iLoop<N; iLoop++)
{
cout << "Phone contact # " << (iLoop+1) << endl;
cout << "---------------------------------------" << endl;
cout << "Please input the name :>";
cin >> curName;
cout << "Please input the phone # :>";
cin >> curPhoneNum;
strVectorNames.push_back(curName);
strVectorPhoneNumbers.push_back(curPhoneNum);
}
cout << "Please input the Name :>";
cin >> targetName;
targetPhoneNum = GetPhoneNumber(strVectorNames,strVectorPhoneNumbers,targetName);
cout << targetPhoneNum << endl;
}
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.
You are not comparing the name in the vector to the passed target name. If they are equal, compare returns zer0. You then must set the index of the string and BREAK, as you have found the name03/01/21