Why is my list method only printing the last book

I am building a list method to print out all the books in the warehouse. Which in theory should print all the books read from the file book.dat. However, when running this program, only the last book is printed. What am I doing wrong?

Book.cpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  #include "Book.h"
#include <iostream>
#include <string>
#include <cstring>
using namespace std;

Book :: Book() {}
Book :: Book (string title, string authors[], int authorCount, string publisher, short yearPublish, bool hardcover, float price, string isbn, long copies){
    this->title_ = title;
    this->authorCount_ = authorCount;
    this-> authors_[MAX_AUTHORS-1]=authors[MAX_AUTHORS];
    this->publisher_ = publisher;
    this->yearPublish_ = yearPublish;
    this->hardcover_ = hardcover;
    this->price_ = price;
    this->isbn_ = isbn;
    this->copies_= copies;
}
void Book::setTitle(string title){
    this->title_=title;
}
string Book::getTitle()const{
    return title_;
}

void Book:: setIsbn(string isbn){
    isbn_ = isbn;
}
string Book::getIsbn()const {
    return isbn_;
}
istream& operator >> (istream& is, Book& book){
    string input;
    while(!is.eof()){
        getline(is, book.title_);
        getline(is, input);
        book.authorCount_ = atoi(input.c_str());
        for (int i=0;i<book.authorCount_;i++){
        	getline(is, book.authors_[i]);
        }
        getline(is, book.publisher_);
        is >> book.yearPublish_;
        is >> book.hardcover_;
        is >> book.price_;
        is >> book.isbn_;
        is >> book.copies_;
        getline(is, input);
    }    
    return is;
}
ostream& operator << (ostream& os, const Book& book){
    os<< "Title: " << book.title_ << endl;
    for (int i=0; i<book.authorCount_; i++){
        os<< "Authors: " << book.authors_[i] << endl;
    }
    os<< "Publisher: " << book.publisher_ << endl;
    os<< "Year Published: " << book.yearPublish_ << endl;
    os<<"Cover: ";
    if (book.hardcover_ ==0) os << "Paperback" << endl;
    else if (book.hardcover_==1) os << "Hardcover" << endl;
    os<< "Price: $" << book.price_ << endl;
    os<< "ISBN: " << book.isbn_ << endl;
    os<<"Copies: " << book.copies_ << endl<< endl;
    return os;
}


Warehouse.cpp
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
46
47
48
49
50
51
52
53
#include "Warehouse.h"
#include "Book.h"
#include <iostream>
#include<string>
#include <fstream>

using namespace std;


istream& operator >> (istream& is, Warehouse& warehouse){
    int counter = 0;
    while(!is.eof()){
        is >> warehouse.books[counter];
        counter++;
    }
    warehouse.bookCount = counter;
    return is;
}

ostream& operator << (ostream& os, const Warehouse& warehouse){
    os << warehouse.bookCount<<endl;
    return os;
}

Warehouse :: Warehouse() {};
bool Warehouse:: find (string isbn, Book& book) const{
    bool result = false;
    for (int i=0;i<bookCount;i++){
        if(books[i].getIsbn() == isbn){
            result = true;
            cout << "ISBN: " << books[i].getIsbn() << "-- FOUND"<<endl;
        }
        else cout << "ISBN NOT FOUND" << endl;
    }   
    return result;
}
void Warehouse:: list()const{
    for (int i=0;i<bookCount;i++){
        cout << books[i];
    }
}
void Warehouse::sort_(){
   for (int i=0;i<bookCount;i++){
        for (int j=i+1; j<bookCount; j++){
            if (books[i].getTitle() < books[j].getTitle()){
               Book temp;
                temp = books[j];
                books[i] = books[j];
                books[j] = temp;
            }
        }
    }
}


Main method
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
#include "Book.h"
#include "Warehouse.h"
#include <iostream>
#include<fstream>
#include <string>

using namespace std;

const int MAX_BOOKS = 35;

int main(int argc, char* argv[]){
    Warehouse warehouse;

    string str = argv[1];
    string filename = str;
    ifstream inputFile;
    inputFile.open(filename);
    if (!inputFile){
        cout << "File: " << filename << " could not be open" << endl;
    }
    inputFile >> warehouse;
    if (string(argv[2]) == "list"){
        warehouse.list();
    }
    if (argc>2){
        Book bookInput;
        inputFile >> warehouse;
        if (string(argv[2]) == "find"){
            string isbn = argv[3];
            if (warehouse.find(isbn,bookInput)) cout << bookInput;
        }
    }
    inputFile.close();
    return 0;
}        


book.dat:

C++ Network Programming – Volume 1
2
Douglas C. Schmidt
Stephen D. Huston
Addison-Wesley
2002
0
35.99
0-201-60464-7
236
Programming Ruby
2
David Thomas
Andrew Hunt
Addison-Wesley
2001
0
42.95
0-201-71089-7
123
Problem Solving with C++ - The Object of Programming
1
Walter Savitch
Addison Wesley Longman, Inc.
2001
0
74.00
0-201-70390-4
325
> istream& operator >> (istream& is, Book& book){
This reads the WHOLE file.
Not what it's supposed to do, which is read just one book.

Topic archived. No new replies allowed.