0

I have looked over many questions available on SO, but I could not find something that actually answered my question.

I was wondering how to implement the istream& operator >> method to create an array of Books.

I just need help with the istream method, not the whole program.

#include "Warehouse.h"
#include "Book.h"
#include <iostream>
#include<string>

using namespace std;

static const int MAX_BOOKS = 35;

clss Warehouse {
    /**
    * @param is the input stream
    * @param warehouse the warehouse object reference
    * @return the input stream
    */
    friend istream& operator >> (istream& is, Warehouse& warehouse);

    /**
    * @param os the output stream
    * @param warehouse the warehouse object reference
    * @return the output stream
    */
    friend ostream& operator << (ostream& os, const Warehouse& warehouse);
    public:
        static const int MAX_BOOKS = 35;
        Warehouse();
        /**
         * @param isbn the ISBN number to search for
         * @param book reference to the matched book object, if found
         * @return true if found.
         */
        bool find (string isbn, Book& book) const;
        /**
         * Prints the inventory of the Warehouse (i.e. list all the books)
         */
        void list () const;
    private: /* extra credit */
        void sort_();
    private:
        Book books[Warehouse::MAX_BOOKS];
        int bookCount;
};

#endif /* WAREHOUSE_H */

This is the cpp file for the Book:

#include "Book.h"
#include <iostream>
#include <string>
using namespace std;


static const int MAX_BOOKS = 35;
static const int MAX_AUTHORS = 20;
string title_;
string authors_[Book::MAX_AUTHORS];
int authorCount_;
string publisher_;
short yearPublish_;
bool hardcover_;
float price_;
string isbn_;
long copies_;


istream& operator >> (istream& is, Book& book){
    is >> book.title_;
    is.ignore();
    is >> book.authorCount_;
    for (int j=0;j<book.authorCount_;j++){
        is >> book.authors_[j];
        is.ignore();
    }
    is >> book.publisher_;
    is.ignore();
    is >> book.yearPublish_;
    is.ignore();
    is >> book.hardcover_;
    is.ignore();
    is >> book.price_;
    is.ignore();
    is >> isbn_;
    is.ignore();
    is >> copies_;
    is.ignore();
    return is;
}
ostream& operator << (ostream& os, const Book& book){
    os<< book.title_ << endl;
    for (int j=0; j<book.authorCount_; j++){
        os<< book.authors_[j] << endl;
    }
    os<< book.publisher_ << endl;
    os<< book.yearPublish_;
    os<<book.hardcover_;
    os<< book.price_ << endl;
    os<< book.isbn_ << endl;
    os<< book.copies_ << endl;
    return os;
}

