I need your help for this C++ Problem. PLEASE do it with COMMENTS, thank you. Pr
ID: 3893635 • Letter: I
Question
I need your help for this C++ Problem. PLEASE do it with COMMENTS, thank you.
Problem 2: (Phone Number Lookup) Write a program that initializes two arrays: one is a string array that contains names and the other is an array of long integers containing phone numbers (as long integers without spaces or hyphens). Each array should have length at least 5. Prompt the user to input a name. Then, search the first array for that name using a loop. If the name exists, then print out the phone number from the other array (at the same subscript where you found the name) in the form xxx-xxx-xxxx. If the name does not exist in the first array, then print out an error message For example, suppose the first array is {"Mike", "Sue", "Joe", "Mary", "Bob"^ and the second is (1234567890, 9876543210, 5463728109, 3456789012, 7531086420 [notice no hyphens] If the user enters "Sue", then your program should print out 987-654-3210 (with the hyphens) If the user enters "Jen", then the program should print out an error message Please use made-up names and phone numbers as I don't need any personal information about anybodyExplanation / Answer
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
using namespace std;
void display_phone_number_with_format(string key, long int phone_number){
cout << "Entry to phone directory for " << key << " is " <<phone_number << endl;
stringstream ss;
ss << phone_number;
string result = ss.str();
result.insert(6, "-" ); // insert hyphens
result.insert(3, "-" );
cout << result<< endl;
}
int search(string key, string names[], int size){
int not_found = -1;
for(int i=0; i < size; i++ ){
if(key == names[i]){
int found = i;
return found;
}
}
return not_found;
}
void search_display(string key, string names[], long int numbers[], int size ){
int index = search( key, names, size) ;
if ( index > -1 ){
display_phone_number_with_format(key, numbers[index]);
}
else{
cout << "Entry to phone directory for " << key << " is not found" << endl;
}
}
int main(){
int size = 5;
string names[] ={"Mike", "Sue", "Joe", "Mary", "Bob" };
long int numbers[] = {1234567890, 9876543210, 5463728109, 3456789012,
7531086420};
string yes_no = "y";
string key;
while(1){
cout << "Enter name to search" << endl;
cin >> key;
search_display( key , names, numbers, size );
cout << "do you want to continue y/n" << endl;
cin >> yes_no;
if(yes_no != "y"){
break;
}
}
}