Question: Output of this code? Put in necessary header files, please! const int MAX_ENTRIES = 20; const int MAX_NAME_LEN = 40; const int PHONE_LEN = 8;
Output of this code? Put in necessary header files, please!
const int MAX_ENTRIES = 20;
const int MAX_NAME_LEN = 40;
const int PHONE_LEN = 8;
struct PhoneEntry
{
char name[MAX_NAME_LEN + 1], phone[PHONE_LEN + 1];
};
struct PhoneBookType
{
int numPhones;
PhoneEntry phoneList[MAX_ENTRIES];
};
void ReadPhoneBook(PhoneBookType& APhoneBook);
void ReadEntry(PhoneEntry &entry);
void PrintMatches(const char number[], const PhoneBookType& APhoneBook);
void PrintEntry(const PhoneEntry &entry);
int main()
{
PhoneBookType MyPhoneBook;
char phoneNumber[PHONE_LEN + 1];
ReadPhoneBook(MyPhoneBook);
cout << endl;
cin >> phoneNumber;
while ( cin )
{
PrintMatches(phoneNumber, MyPhoneBook);
cout << endl;
cin >> phoneNumber;
}
return 0;
}
void ReadEntry(PhoneEntry &entry)
{
cin >> entry.phone;
cin >> entry.name;
}
void ReadPhoneBook(PhoneBookType& APhoneBook)
{
cout << "Number of entries in phone book: ";
cin >> APhoneBook.numPhones;
cout << "Enter items in the format " << endl;
for(int i = 0; i < APhoneBook.numPhones; i++)
ReadEntry(APhoneBook.phoneList[i]);
}
void PrintEntry(const PhoneEntry& entry)
{
cout << entry.name << ": " << entry.phone;
}
void PrintMatches(const char number[], const PhoneBookType& APhoneBook)
{
bool foundMatch = false;
cout << "Entries matching " << number << ":" << endl;
for(int i = 0; i < APhoneBook.numPhones; i++)
{
if ( strcmp(APhoneBook.phoneList[i].phone, number) == 0)
{
foundMatch = true;
PrintEntry(APhoneBook.phoneList[i]);
cout << endl;
}
}
if ( !foundMatch )
cout << "-- none" << endl;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
