2013-10-08 18 views
-1

程序編譯並運行,但是當我輸入2個或更多的條目時,它在輸入第二個電話號碼的那一刻崩潰。以下是我進入和調用堆棧:當在字符串中讀取時崩潰

crash

「: 0000005:在0x013A6EC6圖書館Holdings.exe未處理的異常。訪問衝突閱讀位置00000000」

這次它在我進入第二個電話#之前崩潰了。

這裏是另一個崩潰發生這種情況,在出於某種原因不同點崩潰,但是這是最常見的死機我正 crash

在0x779B016E(NTDLL.DLL)圖書館Holdings.exe

未處理的異常: 0x00000000:操作成功完成。

我想知道如何讓它停止崩潰?

LibraryDrv.h:

#ifndef _LIBRARYDRV_H 
#define _LIBRARYDRV_H 

#include "Holding.h" 
#include "Recording.h" 
#include "Book.h" 

Holding* inputHolding(); 

#endif 

LibraryDr.cpp:

#include <iostream> 
#include "LibraryDrv.h" 

using namespace std; 

int main() { 
Holding *hptr[5]; 

for (int i = 0; i < 5; i++) { 
    hptr[i] = inputHolding(); 
} 

for (int i = 0; i < 5; i++) { 
    hptr[i]->print(); 
} 

return 0; 
} 

Holding* inputHolding() { 
char selection; 
char title[50]; 
int callNumber = 0; 
char author[50]; 
char performer[50]; 
char format; 

cout << "Enter B for book, R for recording: "; 
cin >> selection; 

if (selection == 'B') { 
    cout << "Enter book title: "; 
    cin >> title; 

    cout << "Enter book author: "; 
    cin >> author; 

    cout << "Enter call number: "; 
    cin >> callNumber; 

    Book* aBook = new Book(title, callNumber, author); 
    return aBook; 
} 
else if (selection == 'R') { 
    cout << "Enter recording title: "; 
    cin >> title; 

    cout << "Enter performer: "; 
    cin >> performer; 

    cout << "Enter format: (M)P3, (W)AV, (A)IFF: "; 
    cin >> format; 

    cout << "Enter call number: "; 
    cin >> callNumber; 

    Recording* aRecording = new Recording(title, callNumber, performer,  format); 
    return aRecording; 
} 
else { 
    cout << "Incorrect selection" << endl; 
    return nullptr; 
} 

}

Holding.h

#ifndef _HOLDING_H 
#define _HOLDING_H 

class Holding { 
protected: 
int callNumber; 
char* title; 

public: 
Holding(); 
Holding(const Holding&); 
Holding(char*, int); 
virtual void print() = 0; 
virtual ~Holding(); 
}; 

#endif 

Holding.cpp

#include "Holding.h" 
#include "String.h" 

Holding::Holding() { 

} 

Holding::Holding(const Holding& copy) { 
title = new char[strlen(copy.title) + 1]; 

strcpy_s(title,sizeof(title), copy.title); 
callNumber = copy.callNumber; 
} 

Holding::Holding(char* copy, int inputCall) { 
int len = strlen(copy) + 1; 
title = new char[len]; 

strcpy_s(title, sizeof(char) * len, copy); 
callNumber = inputCall; 
} 

Holding::~Holding() { 
delete [] title; 
} 

Book.h:

#ifndef _BOOK_H 
#define _BOOK_H 

#include "Holding.h" 

class Book : public Holding { 
private: 
char* author; 

public: 
Book(); 
Book(const Book&); 
Book(char*, int, char*); 
virtual void print(); 
virtual ~Book(); 
}; 

#endif 

Book.cpp

#include <iostream> 
#include "Holding.h" 
#include "Book.h" 
#include "String.h" 

using namespace std; 

Book::Book() { 
author = nullptr; 
} 

Book::Book(const Book& copy) : Holding(copy) { 
author = new char[strlen(copy.author) + 1]; 

strcpy_s(author, sizeof(author), copy.author); 
} 

Book::Book(char* inputTitle, int inputCallNum, char* inputAuthor) : Holding(inputTitle, inputCallNum) { 
int len = strlen(inputAuthor) + 1; 
author = new char[len]; 

strcpy_s(author, sizeof(author)*len, inputAuthor); 
} 

Book::~Book() { 
delete [] author; 
} 

void Book::print() { 
cout << "BOOK: " << author << " " << title << " " << callNumber << endl; 
} 

Recording.h:

#ifndef _RECORDING_H 
#define _RECORDING_H 

#include "Holding.h" 

class Recording : public Holding { 
private: 
char* performer; 
char format; 

public: 
Recording(); 
Recording(const Recording&); 
Recording(char*, int, char*, char); 
virtual void print(); 
virtual ~Recording(); 

}; 

#endif 

Recording.cpp:

#include <iostream> 
#include "String.h" 
#include "Holding.h" 
#include "Recording.h" 

using namespace std; 

Recording::Recording() { 

} 

Recording::Recording(const Recording& copy) : Holding(copy) { 
int len = strlen(copy.performer) + 1; 
performer = new char[len]; 

strcat_s(performer, sizeof(performer)*len, copy.performer); 
format = copy.format; 
} 

Recording::Recording(char* inputTitle, int inputCallNum, char* inputPerformer, char  inputFormat) 
: Holding(inputTitle, inputCallNum) { 
int len = strlen(inputPerformer) + 1; 
performer = new char[len]; 

strcpy_s(performer, sizeof(performer)*len, inputPerformer); 
format = inputFormat; 
} 

Recording::~Recording() { 
delete [] performer; 
} 

void Recording::print() { 
cout << "RECORDING: " << title << " " << performer << " (" << format << ") " <<  callNumber << endl; 
} 
+0

這是什麼?調試螞蟻?你是否遇到異常? – nhgrif

+0

您還粘貼了兩次'Book.h'(沒有'Book.cpp') – yan

+0

我的螞蟻朋友告訴我12/11行有錯誤。不知道哪個文件。螞蟻還沒有想到。這兩行(參考這些行號的行)是什麼意思?你能弄清楚那些線是什麼,並指出這些線? – nhgrif

回答

1

cin只能使用'>>'取一個單詞。所以當你輸入「再見」時,cin爲作者分配「好」,並且由於「再見」仍然在緩衝區中,它會自動嘗試將其分配給callNumber,但由於callNumber是一個int變量,它會拋出一個壞的例外。因此,要麼只輸入一個字輸入,或更改輸入方法如下:

///cout statement here 
getline(cin, title); 
cin.ignore(); 

///cout statement here 
getline(cin, author); 
cin.ignore() 

的cin.ignore是簡單地忽略「\ n」,這是什麼回車鍵追加到輸入字符串。如果cin.ignore被遺漏,你會遇到同樣的問題,因爲'\ n'仍然在緩衝區中。