Im trying to delete information from this created array Customer. The array contains the information, firstname, lastname, checkingbalance,savingsbalance, moneymarketbalance,accountnumber and balance. The information is saved and called upon later. I have to be able to delete them and I want to do it using the account number.
#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);
}
}
}
Currently I cant think of a way to do it (probably because its super late). Also what are these:
strcpy(customer[AccountNumber-1].LastName,LastName);
(customer[AccountNumber-1].AccountNumber=AccountNumber);
called? I don't know how to search for them on the internet and I think its hindering me a little.
PS:
If there is anything wrong with the formatting of the question or something please dont close this post ive been up working on the other parts of the program and I will fix the formatting when I wake up. Thank you
CodePudding user response:
How to delete information from array (?)
Once an array is defined, its size cannot change. Thus an array element cannot get deleted.
OP could simply use NumOfAccounts--
to prevent access to the last account.
If an earlier indexed account is to be deleted, perhaps copy the last one to it and then NumOfAccounts--
.
Deeper
Code can overwrite an array:
memset(array, 0, sizeof array);
... or an array element:
memset(&array[i], 0, sizeof array[i]);
If that memory is not potential read later, the call could be optimized out. Yet I suspect for OP's proposes, this is not a concern.
CodePudding user response:
I figured it out. I just manually set each value to null. Im sure there are better ways but this works well enough. `
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");
}
`