c++ - 遍历数组时出现段错误

标签 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() 的行。提前感谢您的帮助。

最佳答案

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

关于c++ - 遍历数组时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20110329/

相关文章:

c++ - 数值食谱/多维根搜索(使用newt): How to minimize the maximum error

c++ - 无法通过函数为全局变量赋值(Linux 上的共享库注入(inject))

c - 尝试修改指向数组的指针时出现段错误

c - 打开文件时出现段错误

c++ - 第一个带有头文件和 2 个 .cpp 文件的程序

c++ - 所有文件的 mime 类型是什么

c++ - 打印原子枚举值时 gdb 崩溃

c - 小功能段错误

c++ - 获取过滤后的用户输入并使其对应于菜单选项

c++ - 不使用重新定义的符号时避免多定义链接器错误