It appears that there is a programming error in the on-line solution to the chapter 10 exercise 4 getEntry function. In the on-line solution's getEntry function, the response will be "No entry found for John Doe" only if the entered invalid person name (John Doe) is greater than the last Person in the list. Otherwise, the response wil be "The following numbers are listed for John Doe" followed by an empty line. This behavior is not what the code suggests is correct, based on the text in the two cout's.
Below is the code from the on-line solution, followed by modified code that does the intended function.
Code:
void getEntry(multimap<Person, string>& book)
{
Person person = getPerson();
multimap<Person, string>::iterator iter = book.lower_bound(person);
if(iter == book.end())
cout << "No entry found for " << person.getName() << endl;
else
{
cout << "The following numbers are listed for " << person.getName() << ":" << endl;
for( ; iter != book.upper_bound(person) ; iter++)
cout << iter->second << endl;
}
}
Code:
void getEntry(multimap<Person, string>& book)
{
Person person = getPerson();
multimap<Person, string>::iterator iter = book.lower_bound(person); // refer to page 664 in book
multimap<Person, string>::iterator iter2 = book.upper_bound(person);
// if(iter == book.end())
if(iter == iter2)
cout << "No entry found for " << person.getName() << endl;
else
cout << "The following numbers are listed for " << person.getName() << ":" << endl;
for( ; iter != book.upper_bound(person); iter++)
cout << iter->second << endl;
}
Please comment regarding whether or not you agree, and if you have any other comments regarding this.