2013-11-21 39 views
2

我正在爲一個學校作業編寫一個圖書館管理程序,並不斷收到分段錯誤。當我使用類功能(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()的行。提前謝謝你的幫助。

+3

它可能與你已經聲明printBook返回字符串的事實有關,但它實際上並沒有返回值嗎?它應該返回'void'嗎? –

+0

這樣做了哈哈。我想有時候你需要一組新的眼睛來找到你的愚蠢的錯誤。 CAn發佈這個答案,以便我可以將其標記爲已解決? – IanAuld

+0

如果調高編譯器的警告級別並注意警告,很容易發現。 –

回答

2

正如我的評論,它看起來與printBook的返回類型是string,但你沒有返回一個值。返回類型應該可能是void