Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I cant' display book and borrower's details

Status
Not open for further replies.

grscot

Programmer
Apr 25, 2003
16
GR
<pre>
Dear all,
I can't borrow a book even though the code does not have any error. When I try to display the book details along with the member's name (in case the member has borrowed the book), I can't display the member's name, but only the book details and a message: &quot;no member has borrowed a book&quot;.


Though the code works fine when I have one book and one member, but now that I'm using an array of books and members it doesn't work.


The code that displays the book and the member's name when I have 1 book and 1 member is:
<code>
void Book::display(Member* borrower)
{
cout<<&quot;Book title and author are: &quot;<<this->bookDetails<<&quot;.&quot;<<endl;
if (borrower)
cout<<&quot;The member's name is: &quot;<<borrower->get_Data()/*get_name()*/<<&quot;.&quot;<<endl;
else
cout<<&quot;No member has borrowed a book.&quot;<<endl;
}
</code>


<code>
char* get_Data(){return this->name;}
</code>
<pre>returns the member's full name, entered in the constructor</pre>


<pre>
I have an associationlist that holds pointers to associations. Each association has two pointers, one to a book object and the other to the member object.
</pre>

<code>
class AssociationList
{
public:
//It initialises all slots in the associationlist array to zero.
AssociationList();

/* It searches the associationlist, if book is found then returns
the member that is connected with.*/
//Member* get_member(Book* book);
Member* get_data(Book* book);

/* It searches the associationlist, if member is found then returns
the book that is connected with.*/
//Book* get_book(Member* member);
Book* get_data(Member* member);

/* Checks that book/member not already linked
creates association if objects are free to link
returns whether or not link was valid */
bool link(Book* book,Member* member);

/* Checks that book and member are linked
deletes association if they are linked
returns whether or not unlinking was valid */
bool unlink(Book* book,Member* member);
private:
Association<Book,Member>* association_list[LIST_SIZE];
};
</code>



<pre>The code that performs the linking between 1 book object and 1 member object is the following:</pre>

<code>
template<class Book,class Member>
bool AssociationList<Book,Member>::link(Book* book,Member* member)
{
bool searching=true;
bool success=true;
int index=0;
int free_slot;

while(searching)
{
if(this->association_list[index])
if((this->association_list[index]->linked_book()==book ||
(this->association_list[index]->linked_member()==member)))
{
searching=false;
success=false;
}
else
index++;
else
{
free_slot=index;
index++;
}
if(searching&&(index == LIST_SIZE))
searching=false;
}
if (success)
this->association_list[free_slot]=new Association(book,member);
return success;
}
</code>


<pre>
Code that is used in the link function
</pre>

<code>
template<class Book,class Member>
class Association
{
public:
//Sets up book and member with parameters
Association(Book* book, Member* member);

//Returns Book
Book* linked_book(){return this->book;}

//Returns Member
Member* linked_member(){return this->member;}

private:
Book* book;
Member* member;
};
</code>

<pre>
and
</pre>

<code>
template<class Book,class Member>
Association<Book,Member>::Association(Book* book, Member* member)
{
cout<<&quot;Association constructor called\n&quot;;
this->book=book;
this->member=member;
}
</code>


<pre>
What needs to be done so that each member to be able to borrow one book?
(10-members)
(10-books)
(1 member can only borrow one book at a time)

Regards,
grscot

</pre>


 
Do you know how to use the debugger and step through your code? This usually proves helpful for issues like this.

-pete

 
Not really, I don't know how to use the debugger.

grscot
 
Then I would suggest you start learning that real quickly, as I've never heard of a developer yet that can create any program without having debug sessions......


Greetings,
Rick
 
Funny you should say that Rick... I just had a conference call with two of them today from our home office! [bugeyed]

that was fun [hammer]

-pete

 
Dear all,

I'm still struggling to display the book and the borrower's details together.
Could someone help me?

regards,
grscot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top