output problem

I am reading .txt files in a constructor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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.setTitle(title);
		inbooks.setAuthor(author);
		inbooks.setISBN(isbn);
		inbooks.setStatus(status);
		inbooks.setholder(holderID);
        }
}

and I have a another function showing those books:
1
2
3
4
5
6
7
8
void Library::showBooks()
{
	for(int i=0; i < 3 ; i++)
	{
		inbooks.printbooks();
	}
	
}

and a do command:
1
2
3
4
5
6
7
8
9
10
11
12
int Library::doCommand(int command)
{
	switch(command)
	{
	case 1:
		showCards();
		break;
	case 2:
		showBooks();
		break;
        }
}


when 2 is pressed, nothing outputs.

please help,
Thank you.
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.
inbooks is an object of Book inside class Library(library "has-a" Book(class)).
so like this
1
2
3
4
5
6
7
8
9
class Library
{
private:
	int numCards;
	int numBooks;

	Book inbooks[3];
public:
//.... 


This is printbooks() inside Book class.
1
2
3
4
5
6
7
8
void Book::printbooks()
{
	cout << "Title: " << title << endl;
	cout << "Author: " << author << endl;
	cout << "ISBN-10: " << ISBN << endl;
	cout << "Status: " << status << endl;
	cout << "Holder's ID number: " << holderID << endl << endl;
}

title,author,isbn are all private data members here.

This is the only issue left, and I cant figure out why it wont print to the screen!, and the project is due tomrrow :/.

Thank you for your help.
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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.
yes, I am sorry, my constructor is exactly what you posted, I posted this before it was changed to an array.

when I test
inbooks[i].printbooks();
in the Library constructor, every book prints after reading from files.

but when 2 is pressed, inside the program to showbooks(switch statements), this is all that outputs:

Title:
Author:
ISBN-10:
Status: 0
Holder's ID number: 0

and it shows nothing is assigned? even tho it is?!
something is wrong and I cant pinpoint exactly.
Show your code. Your REAL code this time. Copy & paste, no re-typing.
Library Constructor:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Library::Library(ifstream& inbookfile, ifstream& incardfile)
{
	//temporary variables
	string title, author, name, isbn;
	int status, holderID, cardnum, booksout;
	string	phonenum; 

	numBooks = 3;
	numCards = 3;
	inbooks[numBooks];

	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.
			       
		}
		
	for(int i = 0; i < 3; i++)
		{
			incardfile >> name >> phonenum >> cardnum >> booksout;

			incards.setname(name);
			incards.setphonenum(phonenum);
			incards.setcardnum(cardnum);
			incards.setbooksout(booksout);

			//TEST: incards.printcards();
		
		}		
}

Havn't made Cards into an array yet.

inbooks.printbooks() works in constructor after reading from file.
inbooks.printbooks() does not work when called in "showbooks()" function.

heres showbooks:
1
2
3
4
5
6
7
void Library::showBooks()
{
		for(int i = 0; i < 3; i++){
		inbooks[i].printbooks();
	}

}
Last edited on
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.
Okay I will try this.

I just re-read the requirements of the project and I havn't done this for Book class:

copy itself (copy constructor), be assigned to (operator=),


could that be the issue?
if so, how would I create a copy constructor and assigned operator???

Thank you.

EDIT: I tried what you said, the program compiles, when I press 2, only an empty "Title: " is displayed and then it says it has stopped working.
Last edited on
I added this copy constructor for Book.

1
2
3
4
5
6
7
8
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;
}


Last edited on
Okay this is what my Library constructor looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Library::Library(ifstream& inbookfile, ifstream& incardfile)
{
	//temporary variables
	string title, author, name, isbn;
	int status, holderID, cardnum, booksout;
	string	phonenum; 

	numBooks = 3;
	numCards = 3;
	inbooks = new Book[numBooks];

	for(int i = 1; i < numBooks; i++)
	{
		inbookfile >> title >> author >> isbn >> status >> holderID;

			inbooks->setTitle(title);
			inbooks->setAuthor(author);
			inbooks->setISBN(isbn);
			inbooks->setStatus(status);
			inbooks->setholder(holderID);

			inbooks->printbooks();
			cout << "TESTING" << endl;
			       
		}
		
	for(int i = 0; i < 3; i++)
		{
			incardfile >> name >> phonenum >> cardnum >> booksout;

			incards.setname(name);
			incards.setphonenum(phonenum);
			incards.setcardnum(cardnum);
			incards.setbooksout(booksout);

			//TEST: incards.printcards();
			
		}		
}


Print test is working in constructor, showbooks() function still fails to display any books, instead now it crashes when told to show books.
Debug the program and find out which line exactly makes your application crash.
okay.

I got this message, when 2 is pressed while debugging:
Unhandled exception at 0x00083516 in Project1-330.exe: 0xC0000005: Access violation reading location 0xcccccce0.


and in xstring:

size_type size() const
{ // return length of sequence
return (this->_Mysize); <-----------Here?
}


maybe I am not assigning(title,author,isbn etc) correctly, since now Book has a copy constructor and operator= ???
Rephrasing: Debug the program and find out which line of code of your own making makes your application crash.

In other words: When the program crashes, look at the call stack. Identify the line of code that you wrote where the program crashes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.
Last edited on
Topic archived. No new replies allowed.