2012-09-23 30 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); 
    //...// 
} 
相关问题