why wont anything display?

I am reading 2 .txt files into my 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
42
43
44
45
Library::Library(ifstream& inbookfile, ifstream& incardfile)
{
	//temporary variables
	string title, author, name, isbn;
	int status, holderID, cardnum, booksout, ch;
	string	phonenum; 
  
	while((ch = inbookfile.get()) != EOF)
	{
		for(int i = 0; i < numBooks; i++)
		{
			//reading Books
			getline(inbookfile, title);	        //reads title
			getline(inbookfile, author);
			getline(inbookfile,isbn);               //reads author
			inbookfile >> status >> holderID;

			inbooks = new Book[numBooks];
			inbooks->setTitle(title);
			inbooks->setAuthor(author);
			inbooks->setISBN(isbn);
			
			delete inbooks;
		}
		//booklist.push_back(Book(title,author,isbn,status,holderID));
	}
		
	while((ch = incardfile.get()) != EOF)
	{
		for(int i = 0; i < 10; i++)
		{
			//reading Cards	
			getline(incardfile, name);
			getline(incardfile, phonenum);
			incardfile >> cardnum >> booksout;

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

			//cardlist.push_back(Card(name, phonenum,cardnum,booksout));
		}		
	}
}

I created Books as a pointer object and Cards as a regular object just to test things out.
the problem is when the program asks the user if they want to see all the books or cards, nothing displays to the screen.
Heres the method to show Cards.
1
2
3
4
5
6
7
8
9
10
void Library::showCards()
{
	
	for(int i = 0; i < numCards ; i++)
	{
		//incards.printcards();
		cout << incards.getname();
		cout << incards.getphonenum();
	}
}


Any help would be really appriciated.
Thank you
Any suggestions?
I suggest you check to see what data is read in and check at the point it is read in where it is being stored. Do this by outputting values to screen.
I figured some out, I am able to read now, but something else is wrong,
my .txt has:

Art of War
sun tsu
194765 //isbn
0 //status
0 //holders ID number
Dance
Diya
918476
0
0
Music
Nona
194876
0
0

my code for reading books:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
		for(int i = 0; i < 3; i ++)                 //counting to 3 to test 
                {
			//reading Books
			getline(inbookfile, title);					//reads title
			getline(inbookfile, author);
			getline(inbookfile,isbn);               
			inbookfile >> status >> holderID;
			
			inbooks.setTitle(title);
			inbooks.setAuthor(author);
			inbooks.setISBN(isbn);
			inbooks.setStatus(status);
			inbooks.setholder(holderID);

			cout << inbooks.getTitle() << endl;
			cout << inbooks.getAuthor() << endl;
			cout << inbooks.getISBN() << endl;
			cout << inbooks.getStatus() << endl;
			cout << inbooks.getholder() << endl;

			booklist.push_back(Book(title,author,isbn,status,holderID));
		}

it outputs to the screen:
Art of War
Sun Tsu
194765  
0            
0

Dance
Diya
918476
0

0
Music
Nona
194876
0

An uneven space/Enter is being entered, and I cant seem to find why/how.
Last edited on
I figured it out, for some reason the .txt file has to be like this:

Art of War
sun tsu
194765
0
0 Dance
Diya
918476
0
0Music
Nona
194876
0
0

need to find out why...
need to find out why...


Well let's take a look, using this as the text file:
1
2
3
4
Art of War
sun tsu
194765 //isbn
0 //status 


getline(inbookfile, title);
This code reads in the first line to title, so we've set the title to "Art of War".

getline(inbookfile, author);
This code reads in the next line to author, so we've set the author to "sun tsu".

getline(inbookfile,isbn);
This code reads in the next line to isbn, so we've set the isbn to "194765 // isbn". Hey look, we've read in the bit that says \\isbn as well, because we read in the entire line.

inbookfile >> status >> holderID;
This code reads in the next two words (NOT LINES) to status and holderID. So we've set status to 0 and holderID to //status But those variables are NUMBERS, and we've tried to store in them things that aren't numbers. It's all going wrong!

Does that make it clear? When you read in a text file, the code does not magically skip over text that you don't want to read. It isn't psychic. It will read in the text you tell it to. If you don't want it to read some of the text, YOU must code that. It's not magically able to guess which words you don't want read in.
Last edited on
Yeaaa, sorry about that, the .txt file actually doesn't contain "//isbn" or "//status", that just me copying from somewhere else, sorry for the confusion.


It's not magically able to guess which words you don't want read in.

I thought programming was magic............. :P

the actual .txt file is:

Art of War
sun tsu
194765
0
0
Dance
Diya
918476
0
0


I changed it from getline() to inbookfile >> and then it reads fine but the words cant have spaces between them.
Topic archived. No new replies allowed.