阅读字符串时崩溃

时间:2013-10-08 01:48:52

标签: c++ string crash

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

crash

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

这次它在我进入第二个电话#。

之前崩溃了

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

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

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

1 个答案:

答案 0 :(得分:1)

cin在使用'&gt;&gt;'时只能使用一个单词。所以当你输入“再见”时,cin为作者分配“好”并且因为“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'仍在缓冲区中。