How to read a file into pointers

I am trying to read a file using two separate classes: Book.cpp and Warehouse.cpp.
Book is the objects and the Warehouse is basically a collection of Books.
However, my operator >> method for Warehouse.cpp is not working correctly most likely due to my lack of knowledge about pointers and how to use them.
Can anyone help me fix my problem for istream& operator >> for the Warehouse.cpp?


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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  #include "Warehouse.h"
#include "Book.h"
#include <iostream>
#include <string>

using namespace std;

Warehouse:: Warehouse() {}

Book* Warehouse:: find(string isbn) const{
    Book* testBook;
    Book* correctBook;
    bool result = false;

    while (result == false){
        Book* temp;
        
        if (testBook->getISBN() == isbn){
            result = true;
            correctBook = testBook;
            break;
        }
        else if(testBook->getISBN() != isbn){
            temp = testBook;
            testBook = temp->getNext();
        }
    }
    return correctBook;
}

void Warehouse :: list() const{
    Book* testBook;
    Book* temp;
    testBook = head;
    for (int i=0; i<this->bookCount;i++){
        cout << testBook;
        temp = testBook;
        testBook = temp->getNext();
    }
}
    

istream& operator >> (istream& is, Warehouse& warehouse){
    
    int counter = 0;
    Book* temp;
    Book* headNode;
        Book* inputNode;

        temp = new Book;
        headNode = new Book;
        inputNode = new Book;

    is >> headNode;
    while (is >> inputNode){
        temp = headNode;
        is>> headNode;
        headNode->setNext(temp); 
        counter++;
    }
    warehouse.bookCount = counter; 
    warehouse.head = headNode;
    return is;
}

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



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
66
67
68
69
70
71
72
73
74
75
76
77
#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] = authors[MAX_AUTHORS];
    this -> publisher = publisher;
    this -> yearPublish = yearPublish;
    this-> hardcover = hardcover;
    this-> price = price;
    this-> isbn = isbn;
    this-> copies = copies;
    this->next = nullptr;
}

void Book:: setTitle(string title){
    this-> title = title;
}
string Book:: getTitle()const{
    return title;
}
void Book:: setISBN(string isbn){
    this-> isbn = isbn;
}
string Book::getISBN()const{
    return isbn;
}
void Book::setNext(Book* next){
    this-> next = next;
}
Book* Book:: getNext() const{
    return next;
}

istream& operator >> (istream& is, Book& book){
    string input;
    int counter = 1;
    while (counter==1 && !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;
        getline(is, input);
        counter++;
    }
    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;
}
> my operator >> method for Warehouse.cpp is not working correctly
you need to be more descriptive
¿does your code compiles? ¿what error message does the compiler give you?
¿the list is not recreated? ¿how did you test it?

you are working with a file, you need to provide an example of its content.

in `Warehouse:: find()' you never initialise the pointers, yet you try to dereference them.
in `operator<<(Warehouse)' you just output the size of the list

in `operator>>(Warehouse)' you need abstraction.
1
2
3
4
5
6
istream& operator >> (istream& is, Warehouse& warehouse){
	Book b;
	while(is >> b)
		warehouse.push_back(b);
	return is;
}

Last edited on
It is not compiling, and the error I am getting is too long to paste it onto this page. However, I think it has to do something with pointers and how I cannot make Book* equal Book. However, I do not really know what this means.
I think the part you stated about the find() method is the part I am currently having trouble with. the part about initializing the pointers and trying to dereference them
use github or similar, or just post the top errors, but don't go paraphrasing when you don't have idea of their meaning.
I cannot explain you the error message if I don't know what is the error message.

for the find() function, take a look at your at your list() function
you need to traverse the collection, but instead of printing you need to do a comparison.
also, ¿what'll you return in the case the book is not present on the collection?
Topic archived. No new replies allowed.