0

So, my assignment is to make a book inventory from a file that gets read by the program and put into a linked list. All of the classes, members, constructors and functions seen below are required to be in the program. The only added constructed I add is

Date(unsigned int, unsigned int, unsigned int);

The error I'm getting just says "warning: unused variable 'published'.

I don't think I'm using Date *published correctly in class Book. I tried to make a struct in class Date, but that seemed to only make my problems worse.

I would appreciate any nudge in the right direction. Thanks! In the name of saving space, I've left out some stuff that I think is working fine, but I can add back in here if it would be helpful.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class Date{
public:
    unsigned int day;
    unsigned int month;
    unsigned int year;
    Date(void);
    Date(unsigned int, unsigned int, unsigned int);
    ~Date(void);
};

Date::Date(void){
    day = 0;
    month = 0;
    year = 0;
}

Date::Date(unsigned int day, unsigned int month, unsigned int year){
    day = day;
    month = month;
    year = year;
}

Date::~Date(void){
}

class Book{
public:
    string title;
    string author;
    Date *published;
    string publisher;
    float price;
    string isbn;
    unsigned int pages;
    unsigned int copies;
    Book(void);
    Book(string, string, Date, string, float, string, unsigned int, unsigned int );
    ~Book(void);
};

Book::Book(void){
    title = "";
    author = "";
    Date *published = NULL;
    publisher = "";
    price = 0;
    isbn = "";
    pages = 0;
    copies = 0;
}

Book::Book( string title, string author, Date published, string publisher,
           float price, string isbn, unsigned int pages, unsigned int copies){
    title = title;
    author = author;
    published = published;
    publisher = publisher;
    price = price;
    isbn = isbn;
    pages = pages;
    copies = copies;
}

void LinkedList::print_list(void){
    Node *temp = head;
    while( temp != NULL ){
        cout << temp->book->title << endl;
        cout << temp->book->author << endl;
        cout << temp->book->published << endl;
        cout << temp->book->publisher << endl;
        cout << temp->book->price << endl;
        cout << temp->book->isbn << endl;
        cout << temp->book->pages << endl;
        cout << temp->book->copies << endl;
        temp = temp->next;
        cout << endl;
    }
}

LinkedList::~LinkedList(void){
}

int main()
{
    LinkedList myList;
    ifstream myfile("booklist.txt");

    string title;
    string author;
    Date published;
    string publisher;
    float price;
    string isbn;
    unsigned int pages;
    unsigned int copies;

    while( myfile ){
        myList.insert_front( new Book(title, author, published, publisher,
                                      price, isbn, pages, copies));

        myfile >> title;
        myfile >> author;
        myfile >> published;
        myfile >> publisher;
        myfile >> price;
        myfile >> isbn;
        myfile >> pages;
        myfile >> copies;
    }

    myList.print_list();

    return 0;
}
  • 1
    Putting `(void)` in constructors and destructors seems like a really bad habit you've picked up from somewhere. That's normally expressed as `()` for brevity and clarity. – tadman Sep 26 '17 at 03:37
  • consider doing `Date published` instead of `Date *published` – kmdreko Sep 26 '17 at 03:37
  • Don't forget you can override `operator< – tadman Sep 26 '17 at 03:38
  • Possible duplicate of [Why should C++ programmers minimize use of 'new'?](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) –  Sep 26 '17 at 03:38
  • @tadman, the use of `(void)` was part of the assignment instructions. –  Sep 26 '17 at 03:41
  • @vu1p3n0x, I've tried that, but that seems to make more problems. I get a bunch of errors when I try that. in `Book::Book(void)`, I get an error at `published = NULL;` saying there is no math for the operator? –  Sep 26 '17 at 03:43
  • 1
    The people writing these assignments are doing way more harm than good. That is not how production C++ code is written, especially the `using namespace std` part which is practically epidemic. One thing you'll want to do is pass in your arguments by reference for things like `std::string` to avoid making endless copies, plus avoid initializing strings with empty strings when that's their default state. Having argument names and variable names the same will be trouble, as well, try to differentiate those somehow. – tadman Sep 26 '17 at 03:43
  • @1girl2pups which parts of this code are from your assignment (i.e. you cant change) and which parts are yours, that you can change? Hopefullyl it's all of it is yours and can be changed. – Paul Rooney Sep 26 '17 at 04:22
  • @PaulRooney, pretty much all of it cannot be changed :/. The only things that could be changed are `Date(unsigned int, unsigned int, unsigned int);` , both of the `Date *published;` (but only it must contain atleast `Date published`), and then the way the linked list is read could be changed, and the way it is printed could be changed. –  Sep 26 '17 at 04:30
  • You should pick up a [good c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Know that there are plain bad instructors out there, I personally met at least 4 that doesn't know squat about c++ – Passer By Sep 26 '17 at 05:56

0 Answers0