C语言 如何从阵列中删除信息

bvjxkvbb  于 2022-12-17  发布在  其他
关注(0)|答案(2)|浏览(139)

我正在尝试从这个创建的数组Customer中删除信息。该数组包含信息,名、姓、checkingbalance、savingsbalance、moneymarketbalance、帐号和余额。这些信息将被保存并在以后调用。我必须能够删除它们,并且我希望使用帐号来完成此操作。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Customer
{
    char FirstName[100];
    char LastName[100];
    int CheckingBalance;
    int SavingsBalance;
    int MoneyMarketBalance;
    int AccountNumber;
    float Balance;
};

struct Customer customer[20]; 

int NumOfAccounts;
 
void NewAccount()
{
    char FirstName[100];
    char LastName[100];
    int CheckingBalance;
    int SavingsBalance;
    int MoneyMarketBalance;
    int AccountNumber;
    float Balance = 0;

    printf("You have chosen that you want to become a new member of Global Bank! Please enter your first name:\n");
    scanf("%s", FirstName);
    printf("Thank you! Now please enter your last name:\n");
    scanf("%s", LastName);
    printf("Thank you %s %s for entering your name!", FirstName, LastName);
    printf("\nEnter how much would you like to deposit into your checking account: \n");
    scanf("%d", &CheckingBalance); 
    printf("Thank you %s for your initial deposit of %d!", FirstName, CheckingBalance);
    printf("\nEnter how much would you like to deposit into your savings account: \n");
    scanf("%d", &SavingsBalance); 
    printf("Thank you %s for your initial deposit of %d!", FirstName, SavingsBalance);
    printf("\nEnter how much would you like to deposit into your Money Market account: \n");
    scanf("%d", &MoneyMarketBalance); 
    printf("Thank you %s for your initial deposit of %d!", FirstName, MoneyMarketBalance);
    printf("\nNow choose your custom account number: \n");
    scanf("%d", &AccountNumber);
    printf("\nWelcome %s %s to Global Bank. Your account number is %d your initial balance in your checking is %d and your initial balance in your savings is %d.", FirstName, LastName, AccountNumber, CheckingBalance, SavingsBalance);
    printf("\n\nIf you would like to check the bank balance of your account press '2'. \nIf you would like to close your account, press '3'. \nIf you would like to make a deposit, press '4'. \nIf you would like to make a withdrawal, press '5'. \nIf you would like to check all members last names, press '6'. \nIf you would like to exit press '7'.\n"); 

    strcpy(customer[AccountNumber-1].FirstName,FirstName);
    strcpy(customer[AccountNumber-1].LastName,LastName);
    (customer[AccountNumber-1].AccountNumber=AccountNumber);
    (customer[AccountNumber-1].CheckingBalance=CheckingBalance);
    (customer[AccountNumber-1].SavingsBalance=SavingsBalance);
    (customer[AccountNumber-1].MoneyMarketBalance=MoneyMarketBalance);

    NumOfAccounts++;
}

void CloseAccount()
{
char FirstName[100];
char LastName[100];
int CheckingBalance;
int SavingsBalance;
int MoneyMarketBalance;
int AccountNumber;
int i;
printf("\nYou have chosen that you want to close your account. Enter your account number: \n");
scanf("%d", &customer->AccountNumber);
while (1)
{
memset(&customer[AccountNumber-1], 0, sizeof customer[AccountNumber-1]);
break;
}
printf("\nYou have closed your account. We are sad to see you go.\n");
}
}

int main()
{

    int choice;
    printf("Welcome to the Global Bank app! \nIf you would like to become a new member, press '1'. \nIf you would like to check the bank balance of your account press '2'. \nIf you would like to close your account, press '3'. \nIf you would like to make a deposit, press '4'. \nIf you would like to make a withdrawal, press '5'. \nIf you would like to check all members last names, press '6'. \nIf you would like to exit press '7'.\n"); 
    while(1){
        scanf("%d", &choice);
        switch(choice)
        {
            case 1:
            NewAccount();
            break;

            case 2:
            case 3:
                CloseAccount();
            case 4:
            break;
            case 5:

            break;
            case 6:

            case 7:
            printf("Have a good day and thank you for banking with Global Bank!\n");
            exit(0);
        }
    }
}

目前我想不出办法来做(可能是因为太晚了)。还有这些是什么:

strcpy(customer[AccountNumber-1].LastName,LastName);
(customer[AccountNumber-1].AccountNumber=AccountNumber);

我不知道如何在网上搜索他们,我想这有点妨碍我。
附言:
如果问题的格式有任何问题,请不要关闭这个帖子,我一直在处理程序的其他部分,我会在醒来时修复格式。谢谢

z0qdvdin

z0qdvdin1#

如何从数组中删除信息(?)
一旦定义了数组,它的大小就不能改变,因此数组元素不能被删除。
OP可以简单地使用NumOfAccounts--来阻止对最后一个帐户的访问。
如果要删除较早的索引帐户,可能需要将最后一个帐户复制到该帐户,然后复制NumOfAccounts--

更深

代码可以覆盖数组:

memset(array, 0, sizeof array);

...或数组元素:

memset(&array[i], 0, sizeof array[i]);

如果该内存不是以后可能读取的,则调用可以被优化掉。然而我怀疑OP的建议,这不是一个问题。

sqougxex

sqougxex2#

我想出来了。我只是手动设置每个值为空。我相信有更好的方法,但这已经足够好了。

void CloseAccount()
{
char FirstName[100];
char LastName[100];
int CheckingBalance;
int SavingsBalance;
int MoneyMarketBalance;
int AccountNumber;
int i;
printf("\nYou have chosen that you want to close your account. Enter your account number: \n");
scanf("%d", &customer->AccountNumber);
while (AccountNumber=customer[AccountNumber-1].AccountNumber)
{
customer[AccountNumber-1].CheckingBalance = NULL;
customer[AccountNumber-1].SavingsBalance = NULL;
customer[AccountNumber-1].MoneyMarketBalance = NULL;
break;
}
printf("\nYou have closed your account. We are sad to see you go.\n");
}

相关问题