Book :: Book() {};
Book :: Book (string title, string authors[], int authorCount, string publisher, short yearPublish, bool hardcover, float price, string isbn, long copies){
    title_ = title;
    authorCount_ = authorCount;
    for (int i=0;i<authorCount_;i++){
        authors_[i] = authors[i];
    }
    publisher_ = publisher;
    yearPublish_ = yearPublish;
    hardcover_ = hardcover;
    price_ = price;
    isbn_ = isbn;
    copies_= copies;
}
void Book::setTitle(string title){
    title_=title;
}
string Book::getTitle()const{
    return title_;
}
void Book:: setAuthorCount(short authorCount){
    authorCount_ = authorCount;
}
void Book::setAuthors(string authors[]){
    authors_[MAX_AUTHORS]=authors[MAX_AUTHORS];
}
string Book::getAuthors() const{
    return authors_[MAX_AUTHORS-1];
}
void Book::setPublisher(string publisher){
    publisher_ = publisher;
}
string Book::getPublisher() const{
    return publisher_;
}
void Book::setYearPublish(short yearPublish){
    yearPublish_ = yearPublish;
}
short Book:: getYearPublish() const{
    return yearPublish_;
}
void Book::setHardcover(bool hardcover){
    hardcover_ = hardcover;
}
bool Book::getHardcover() const{
    return hardcover_;
}
void Book:: setIsbn(string isbn){
    isbn_ = isbn;
}
string Book::getIsbn() const{
    return isbn_;
}
void Book:: setPrice(float price){
    price_ = price;
}
float Book:: getPrice() const{
    return price_;
}
void Book:: setCopies(long copies){
    copies_ = copies;
}
long Book::getCopies()const{
    return copies_;
}
David Park
  • 21
  • 4
  • Probably related: https://stackoverflow.com/a/23263087/3684343 – mch Mar 27 '19 at 09:23
  • without the definition of _Warehouse_ how can you hope an help ? – bruno Mar 27 '19 at 09:24
  • Sorry, I just did not know how to add my code. Below, I added the warehouse class header file, as well as the program for Book.cpp which is also part of the project. Hope this explains it. – David Park Mar 27 '19 at 09:25
  • I would ditch all those `is.ignore()` calls. It's not a subsititute for understanding how input works in C++. – john Mar 27 '19 at 09:33
  • 1
    It’s often better to define `<<` so it writes a format that `>>` can easily read, and leave user-facing formatting to the application. – molbdnilo Mar 27 '19 at 09:33
  • In your `Book` version of `operstor<<` you consistently use `cout` when you should be using `os`. – john Mar 27 '19 at 09:33
  • Oh you're right! I will change that right now. Thank You! – David Park Mar 27 '19 at 09:35
  • Your `Book` versions of `operator<<` and `operator>>` have a serious flaw, that something written by `operator<<` could not then be read back by `operator>>`. These functions are supposed to go in pairs. So output by `operator<<` can be read by `operator>>`. (just noticed @molbdnilo said the same thing) – john Mar 27 '19 at 09:35
  • Are you saying I should be using getters and setters to write this code? Sorry, I am not fully grasping what you are saying. – David Park Mar 27 '19 at 09:39
  • First you need to fix the Book versions of operator>> and operator<<. I would ditch all the 'ignore` stuff and all the `cout << "Publisher: ";` stuff. Make them a paitr so that they are both using the same format. Then you need to decide how to format the `Warehouse`. In particular you need to decide how to indicate how many books are in the Warehouse. Obviousl thing would be to output the number of book first. Then yuo need to write your Warehouse operator>> and operator<< using the Book operator>> and operator<<. – john Mar 27 '19 at 09:40
  • @DavidPark No, simply that the formats are different, when you write you output headings, `"Publisher: "` etc. but you read code takes no account of these heading. – john Mar 27 '19 at 09:40
  • If `<<` writes "Title: The Lord of the Rings\n", then `>>` should extract "The Lord of the Rings" from that, and move past the "\n" – Caleth Mar 27 '19 at 09:41
  • I am so sorry if I sound really stupid, but can you explain what you mean by same format? this is my first time working with Class in C++ programs and I am just very confused. Thank You guys so much for using your time helping me. I really appreciate it – David Park Mar 27 '19 at 09:42
  • Look at you output code. If outputs "Publisher: Penguin" (or whatever). Now look at your input code. Does it understand that "Publisher" is just a heading? – john Mar 27 '19 at 09:43
  • Ohhh I get it. I tried cleaning up my << method, would that work better than before? I still haven't gotten rid of the is.ignore() yet. – David Park Mar 27 '19 at 09:45
  • @DavidPark Your input and output function should be complimentary. If you write a Book with operator<< then you can read the same book with operator>>. That's not the case at the moment because the two functions are using a different format to save book information. – john Mar 27 '19 at 09:45
  • I was wondering... so then from here, I would get the inputs for the >> operator of the Warehouse Class from the << operator of the Book Class? – David Park Mar 27 '19 at 09:47
  • @DavidPark Yes I would ditch the heading. I would also make life easy for myself by choosing a fomrat that is easy to read back in. For instance I would put each field on a seperate line, so I can read them back in with getline. – john Mar 27 '19 at 09:47
  • I tried something like that by using endl; would it be better if I just used \n instead? – David Park Mar 27 '19 at 09:50
  • @DavidPark Same basic rules apply, operator>> and operator<< for Warehouse should be complimentary. It certainly makes sense to reuse the Book format inside the bigger Warehouse format. But as I said before you also need to think of some mechanism to know how many books in the warehouse. Storing the number of books before the individual book seems the obvious thing to do. But get the Book stuff sorted first, and then build on those to do the Warehouse stuff. – john Mar 27 '19 at 09:50
  • @DavidPark The only difference is that endl also flushes output. `\n` make more sense I think since flushing the output is something the user of operator<< can decide upon. It's not something the operator<< itself should be deciding. – john Mar 27 '19 at 09:52
  • But what I do not get is that there are multiple books that I am reading from a file, and if I try to use the operators to form the Warehouse Class objects, how would it read past the first book? Also would it be better if I used getline for my Book operator >>? – David Park Mar 27 '19 at 09:56
  • did you see this question https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading ? It is quite exhaustive. If it does not cover what you need help with, please be more specific on what is your question – 463035818_is_not_an_ai Mar 27 '19 at 10:19
  • @user463035818 As of now, I am wondering how I would be able to call the operator << from Book Class when trying to implement the operator << of the Warehouse Class. I am looking through the like right now, but I do not find anything that can help me – David Park Mar 27 '19 at 10:40
  • 1
    you simply call it via `some_ostream << some_book;`, though I dont find anything like this in your code. Please read about [mcve], I guess you could make a simpler example with only two classes with one or two members only. – 463035818_is_not_an_ai Mar 27 '19 at 10:43
  • If I wanted to use the operator << from Class Book, would I use Book::os? because the operator name in both classes Book and Warehouse is os, so there must be a way to distinguish them. – David Park Mar 27 '19 at 10:59

0 Answers0