请帮忙"EXC_BAD_ACCESS(Code 2)"
Please Help "EXC_BAD_ACCESS(Code 2)"
im 编写一个C++程序,该程序从外部文本文件中读取有关书籍的信息,将信息存储在数组中,并写入一个新文件,其中包含每本书的标题和价格。 在编译时没有错误,但是当我运行程序时,我得到了一堆在屏幕上弹出的内存位置和一个错误
"线程 1:EXC_BAD_ACCESS(代码 2)"
如果重要,我在英特尔 Mac 上使用 Xcode。下面的代码包含该问题的一些屏幕截图。谢谢
守则:
//main.cpp
include <iostream>
include <fstream>
include "Book.h"
include "textbook.h"
include "Name.h"
using namespace std;
int main()
{
string title, authorf, authorl, publisher, isbn, subject;
int pages, i = 0, j = 0;
float price, rentalp;
char code;
static int bookCount = 0, textbookCount = 0;
ifstream file1, file2;
ofstream newFile;
file1.open("/Users/TVaughn/Documents/School Stuff/Fall 2012/CS 246/In Class Programs/Book/Book/Data.txt", ios::in); //Opening the data.txt file to read from.
newFile.open("/Users/TVaughn/Documents/School Stuff/Fall 2012/CS 246/In Class Programs/Book/Book/Title.txt", ios::out); //Creating a new file to store titles & prices of all the books.
book bookList[3];
while (!file1.eof()) {
getline (file1, title, ',');
getline (file1, authorf, ',');
getline (file1, authorl, ',');
getline (file1, publisher, ',');
getline (file1, isbn, ',');
file1 >> pages;
file1.ignore();
file1 >> price;
file1.ignore();
file1 >> code;
file1.ignore(10, 'n');
book b1(title, authorf, authorl, publisher, pages, isbn, price, code);
bookList[i] = b1; //initalizing the first element of the array to b1
newFile << "Title: " << bookList[i].getTitle() << "n" << "Price: " << bookList[i].getPrice() << "nn"; //storing the title and price of all the books in a new file called titles.
bookList[i].PrintBook();
i++;
bookCount++;
}
file1.close();
file2.open("/Users/TVaughn/Documents/School Stuff/Fall 2012/CS 246/In Class Programs/Book/Book/TextbookData.txt", ios::in); //Opening the TextbookData.txt file to read from.
textbook TbookList[2];
while (!file2.eof()) {
getline (file2, title, ','); //retrieving info from data.txt and storing that info into variables so the variable may be passed as arguments in my textbook object b2.
getline (file2, authorf, ',');
getline (file2, authorl, ',');
getline (file2, isbn, ',');
file2 >> pages;
file2.ignore();
file2 >> price;
file2.ignore();
getline(file2, subject, ',');
file2 >> code;
file2 >> rentalp;
file2.ignore(10, 'n');
textbook b2(title, authorf, authorl, publisher, pages, isbn, price, code, subject, rentalp);
TbookList[j] = b2; //initalizing the first element of the array to b2.
newFile << "Title: " << bookList[j].getTitle() << "n" << "Price: " << bookList[j].getPrice() << "nn";
TbookList[j].PrintBook();
j++;
textbookCount++;
}
file2.close();
newFile.close();
cout << "Number of books: " << bookCount << "nNumber of textbooks: " << textbookCount << endl;
book b3("C++ Data Structures", "Nell B.", "Dale", "Jones & Bartlett Publishers", 781, "9780763741587", 107.67, 'A'); //This line is used to test the exception handeling.
try {
b3.setPrice(price);
b3.setPages(pages);
}
catch (book::negativeNumber) {
cout << "Negative price was entered nn" << "End of program n";
}
return 0;
}
//book.h
#include <iostream>
#include "Name.h"
#include <string>
using namespace std;
#ifndef BOOK_H
#define BOOK_H
class book
{
private:
string title;
Name aurthor;
string publisher;
string ISBN;
int pages;
float price;
char code;
public:
class negativeNumber{};
void setTitle(string);
void setAurthor(string f, string l);
void setPublisher(string);
void setISBN(string);
void setPages(int);
void setPrice(float);
void setCode(char);
string getTitle();
Name getAurthor();
string getPublisher();
string getISBN();
int getPages();
float getPrice();
char getCode();
void PrintBook();
book(); //default constructor
book(string, string, string, string, int, string, float, char);
~book(); //Destructor
};
#endif
//Book.cpp
#include <iostream>
#include <string>
#include "Book.h"
#include "Name.h"
using namespace std;
book::book() {
title = "Southern Charm";
Name::Name();
publisher = "Impact Publishers";
pages = 223;
ISBN = "234-232-10009045";
price = 14.99;
code = 'L';
}
book::book(string the_title, string f, string l, string the_pub, int page_num, string the_ISBN, float the_price, char the_c)
{
title = the_title;
//Name(f, l);
aurthor.setfName(f);
aurthor.setlName(l);
publisher = the_pub;
pages = page_num;
ISBN = the_ISBN;
price = the_price;
code = the_c;
}
book::~book()
{
}
void book::setTitle(string t)
{
title = t;
}
void book::setAurthor(string f, string l)
{
aurthor.setfName(f);
aurthor.setlName(l);
}
void book::setPublisher(string pub)
{
publisher = pub;
}
void book::setISBN(string ISBN_num)
{
ISBN = ISBN_num;
}
void book::setPages(int p) {
if (pages > 0)
pages = p;
else
throw negativeNumber();
}
void book::setPrice(float cost) {
if (price >= 0) //This is called exception handling.
price = cost;
else
throw negativeNumber();
}
void book::setCode(char c) {
code = c;
}
string book::getTitle() {
return title;
}
Name book::getAurthor(){
return aurthor;
}
string book::getPublisher() {
return publisher;
}
string book::getISBN(){
return ISBN;
}
int book::getPages() {
return pages;
}
float book::getPrice() {
return price;
}
char book::getCode() {
return code;
}
void book::PrintBook() {
cout << "Title: " << getTitle() << "n" << "Aurthor: ";
getAurthor().printName();
cout << "Publisher: " << getPublisher() << "n" << "Pages: " << getPages() << "n" << "ISBN: " << getISBN() << "n" << "Price: " << getPrice() << "n" << "Code: " << getCode() << "n n n";
}
以下是我突然想到的几点:
- 我的口头禅:你应该检查阅读在你阅读之后起作用。
- 使用
std::ios_base::eof()
只能确定上一次读取失败的原因。 - 你实际的问题是:如果超过3本书会发生什么?(无需回答:我知道会发生什么...
- 专家程序员很难正确获得长函数。初学者没有机会!将您的代码拆分为易于理解和连贯的块!
修复问题并重试。
相关文章:
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- Visual Studio Code - C++ Debugger 無法啟動
- Visual Studio Code "undefined reference to `WinMain@16'"
- VS Code "command":"make"与终端窗口中的命令行"make"不同
- 使用VS Code和CMake Tools运行自定义命令
- 修改 VS Code 中的默认C++代码段
- 如何配置Visual Studio Code以使用cygwin,cmake和gcc进行调试
- std::to_string - 'to_string' 不是 'std' 的成员 - Visual Studio Code 1.42.0
- VS Code C++:不准确的系统包括路径错误(wchar.h,boost/lambda/lambda.hpp)
- C++新手,想知道如何使用VS code 2019播放音频文件
- "Unable to start debugging. No process is associated with this object." - 在Visual Studio Code中使用GDB
- 如何在Visual Studio Code中重命名我的a.exe文件?
- C++ Visual Studio Code 的设置不起作用
- 以某种方式告诉编译器"Do not process line of code"
- 在VS Code中编译C / C ++时如何禁用自动创建EXE文件?
- 将Qt与Visual Studio Code(Windows)一起使用
- 可视化 使用 VS Code 查找C++应用程序中的内存泄漏
- 将数组信息存储到 c++ 向量中有一个"Access violation reading location"
- 将Visual Studio Code路径设置为.clang_format文件
- C++ EXC_BAD_ACCESS (code=1 access=0x0)