私のメイン関数では、inputHolding を 5 回呼び出します。いくつかのループを経て、通常は 3 番目のループで callNumber を読み込もうとすると、ヒープが壊れているというエラーが表示されます。このクラッシュを修正するにはどうすればよいですか?
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* muhbooksie = new Book(title, callNumber, author);
return muhbooksie;
}
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* muhbooksie = new Recording(title, callNumber, performer, format);
return muhbooksie;
}
else {
cout << "Incorrect selection" << endl;
return nullptr;
}
}
Book.cpp:
#include <iostream>
#include "Holding.h"
#include "Book.h"
#include "String.h"
using namespace std;
Book::Book() {
}
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;
}