2011-11-23 22 views
-2

我的代碼應該從文本文件中讀取16行,並將它們傳遞到4個對象的數組中,每個對象具有4個屬性。我遇到的問題是,儘管在將文本細節傳遞給數組時,一切似乎都正常工作,但數組中最後一個對象的第一個數組元素不是應該有的!我真的被卡住了!C++ I/O流文本文件到數組問題

我的代碼是:

#include <iostream> 
#include <fstream> 
#include <sstream> 

using namespace std; 

int CDsize; 

class CD { 
public: 
    // constructors 
    CD(); 
    CD(string arist, string title, int year, double price); 

    // getter methods 
    string getArtist() const { return this->artist; } 
    string getTitle() const { return this->title; } 
    int getYear() const { return this->year; } 
    double getPrice() const { return this->price; } 

    // setter methods - inline functions 
    void setArtist(const string artist) { this->artist = artist; } 
    void setTitle(const string title) { this->title = title; } 
    void setYear(const int year) { this->year = year; } 
    void setPrice(const double price) { this->price = price; } 

    // option methods 
private: 
    string artist; 
    string title; 
    int year; 
    double price; 
}; 

/*Text menu option 1/* 
void printallcds(CD MAX_CDS[]) 
{ 
    int d; 
    for (d=0; d<=CDsize; d++) 

    { 
    cout << "CD Number : " << d << "/ Artist : " << MAX_CDS[d].getArtist() << "/ 
Title : " << cout << MAX_CDS[d].getTitle() << "/ Year of Release : " << 
MAX_CDS[d].getYear() << "/ Price : " << 
    cout << MAX_CDS[d].getPrice() << endl; 
    } 
}*/ 

int main() { 
    CD MAX_CDS[3]; 
    int CDsize = (sizeof(MAX_CDS)/sizeof(MAX_CDS[0])); 
    ifstream CDfile("mystock.txt"); 
    string data; 
    int yeardata; 
    double pricedata; 
    int i; 

    for (i = 0; i < 4; i++) { 
     getline(CDfile, data); 
     MAX_CDS[i].setArtist(data); 

     getline(CDfile, data); 
     MAX_CDS[i].setTitle(data); 

     getline(CDfile, data); 
     stringstream yearstream(data); 
     yearstream >> yeardata; 
     MAX_CDS[i].setYear(yeardata); 

     getline(CDfile, data); 
     stringstream pricestream(data); 
     pricestream >> pricedata; 
     MAX_CDS[i].setPrice(pricedata); 
    } 

    CDfile.close(); 

    // testing 
    cout << MAX_CDS[3].getArtist() << endl; // error !!! 
    cout << MAX_CDS[3].getTitle() << endl; 
    cout << MAX_CDS[3].getYear() << endl; 
    cout << MAX_CDS[3].getPrice() << endl; 

    return 0; 
} 

// constructors implementation 
CD::CD() {} 

CD::CD(string artist, string title, int year, double price) { 
    this->artist = artist; 
    this->title = title; 
    this->year = year; 
    this->price = price; 
} 
} 
+0

你能說明文件內容是怎麼樣的,至少有幾行? –

+0

爲什麼你要聲明CD MAX_CDS [3];大小3? – Viruzzo

+0

@sixlettervariables +1 –

回答

2

你只需要在MAX_CDS 3項(見CD MAX_CDS[3];)的空間,但如果引用在錯誤的4項。

MAX_CDS[3] // Actually represents the 4th item 

在C++中從0開始計數。

因此,要引用第3項,在您的情況下最後項目,請使用MAX_CDS[2]MAX_CDS[CDSize-1]

cout << MAX_CDS[CDSize-1].getArtist() << endl; 
cout << MAX_CDS[CDSize-1].getTitle() << endl; 
cout << MAX_CDS[CDSize-1].getYear() << endl; 
cout << MAX_CDS[CDSize-1].getPrice() << endl; 

重新讀你的問題,你可能想要更多的項目!

CD MAX_CDS[4]; // Now you have 4 items available: indexed 0, 1, 2, and 3 
+0

傳奇!!!非常感謝你們:) –