迭代数组时出现分段错误

时间:2013-11-21 00:50:46

标签: c++ segmentation-fault

我正在为学校作业编写图书馆管理程序,并不断收到细分错误。当我使用类函数(printBook())来显示每本书的信息时,会发生错误。这个函数最初工作没有任何问题,但在我对程序的其余部分进行了一些更改后开始抛出错误。 printBook()函数没有改变(这是我写的第一件事之一,所以我可以看到一切都正常工作)。代码如下:

// ****************** 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;
}

我评论了使用printBook()的行。感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

正如我的评论一样,它看起来与printBook的返回类型string有关,但你没有返回一个值。返回类型应该是void