将数组中的元素设置为0或NULL

时间:2012-12-12 19:44:30

标签: c++ arrays visual-c++

我创建了一个按用户输入调整大小的数组。

    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;

上收到此错误
  

IntelliSense:没有运算符“=”匹配这些操作数

这是完整的代码。

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; 
}

2 个答案:

答案 0 :(得分:2)

除非你没有显示其他代码,否则目前这个for循环甚至不应该运行:

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

因为spotInArray0i始于n(并且您正在递增i)。

你确定它在那条线路上失败了吗?

答案 1 :(得分: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 );