Phone Number List. This program should have an array of at least 10 string objec
ID: 667133 • Letter: P
Question
Phone Number List. This program should have an array of at least 10 string objects. The array will hold people’s names and phone numbers. The following list is an example of the data in the array.
"Renee Javens, 678-1223",
"Joe Looney, 586-0097",
"Geri Palmer, 223-8787",
"Lynn Presnell, 887-1212",
"Bill Wolfe, 223-8878",
"Sam Wiggins, 486-0998",
"Bob Kain, 586-8712",
"Tim Haynes, 586-7676",
"John Johnson, 223-9037",
"Jean James, 678-4939",
"Ron Palmer, 486-2783"
The program should ask the user to enter a name or partial name to search for in the array. Any entries in the array that match the string entered should be displayed. For example, if the user enters “Palmer” the program should display the following names from the list:
Geri Palmer, 223-8787
Ron Palmer, 486-2783
NOTE: if the user enters “Pal” it should produce the same output.
Explanation / Answer
#include <iostream.h>
#include <fstream.h>
#include <string.h>
using namespace std;
int main()
{
const int SIZE = 50;
string phoneDirectory[SIZE];
int size=0;
string name; //name to look for
ifstream inFile;
inFile.open("phonebook");
while (!inFile.fail()) {
getline(inFile,phoneDirectory[size]);
size++;
}
inFile.close();
// Get a name or partial name to search for.
cout << "Enter a name or partial name to search for: ";
getline(cin, name);
cout << " Here are the results of the search: " << endl;
int numberEntriesFound = 0;
for (int k = 0; k < size; k++)
{
if (phoneDirectory[k].find(name.data(), 0) < phoneDirectory[k].length())
{
numberEntriesFound ++;
cout << phoneDirectory[k] << endl;
}
}
if (numberEntriesFound == 0)
cout << " No Entries were found for " << name;
return 0;
}