2015-02-23 93 views
0

ostream的問題 我的ostream操作< <似乎並沒有被工作或者ostream的運營商<<不能正常工作

+0

嗯,我會想象(INT)鍵[I]可以給你,因爲大小不匹配的垃圾數據... – hanshenrik 2015-02-23 19:40:00

+0

我的尺寸爲512,大小進入getHash的是512.你會建議什麼?謝謝 – kris 2015-02-23 19:44:39

回答

2

東西有一個在功能Customer::getHash一個邏輯錯誤。這可能無法解決您的問題,但無論如何它都應該得到解決。

int Customer::getHash(int hash) 
{ 
    string key = getLastname(); 
    cout<<"key: "<<key<<endl; 
    // getFirstname(); 
    // getID(); 
    int i = 0; 
    // int j = 0; 
    // int k = 0; 
    for (i = 0; i < key.length(); i++) 
    { 
     i += (int)key[i]; // Problem. 
          // At this time, i may be greater than key.length(). 
    } 
    // getFirstname(); 
    // getID(); 
    return i = i % hash; 

} 

您可以通過使用其他變量來修復它以保留臨時哈希值。

int Customer::getHash(int hash) 
{ 
    string key = getLastname(); 
    cout<<"key: "<<key<<endl; 
    int tempHash = 0; 
    int i = 0; 
    for (i = 0; i < key.length(); i++) 
    { 
     tempHash += (int)key[i]; 
    } 
    return tempHash % hash;  
} 

更新

在您發佈的代碼,您在功能

istream &operator >> (istream &in, Customer &obj) 

作爲一個副作用註釋掉return語句的

while (inputFile >> newCustomer) 
行爲

未定義。

取消註釋在該函數的線

//return in; 

。這將解決另一個錯誤。希望這是最後一個。

更新2

您正在閱讀的while循環太多的信息。

// This line reads all the information of one customer 
while (inputFile >> newCustomer) 
{ 
    //inputFile >> newCustomer; 
    string lastname; 

    // PROBLEM 
    // Now you are reading data corresponding to the next customer. 

    getline (inputFile, lastname, ' '); 
    while (inputFile.peek() == ' ') 
     inputFile.get(); 

    string firstname; 
    getline (inputFile, firstname, ' '); 
    while (inputFile.peek() == ' ') 
     inputFile.get(); 

    string id; 
    getline (inputFile, id); 

    buildCustomerList(cHeadPtr, cTailPtr, lastname, firstname, id); 
    customer.insert(newCustomer); 
    //cout<<lastname<<endl; 
    //cout<<firstname<<endl; 
    //cout<<id<<endl; 
} 

將其更改爲:

while (inputFile >> newCustomer) 
{ 
    string lastname = newCustomer.getLastname(); 
    string firstname = newCustomer.getFirstname(); 
    string id = newCustomer.getID(); 

    buildCustomerList(cHeadPtr, cTailPtr, lastname, firstname, id); 
    customer.insert(newCustomer); 
} 
+0

哦,我看到我的錯誤。謝謝!我的for循環是否正常工作?在我看來,有一些非常簡單的東西,在這裏,但我無法抓住它!謝謝 – kris 2015-02-23 19:50:55

+0

@kris,'for'循環對我來說看起來還不錯。 – 2015-02-23 19:52:02

+0

@R Sahu,我發佈了我的完整程序。我在想,也許當我正在閱讀文件並設置我的數據時,出現了什麼問題? – kris 2015-02-23 19:55:53