What is inbooks? It sounds like it should be a collection of some sort, but the setTitle(), setAuthor(), etc. don't fit this. Usually you would have a "book" class and inbooks would be a collection of objects of type "book", like std::vector<book>. So what is inbooks? Also show the implementation of printbooks(). How does printbooks() know which book to print? It doesn't take an index value, so I wonder how it works.
Ok, so I don't even understand how that code compiles. inbooks is an array of 3 books, and that array doesn't have setTitle(), setAuthor(), etc. The Library constructor should not compile. It should be something like this:
Library::Library(ifstream& inbookfile, ifstream& incardfile)
{
//temporary variables
string title, author, name, isbn;
int status, holderID, cardnum, booksout;
string phonenum;
for(int i = 0; i < 3; i++)
{
//reading Books
inbookfile >> title >> author >> isbn >> status >> holderID;
inbooks[i].setTitle(title);
inbooks[i].setAuthor(author);
inbooks[i].setISBN(isbn);
inbooks[i].setStatus(status);
inbooks[i].setholder(holderID);
}
}
inbooks -being an array- doesn't have the printbooks() method so line #5 in showBooks() shouldn't compile either for the same reason. Also note that since printbooks() belongs to the Book class, it really is just one book, so it should really bee printbook(), singular.
I think the problem lies in the fact that you use a non-standard way to allocate the inbooks array. Line 10 is invalid C++, except maybe for one compiler (your compiler, I guess).
I think the inbooks array is destroyed when the constructor finishes. Try dynamic allocation (and standard C++):
1 2 3 4 5
//Change the declaration of inbooks to:
Book *inbooks;
//Now replace line 10 above to be:
inbooks = new Book[numBooks];
Also remember to delete[] inbooks; inbooks = 0; in the destructor of the Library class.
Book::Book(const Book &cSource)
{
title = cSource.title;
author = cSource.author;
ISBN = cSource.ISBN;
status = cSource.status;
holderID = cSource.holderID;
}
and
operator =
1 2 3 4 5 6 7 8 9 10
Book& Book::operator= (const Book &cSource)
{
title = cSource.title;
author = cSource.author;
ISBN = cSource.ISBN;
status = cSource.status;
holderID = cSource.holderID;
return *this;
}
for(int i = 1; i < numBooks; i++)
{
//reading Books
inbookfile >> title >> author >> isbn >> status >> holderID;
inbooks[i].setTitle(title);
inbooks[i].setAuthor(author);
inbooks[i].setISBN(isbn);
inbooks[i].setStatus(status);
inbooks[i].setholder(holderID);
inbooks[i].printbooks();
cout << "TESTING" << endl; //testing to see if books print once read.
}
shouldn't be for (i=0;i<numbooks;i++)??
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
inbooks = new Book[numBooks];
for(int i = 0; i < numBooks; i++)
{
inbookfile >> title >> author >> isbn >> status >> holderID;
inbooks[i].setTitle(title);
inbooks[i].setAuthor(author);
inbooks[i].setISBN(isbn);
inbooks[i].setStatus(status);
inbooks[i].setholder(holderID);
inbooks[i].printbooks();
cout << "TESTING" << endl;
}
You can still access inbooks as an array. Anyway, If you start the "for" with i=1, inbooks [0] will be empty.