c++ - 读取字符串时崩溃

标签 c++ string crash

该程序会编译并运行,但是当我输入2个或更多条目时,它会在我输入第二个电话号码时立即崩溃。这是我输入的内容和调用堆栈:

“Library Holdings.exe中0x013A6EC6的未处理异常:
0xC0000005:访问冲突读取位置0x00000000。”

这次,它崩溃了,我无法进入第二个 call 。

这是另一起发生的崩溃,由于某种原因在不同点崩溃,但这是我遇到的最常见的崩溃

Library Holdings.exe中0x779B016E(ntdll.dll)的未处理异常:0x00000000:操作成功完成。

我想知道如何停止崩溃?

LibraryDrv.h:

#ifndef _LIBRARYDRV_H
#define _LIBRARYDRV_H

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

Holding* inputHolding();

#endif

图书馆博士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;
}

}

保持时间
#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;
}

最佳答案

cin使用“>>”时只能输入一个单词。因此,当您输入“good bye”时,cin为作者分配了“good”,并且由于“bye”仍在缓冲区中,因此它会自动尝试将其分配给callNumber,但是由于callNumber是一个int var,因此抛出了一个糟糕的异常。因此,要么只输入一个单词输入,要么将输入方法更改为以下内容:

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

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

cin.ignore只是忽略'\ n',这是回车键附加到输入字符串的内容。如果忽略了cin.ignore,则由于“\ n”仍在缓冲区中,因此您将遇到相同的问题。

关于c++ - 读取字符串时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19237743/

相关文章:

c++ - 最长的多米诺骨牌链/序列

c++ - 检查元素是否存在

c++ - 在一行中使用 vector push_back 和 "temp"结构(如果可能)

objective-c - 调用[self setValue:forKey:]时应用崩溃

ios - 使用 EXC_RESOURCE 关闭应用程序,iOS 8 GM 上的 WAKEUPS 异常

c++ - 数据类型转换问题

java - 通过套接字接收的字符串在 python 中不进行比较

javascript - 处理字符串中相同出现的字符

c# - 匹配时抓取一部分文本,去掉其余部分,因为它没用

.net - 非托管 dll 调用使 dotnet 应用程序崩溃?