So I finished my project, but my only problem is that I can only search one author at a time. I seem to not be able to figure it out.
This is what I have..am I missing something thats not making me able to find more than one author's last name?
void BookRecordUI::FindBookLast() //allows us to search a book by the last name of the author from the book record...
{
string Last;
cout << "Enter Book by Last Name of Author: " << endl;
getline(cin, Last);
Collection.FindBookAuthorLast(Last);
}
Any help will be much appreciated!
EDIT: So basically I want to find multiple authors..for example, if I inputted John Hopkins and Wilson Greene, I want to pull both authors last name at the same time. Sorry for not clearly explaining it.
I also have this part as well..
void BookRecordList::FindBookAuthorLast(string Last)
{
int K;
for(K = 0; K < (int)List.size(); K++)
if(List[K].GetAuthorLast() == Last)
cout << List[K].GetTitle() << " " << List[K].GetAuthorFirst() << " " << List[K].GetAuthorLast() << " " << List[K].GetPublisher() << " " << List[K].GetPublisherAddress() << " " << List[K].GetPublisherPhone() << " "
<< List[K].GetPublisherContact() << " "<< List[K].GetCategory() << " " <开发者_JAVA百科;< List[K].GetDate() << endl;
};
My whole program is really long, so I dont want to overwhelm you guys by posting the whole thing up..
You may need to change your Book
definition to enable searching for multiple authors:
struct Author
{
std::string& get_name() const;
};
struct Book
{
std::vector<Author> m_authors; // A book can have 1 or more authors
bool has_author(std::string author_name) const
{
std::vector<Author>::const_iterator iter;
for (iter = m_authors.begin();
iter != m_authors.end();
++iter)
{
if (iter.get_name() == author_name)
{
return true;
}
}
return false;
};
The objective now is to write a predicate or functor that will call Book::has_author
.
This is one solution to your issue, there are probably others when you give it more thought.
You do not give us a lot of informations about what is a "book" and what is a Collection
.
But, it seems that you already implement a function that returns you the expected result for one string (the last name of an author).
What you can do is use multiples times your function FindBookAuthorLast
with a different last name each time.
Or, implement a function that takes a vector of string in parameters and that returns you a vector of Book (or whatever is your class containing the books).
EDIT :
With the new informations you posted, here is a way to do it :
(This is not the only solution to do it, there is a lot)
(Code not compiled, not tested)
void BookRecordList::FindBookAuthorLast(vector<string> Last)
{
int K;
vector<string>::iterator author_it = Last.begin();
for ( ; author_it != Last.end(); ++author_it)
{
for(K = 0; K < (int)List.size(); K++)
if(List[K].GetAuthorLast() == *author_it)
cout << List[K].GetTitle() << " " << List[K].GetAuthorFirst() << " " << List[K].GetAuthorLast() << " " << List[K].GetPublisher() << " " << List[K].GetPublisherAddress() << " " << List[K].GetPublisherPhone() << " "
<< List[K].GetPublisherContact() << " "<< List[K].GetCategory() << " " << List[K].GetDate() << endl;
}
};
To build the vector<string>
to give to the function FindBookAuthorLast
, iterate on getline().
Maybe all you want is to loop over your function:
void BookRecordUI::FindBookLast() //allows us to search a book by the last name of the author from the book record...
{
string Last;
do {
cout << "Enter Book by Last Name of Author: " << endl;
getline(cin, Last);
Collection.FindBookAuthorLast(Last);
}
while(!Last.empty());
}
(not tested).
精彩评论