Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there!
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

C++
Date(unsigned int, unsigned int, unsigned int);


What I have tried:

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

I don't think I'm using
C++
Date *published
correctly in
C++
class Book
. I tried to make a struct in
C++
class Date
out of the variables in there, 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.

C++
#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;
    }
}



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 >> publisher;
        myfile >> price;
        myfile >> isbn;
        myfile >> pages;
        myfile >> copies;
    }

    myList.print_list();

    return 0;
}
Posted
Updated 25-Sep-17 21:53pm

Do not use pointer for your simple class Date. A pointer is required when your class either has big data (copying of big data reduces performance) or has some elements that cannot be easily copied (such as a class for a database connection, file or network operations, etc). So, in your case you can use the Date as is:
C++
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);
};
 
Share this answer
 
Quote:
Book::Book(void){
title = "";
author = "";
Date *published = NULL; // <------------ BUG HERE
publisher = "";
price = 0;
isbn = "";
pages = 0;
copies = 0;
}

While The bug is in the above code where you should write instead
C++
published = NULL;

or, better
C++
published = nullptr;


I second Andrew Cherednik on his suggestion: don't use a pointer for the Date member.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900