2

I am writing a Library Management program for a school assignment and keep getting a segmentation error. The error occurs when I use a class function (printBook()) to display the information for each book. This function was initially working without any problems but started to throw the error after I made some changes to the rest of the program. The printBook() function has been unchanged (it was one of the first things I wrote so I could see that everything was working properly). Code to follow:

// ****************** Book class and associated code *************************
class Book{
    private:
        int id;
        string title;
        string author;
        string category;
        int numCopies;
    public:
        void newBook(int newID,string newTitle, string newAuthor, string newCategory, int copies);
        //void editBook();
        void loanBook(int bookID, int memID);
        int getID();
        string getTitle();
        string printBook();
};

void Book::newBook(int newID, string newTitle, string newAuthor, string newCategory, int copies)
{
    id = newID;
    title = newTitle;
    author = newAuthor;
    category = newCategory;
    numCopies = copies;
}

/*void Book::editBook()
{

}*/

int Book::getID()
{
    return id;
}

string Book::getTitle()
{
    return title;
}

string Book::printBook() //problem occurs when this function is called
{
    cout << "ID: " << id << endl;
    cout << "Title: " << title << endl;
    cout << "Author: " << author << endl;
    cout << "Category: " << category << endl;
    cout << "Number Of Copies: " << numCopies << endl;
    cout << endl;
}

// ****************** Collection class and associated code *************************
class Collection{
    private:
        Book bookArray[1000];
        int index;
    public:
        Book search(int bookID);
        Book search(string bookTitle);
        int generateNewID();
        void setIndex(int x);
        int getIndex();
        string getTitle();
        void addToCollection(Book newBook);
        void printCollection();
};

Book Collection::search(int bookID)
{
    for(int i = 0; i < index; i++)
        if(bookArray[i].getID() == bookID)
            return bookArray[i]; 
}

Book Collection::search(string bookTitle)
{
    for(int i = 0; i < index; i++)
        if(bookArray[i].getTitle() == bookTitle)
            return bookArray[i]; 
}

int Collection::generateNewID()
{
    for(int i = 0; i < index; i++)
        if(bookArray[i].getID() != i)
            return i;
}

void Collection::setIndex(int x)
{
    index = x;
}

int Collection::getIndex()
{
    return index;
}

void Collection::addToCollection(Book newBook)
{
    bookArray[index] = newBook;
    index++;
    //sorts bookArray by the id each time a new book is added to the bookArray
    for (int i = 0; i < index; i++)
        for(int j = 0; j < index; j++)
            if(bookArray[i].getID() < bookArray[j].getID())
            {
                Book temp = bookArray[i];
                bookArray[i] = bookArray[j];
                bookArray[j] = temp;
            }
}

//This calls the printBook() function so it also throws the error
void Collection::printCollection() 
{
    for(int i = 0; i < index; i++)
        bookArray[i].printBook();
}

// ****************** Function Declarations *************************



// ******************************************************************
int main()
{
    bool run = true; //used to keep program looping to main menu 
    ifstream collectionFile;
    collectionFile.open("books.txt"); // books are saved in this file
    Collection collection;
    collection.setIndex(0);
    Book book;
    Book searchResult;
    int menuSelection;

    int a, e; //a = id, e = copies
    string b, c, d; //b = title, c = author, d = category

    if(collectionFile.is_open())
    {
        while(collectionFile >> a >> b >> c >> d >> e)
        {
            cout << a << ", " << b << ", " << c << ", " << d << ", " << e << endl;
            book.newBook(a, b, c, d, e);
            collection.addToCollection(book);
        }   
    }
    else
        cout << "File not found" << endl;

    cout << "****************************************************************************" << endl;
    cout << "***************** ABC University Library Management System *****************" << endl;
    cout << "****************************************************************************" << endl;
    cout << endl;
    cout << endl;

    while(run)
    {
        cout << "Please make a selection by entering the number next to your selection: " << endl;
        cout << "1. Add new book" << endl;
        cout << "2. Edit a book" << endl;
        cout << "3. Get books on loan to member" << endl;
        cout << "4. Print out overdue books" << endl;
        cout << "5. Print the collection" << endl;
        cout << "6. Exit" << endl;
        cout << "# of books in collection: " << collection.getIndex() << endl;
        cin >> menuSelection;

        switch(menuSelection)
        {
            bool valid;
            char validationInput;
            int searchInt;
            case 1:
                do
                {
                    cout << "Enter the title: " << endl;
                    cin >> b;
                    cout << "Enter the author: " << endl;
                    cin >> c;
                    cout << "Enter the category: " << endl;
                    cin >> d;
                    cout << "Enter the number of copies: " << endl;
                    cin >> e;
                    cout << "You have entered :" << endl;
                    cout << b << ", " << c << ", " << d << ", " << e << endl;
                    cout << "Is this correct? (Enter 'Y' or 'N')" << endl;
                    cin >> validationInput;
                    validationInput = (char)toupper(validationInput);
                    if(validationInput == 'Y')
                        valid = true;
                    else
                        valid = false;
                }
                while(!valid);
                book.newBook(collection.generateNewID(),b,c,d,e);
                collection.addToCollection(book);
                cout << "You have added the book: " << endl;
                book.printBook(); // error here
                break;
            case 2:
                do
                {
                    cout << "Do you know the ID number of the book you would like to edit? (Enter 'Y' or 'N')" << endl;
                    cin >> validationInput;
                    validationInput = (char)toupper(validationInput);
                    if(validationInput == 'Y')
                    {
                        cout << "Please enter the book ID of the book you would like to edit: " << endl;
                        cin >> searchInt;
                        searchResult = collection.search(searchInt);
                        searchResult.printBook(); //error here
                    }
                }
                while(!valid);
                break;
            case 5:
                collection.printCollection(); //error here
                break;
            case 6:
                cout << "Thank you for using the ABC University Library Management Program!" << endl;
                run = false;
                break;
            default:
                cout << "Invalid Selection";
                break;
        }
    }

    return 0;
}

I have commented the lines where the printBook() is used. Thank you for your help in advance.

3
  • 3
    Could it be related to the fact that you have declared printBook as returning a string, but it doesn't actually return a value? Should it be returning void instead? Commented Nov 21, 2013 at 0:53
  • That did it lol. I guess sometimes you need a fresh set of eyes to find your silly mistakes. CAn post this as an answer so I can mark it as solved? Commented Nov 21, 2013 at 1:05
  • If you turn up the warning level of your compiler and pay attention to the warnings, it is easy to spot on. Commented Dec 10, 2013 at 6:35

1 Answer 1

2

Just as per my comment, it looks to be related to the return type of printBook being string, but you are not returning a value. The return type should probably be void.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.