2012-09-23 3 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 비트가 설정되지 않습니다. 이것은 읽기를 시도 할 때만 발생하며 아무 것도 없습니다. 21 번째 항목 후에는 eof 비트가 설정되어 있지 않으므로 루프가 계속 진행됩니다. 쓰레기 정보를 읽어서 paInventory에 저장하려고 시도하지만 배열의 크기는 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); 
    //...// 
} 
관련 문제