Answer to Question #335682 in C++ for Program training

Question #335682

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), separated by a comma. That list is followed by a name, and your program should output the phone number associated with that name. Output "None" if name is not found.

Ex: If the input is:

3 Joe,123-5432 Linda,983-4123 Frank,867-5309 Frank

the output is:

867-5309

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's answer
2022-04-30T08:16:26-0400
#include <iostream>
#include <vector>
#include <string>


using namespace std;


string GetPhoneNumber(vector<string> nameVec, vector<string> phoneNumberVec, string contactName)
{
	for (int i = 0; i < nameVec.size(); i++)
	{
		if (nameVec[i] == contactName)
			return phoneNumberVec[i];
	}
}


int main()
{
	vector<string> nameVec;
	vector<string> phoneNumberVec;
	string contactName;
	int N;
	cout << "Please, enter the number of word pairs in the list: ";
	cin >> N; 
	cout << "Please, list of names and phone numbers: ";
	for (int i = 0; i < N; i++)
	{
		string tmp,tmpNm;
		cin >> tmp;
		tmpNm = tmp.substr(0, tmp.find(','));
		tmp= tmp.substr(tmp.find(',') + 1);
		nameVec.push_back(tmpNm);
		phoneNumberVec.push_back(tmp);
	}
	cout << "Please, enter the contact name: ";
	cin >> contactName;
	cout << "Phone number is " << GetPhoneNumber(nameVec, phoneNumberVec, contactName);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS