2012-12-12 67 views
2

我已經創建了一個按用戶輸入大小設置的數組。將數組中的元素設置爲0或NULL

int spotInArray = 0; 
    Bankcard* a = NULL; 
    int n;   
    cout << "Enter number of cards" << std::endl; 
    cin >> n; 
    a = new Bankcard[n]; 

這是我的代碼我已經在我的開關中讓用戶選擇他們想要刪除的卡。

  int choice; 
      cout << "Enter Number of card you would like to delete: " << endl; 
      cin >> choice; 
       for (int i = n; i < spotInArray; i++) 
       { 
        a[n] = a[n + 1]; 
        a[choice - 1] = 0; 
       } 

我在[choice - 1] = 0上得到這個錯誤;

智能感知:沒有運營商 「=」 匹配這些操作數

下面是完整的代碼。

int main() 
{ 
//Bankcard bankcard1("Blue Card", 1, .05, 3000.00, 430.32, 200.35, 124.00); 
int spotInArray = 0; 
Bankcard* a = NULL; // Pointer to int, initialize to nothing. 
int n;   // Size needed for array 
cout << "Enter number of cards" << std::endl; 
cin >> n;  // Read in the size 
a = new Bankcard[n]; // Allocate n ints and save ptr in a. 
//for (int i=0; i<n; i++) { 
//a[i] = bankcard1; 
//bankcard1.show(); 
//} 



int choice; 

showMenu(); 

cin >> choice; 

while (choice != 6) 
{ 
    switch(choice) 
    { 
     case 1 : { 
        string productName; 
        int cardNum; 
        double interestRate; 
        double maxLimit; 
        double outstandingBalance; 
        double purchaseAmount; 
        double paymentAmount; 
        cout << "Enter Card Name(No spaces, no special characters)" << std::endl; 
        cin >> productName; 

        cout << "Enter Number of Card" << std::endl; 
        cin >> cardNum; 

        cout << "Enter interest Rate" << std::endl; 
        cin >> interestRate; 

        cout << "Enter Max Limit" << std::endl; 
        cin >> maxLimit; 

        cout << "Enter Outstanding Balance" << std::endl; 
        cin >> outstandingBalance; 

        cout << "Enter Purchase Amount" << std::endl; 
        cin >> purchaseAmount; 

        cout << "Enter Payment Amount" << std::endl; 
        cin >> paymentAmount; 
        Bankcard bankcard1(productName, cardNum, interestRate, maxLimit, outstandingBalance, purchaseAmount, paymentAmount); 
        a[spotInArray] = bankcard1; 
        spotInArray++; 
        break; 
        } 

     case 2 : update(); 
        break; 
     case 3 : { 
         int choice; 
         cout << "Enter Number of card you would like to delete: " << endl; 
         cin >> choice; 
         for (int i = 0; i < n; i++) 
         { 
          a[n] = a[n + 1]; 
          a[choice - 1] = 0; 
         } 
        } 
        deleteCard(); 
        break; 
     case 4 : overLoad(); 
        break; 
     case 5 : { 
         for (int i = 0; i < spotInArray; i++) 
         { 
          cout << a[i]; 
         } 
        } 
        break; 
     case 6 : exit(); 
        break; 

    } 
    showMenu(); 
    cout << endl; 
    cin >> choice; 
}; 

std::cin.get(); 
std::cin.get(); 
    return 0; 
} 
+0

Bankcard是一個類或結構,對嗎?您不能將零賦給類或結構體。你將[choice-1]視爲一個指針,事實並非如此。 –

+0

如果使用C++使用std :: vector而不是C數組。 此外,現在當從你的數組中刪除某些東西時,你將不得不釋放分配給該結構的內存。 代碼中有很多問題。 – Max

+1

有人必須提到['std :: vector'](http://en.cppreference.com/w/cpp/container/vector)可以簡化這一點。那麼你不需要太擔心提示用戶大小和質疑用戶輸入負值(或非常大)時會發生什麼。 –

回答

2

除非是你還沒有表現出其他的代碼,目前本作循環甚至不應該運行:

 for (int i = n; i < spotInArray; i++) 
     { 
      a[n] = a[n + 1]; 
      a[choice - 1] = 0; 
     } 

因爲spotInArray0i開始在n(和你遞增i)。

你確定它在該行失敗嗎?

+0

他甚至沒有達到這一點。在他編譯之前,它會遇到智能感知錯誤。 –

+0

只需添加整個main()代碼即可。 – eek

+0

這是我使用的代碼示例。 (int i = index; i eek

1

有些代碼不正確。首先,a[n]無效。大小爲n的數組的有效索引爲0到n-1。避免處理你自己的記憶。在這種情況下使用std::list(如果頻繁移除)。

std::list<Bankcard> bCards(n); 

// Take input to for the cardToRemove. 

bCards.erase(bCards.begin() + cardToRemove); 
+0

我得到:智能感知:沒有操作符「+」匹配這些操作數 – eek

相關問題