2012-10-08 100 views
1

即時編寫一個C++程序,它從外部文本文件中讀取有關書籍的信息,將信息存儲在一個數組中,並用每本書的標題和價格編寫一個新文件。在編譯時沒有錯誤,但是當我運行該程序,我收到了一堆的是出現在屏幕上的內存位置和錯誤請幫助「EXC_BAD_ACCESS(代碼2)」

「線程1:EXC_BAD_ACCESS(代碼2)」

如果它在intel 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() << "\n\n"; //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() << "\n\n"; 
    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 \n\n" << "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"; 
} 
+0

看起來好像您正在嘗試從文件中讀取多行文本而不檢查您是否已達到結尾或發生了錯誤。你不檢查打開的文件是否成功,然後在while循環中多次調用'getline'而不檢查'eof'或其他錯誤。 – peacemaker

回答

0

下面是我跳出了幾個要點:

  1. 我的口頭禪:你讀後汝等支票讀取工作。
  2. std::ios_base::eof()的使用僅用於確定之前讀取失敗的原因。
  3. 你真正的問題是:如果有超過3本書,會發生什麼? (不需要回答:我知道會發生什麼......)
  4. 專家程序員正努力爭取長時間的功能。初學者沒有機會!將你的代碼分解成消化和連貫的塊!

修復問題並重試。

+0

我檢查是否所有的閱讀工作通過打印信息存儲在變量(bookList [我] .PrintBook)。我需要分解哪些功能?我沒有意識到他們很長。 –

+0

'main()'中發生了一切有趣的事情。例如,您希望有一個函數可以讀取一本書,例如從'main()'調用。這會讓你非常清楚地指出我發現的主要問題(不知道是否還有更多的代碼:對我來說,這些代碼太麻煩了)。 –

+0

也當我註釋掉第二個while循環時程序執行完美。 –