2012-09-23 29 views
-3

我正在學習C++,並且正在處理一個程序,該程序一直給我一個'指針被釋放未分配'錯誤。這是一個從txt文件輸入數據的雜貨店程序,然後用戶可以輸入項目#&數量。我已經讀過類似的問題,但是拋棄我的是'指針'問題。如果有人能看一眼並幫助我,我將不勝感激。我在Mac上使用Netbeans IDE 7.2。 我只是張貼我迄今爲止的整件作品。謝謝。C++運行錯誤:被釋放的指針未分配

#include <iostream> 
#include <fstream> 
#include <string> 
#include <vector> 

using namespace std; 


class Product 
{ 
    public: 
     // PLU Code 
    int getiPluCode() 
    { 
    return iPluCode; 
    } 

    void setiPluCode(int iTempPluCode) 
    { 
    iPluCode = iTempPluCode; 
    } 

    // Description 
    string getsDescription() 
    { 
    return sDescription; 
    } 

    void setsDescription(string sTempDescription) 
    { 
    sDescription = sTempDescription; 
    } 

    // Price 
    double getdPrice() 
    { 
    return dPrice; 
    } 

    void setdPrice(double dTempPrice) 
    { 
    dPrice = dTempPrice; 
    } 

    // Type..weight or unit 
    int getiType() 
    { 
    return iType; 
    } 

    void setiType(int iTempType) 
    { 
    iType = iTempType; 
    } 

    // Inventory quantity 
    double getdInventory() 
    { 
    return dInventory; 
    } 

    void setdInventory(double dTempInventory) 
    { 
    dInventory = dTempInventory; 
    } 




    private: 
     int iPluCode; 
     string sDescription; 
     double dPrice; 
     int iType; 
     double dInventory; 
}; 


int main() 
{ 

    Product paInventory[21]; // Create inventory array 
    Product paPurchase[21]; // Create customer purchase array 

    // Constructor to open inventory input file 
    ifstream InputInventory ("inventory.txt", ios::in); 
     //If ifstream could not open the file 
    if (!InputInventory) 
    { 
     cerr << "File could not be opened" << endl; 
     exit (1); 
    }//end if 

    int x = 0; 

    while (!InputInventory.eof()) 
    { 
     int iTempPluCode; 
     string sTempDescription; 
     double dTempPrice; 
     int iTempType; 
     double dTempInventory; 

     InputInventory >> iTempPluCode >> sTempDescription >> dTempPrice >> iTempType >> dTempInventory; 

     paInventory[x].setiPluCode(iTempPluCode); 
     paInventory[x].setsDescription(sTempDescription); 
     paInventory[x].setdPrice(dTempPrice); 
     paInventory[x].setiType(iTempType); 
     paInventory[x].setdInventory(dTempInventory); 

     x++; 

    } 

    bool bQuit = false; 
    //CREATE MY TOTAL VARIABLE HERE! 

    int iUserItemCount = 0; 
    do 
    { 
     int iUserPLUCode; 
     double dUserAmount; 
     double dAmountAvailable; 
     int iProductIndex = -1; 
     //CREATE MY SUBTOTAL VARIABLE HERE! 

     while(iProductIndex == -1) 
     { 
      cout<<"Please enter the PLU Code of the product."<< endl; 

      cin>>iUserPLUCode; 

      for(int i = 0; i < 21; i++) 
      { 
      if(iUserPLUCode == paInventory[i].getiPluCode()) 
      { 
       dAmountAvailable = paInventory[i].getdInventory(); 
       iProductIndex = i; 
      } 
      } 

         //PLU code entry validation 
      if(iProductIndex == -1) 
      { 
       cout << "You have entered an invalid PLU Code."; 
      } 
     } 



     cout<<"Enter the quantity to buy.\n"<< "There are "<< dAmountAvailable << "available.\n"; 
     cin>> dUserAmount; 

     while(dUserAmount > dAmountAvailable) 
     { 
     cout<<"That's too many, please try again"; 
     cin>>dUserAmount; 
     } 

     paPurchase[iUserItemCount].setiPluCode(iUserPLUCode);// Array of objects function calls 
     paPurchase[iUserItemCount].setdInventory(dUserAmount); 
     paPurchase[iUserItemCount].setdPrice(paInventory[iProductIndex].getdPrice());  
     paInventory[iProductIndex].setdInventory(paInventory[iProductIndex].getdInventory() - dUserAmount);  

      iUserItemCount++; 

     cout <<"Are you done purchasing items? Enter 1 for yes and 0 for no.\n"; 
     cin >> bQuit; 

    //NOTE: Put Amount * quantity for subtotal 
    //NOTE: Put code to update subtotal (total += subtotal) 

    // NOTE: Need to create the output txt file! 

    }while(!bQuit); 




    return 0; 

} 
+0

如果您的inventory.txt中有超過21個項目,您將遇到麻煩。 –

+0

iUserItemCount永遠不會增加。 –

+0

感謝您的快速回復。我再次檢查了我的txt文件,並有21行數據輸入。 –

回答

2

iUserItemCount從不初始化。當您將其用作索引時,您會調用未定義的行爲。

+0

我加了iUserItemCount = 0;謝謝。編譯好,但仍然得到原始錯誤。 –

0

因爲您使用靜態分配的數組,所以您可能在數組結尾後偶然發現了數據。

您聲明該文件有21個條目,但是eof條件會發生什麼?如果您讀取最後一個條目,則該流仍然沒有設置eof位。這隻發生在你嘗試閱讀並沒有任何東西時。 第21次輸入後,循環仍然繼續,因爲eof位未設置。它讀取垃圾信息並嘗試將其存儲到paInventory [21]中,但該數組的大小僅爲21。

//after last read x=21 and eof not set 
while (!InputInventory.eof()) 
{ 
    //first read causes eof to be set 
    InputInventory >> iTempPluCode >> sTempDescription >> dTempPrice >> iTempType >> dTempInventory; 

    //Accessing paInventory[21] here which causes your error 
    paInventory[x].setiPluCode(iTempPluCode); 
    //...// 
} 
相關問題