用cin阅读后崩溃

时间:2013-08-07 01:28:51

标签: c++ crash cin

由于某种原因,程序总是在第二次主要呼叫inputHolding()时崩溃并尝试获取呼叫号码(输入呼叫号码然后崩溃)。我该如何修复此崩溃?下面是调用堆栈的图片以及它在我的代码中崩溃的位置:http://i.imgur.com/zWJ17NC.png

#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;
}
}

0 个答案:

没有答案