mardi 26 mars 2019

How can I take data from a input file while working with classes?

I'm working on a project for class that requires us to make a class named Book and one named Warehouse.

What I am trying to do is have my main cpp file use Book and Warehouse functions to gather data from a .dat file and print them out in a nice format using the functions given to me in the header file.

What am I doing wrong and what should I do?

-- void list() const;

I have written out the operators written out as follows for istream and ostream

in warehouse.cpp

ostream& operator << (ostream& os, const Warehouse& warehouse)
{
   for(int i=0 ; i<warehouse.getSize() ;i++){
   os << "Title: "<<warehouse.getBook(i).getTitle();
   //os << "\nAuthor: "<<warehouse.getBook(i).getAuthors();
   os << "\nPublisher: "<<warehouse.getBook(i).getPublisher();
   os << "\nYear: "<<warehouse.getBook(i).getPublishyear();
   os << "\nCover: "<<warehouse.getBook(i).getHardcover();
   os << "\nPrice: "<<warehouse.getBook(i).getPrice();
   os << "\nISBN: "<<warehouse.getBook(i).getISBN();
   os << "\nCopies: "<<warehouse.getBook(i).getCopies();
   }

   return os;
}

istream& operator >> (istream& is, Warehouse& warehouse){
   string title, authors, Publisher, isbn;
   //int authorCount;
   short yearPublish;
   float price;
   long copies;
   bool hardcover;

   Book temp;
   for (int i=0;i<warehouse.MAX_BOOKS;i++){
   is>>title>>Publisher>>yearPublish>>hardcover>>price>>isbn>>copies;
   temp.setTitle(title);
   //temp.setAuthors(authors);
   temp.setPublisher(Publisher);
   temp.setyearPublish(yearPublish);
   temp.setHardcover(hardcover);
   temp.setISBN(isbn);
   temp.setCopies(copies);
   warehouse.setBook(i, temp);
}

return is;

}

and here is my list function so far in my warehouse.cpp file

void Warehouse::list() const{
   int i;
   for (i = 0; i<bookCount; i++)
   {
       cout << "Title: " << books[i].getTitle() << endl; //print the book title
       /*for (int j = 0; j < books[i].authorCount; j++) //depending on amount of authors it prints them
       {
          cout << "Author: " << books[i].authors[j] << endl;
       }*/
       cout << "Publisher: " << books[i].getPublisher() << endl; //print publisher
       cout << "Year: " << books[i].getPublishyear() << endl; //print year the book was published
       if (books[i].getHardcover() == false) //if 1 then it is a harcover
       {
          cout << "Cover: Paperback" << endl;
       }

       else //else if 0 then print out the cover type of the book is a paperback
       {
          cout << "Cover: Hardcover" << endl;
       }
       cout << "Price: $" << books[i].getPrice() << endl; //print out book price
       cout << "ISBN: " << books[i].getISBN() << endl; //print out isbn number
       cout << "Copies: " << books[i].getCopies() << endl; //print number of copies
       if (i < bookCount)
       {
          cout << endl;
       }
   }
}


How would i implement this in my main function?

int main(int argc, char* argv[])
{
   if(argc <= 2) //if the user did not input the correct amount of arguments in command line
   {
      cout << "./proj01 <input file name> find <id>" << endl;
      cout << "./proj01 <input file name> list"      << endl;
   }
   else if (argc == 3) //if they typed enough arguments
   {
      string filename = argv[1];
      string argv2 = argv[2];
      Book books[34];
      if (argv2 == "list")  //LIST Warehouse ITEMS
      {
         ifstream inputFile;
         inputFile.open(filename);
         if(inputFile.fail())
        cout << "Unable to read input file or file was not found/corrupt" << endl;
     else
     {
        // Code to implement getting data from data input file and list out the books
         }

book.dat "data file"

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

Expected OUTPUT

%./proj02 books.dat list
Title: C++ Network Programming - Volume 1
Author: Douglas C. Schmidt
Author: Stephen D. Huston
Publisher: Addison-Wesley
Year: 2002
Cover: Paperback
Price: $35.99
ISBN: 0-201-60464-7
Copies: 236

Title: Programming Ruby
Author: David Thomas
Author: Andrew Hunt
Publisher: Addison-Wesley
Year: 2001
Cover: Paperback
Price: $42.95
ISBN: 0-201-71089-7
Copies: 123
...

Aucun commentaire:

Enregistrer un commentaire