Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将数组中的元素设置为0或NULL_C++_Arrays_Visual C++ - Fatal编程技术网

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

C++ 将数组中的元素设置为0或NULL,c++,arrays,visual-c++,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 spotInArray=0; 银行卡*a=空; int n; cout n; a=新银行

我已经创建了一个数组,它的大小由用户输入决定

    int spotInArray = 0;
    Bankcard* a = NULL;
    int n;           
    cout << "Enter number of cards" << std::endl;
    cin >> n;
    a = new Bankcard[n];
int spotInArray=0;
银行卡*a=空;
int n;
cout n;
a=新银行卡[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;
                }
int选择;
不能选择;
for(int i=n;i
我在[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; 
}
intmain()
{
//银行卡银行卡1(“蓝卡”,1.05、3000.00、430.32、200.35、124.00);
int spotInArray=0;
银行卡*a=NULL;//指向int的指针,初始化为nothing。
int n;//数组需要的大小
cout n;//读入大小
a=新银行卡[n];//分配n个整数并将ptr保存在a中。
//对于(int i=0;i>choice;
while(选项!=6)
{
开关(选择)
{
案例1:{
字符串名称;
int-cardNum;
双酯;
双最大极限;
双倍余额;
购买量加倍;
双倍支付金额;
cout产品名称;
cout-cardNum;
不能进行酯交换;
cout-maxLimit;
无法平衡;
购买量;
无法支付金额;
银行卡bankcard1(productName、cardNum、interestRate、maxLimit、未结清余额、purchaseAmount、paymentAmount);
a[spotInArray]=银行卡1;
spotInArray++;
打破
}
案例2:更新();
打破
案例3:{
智力选择;
不能选择;
对于(int i=0;i
除非您还没有显示其他代码,否则当前此for循环甚至不应运行:

        for (int i = n; i < spotInArray; i++)
        {
            a[n] = a[n + 1];
            a[choice - 1] = 0;
        }
for(int i=n;i
因为
spotInArray
0
并且
i
n
开始(并且您也在增加
i


你确定它在那一行失败了吗?

代码中几乎没有不正确的地方。首先,
a[n]
是无效的。大小为
n
的数组的有效索引是0到n-1。避免自己处理内存。在这种情况下,使用
std::list

std::列表bcard(n);
//将输入带到,以便卡移动。
b cards.erase(b cards.begin()+cardToRemove);

Bankcard是一个类或结构,对吧?你不能给一个类或结构赋值零。你在处理一个[choice-1]就好像它是一个指针,它不是。如果使用C++:使用STD::vector代替C数组。而且,当现在从数组中移除一些东西时,你必须释放分配给该结构的内存。代码中有很多问题。有人不得不提到,可以简化这一点。那么你不必太担心。提示用户输入大小,并询问当用户输入负(或非常大)大小时会发生什么情况。Bankcard是一个类,数组中填充了用户创建的对象。我曾尝试学习向量,但我对它们不太了解。建议您使用简单的整型返回指针和数组的基本知识(int、char等)。一旦你彻底了解了如何使用数组和指针,你的错误就会显而易见。添加完整的代码并没有真正的帮助。你需要解决其他人已经指出的问题。他甚至还没有达到这一点。在他编译之前,它就遇到了IntelliSense错误。这是我使用的代码示例。例如(int i=index;i std::list<Bankcard> bCards(n); // Take input to for the cardToRemove. bCards.erase( bCards.begin() + cardToRemove );