我已經創建了一個按用戶輸入大小設置的數組。將數組中的元素設置爲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;
}
Bankcard是一個類或結構,對嗎?您不能將零賦給類或結構體。你將[choice-1]視爲一個指針,事實並非如此。 –
如果使用C++使用std :: vector而不是C數組。 此外,現在當從你的數組中刪除某些東西時,你將不得不釋放分配給該結構的內存。 代碼中有很多問題。 – Max
有人必須提到['std :: vector'](http://en.cppreference.com/w/cpp/container/vector)可以簡化這一點。那麼你不需要太擔心提示用戶大小和質疑用戶輸入負值(或非常大)時會發生什麼。 –