deleting elements from an array.

I have a programming project for class where I'm writing bank software. One of the modules I have is I'm trying to close an account. I keep getting a segmentation error.

here's my code.

struct AccountInfo
{
string acNum
string firstName;
string midInitia;
int balance;
};

int linearSearch(string acno, CONST AccountInfo bankRecord[] int numAccs)
{
int i;
for(int i = 0; i<numAccs; i++)
{
if (acno == bankRecord[i].acNum)
return i;
}
return -1;
}

void closeAccount(string acno, AccountInfo BankRecord[], int &numAccs)
{
int i;
AccountInfo newbankRecord[i]
cout<<"Please enter your account number";
cin>>acno
if (linearSearch(acno, bankRecord, numAccs) >0)
{
for(int i =0; i<numAccs; i++)
{
newbankRecord[i+1] = bankRecord[i]
}

}
}


This isn't working. What am I doing wrong?
Last edited on
Assuming linearSearch works and returns the index of the account but you dont use it, The for loop appears to be just copying the first record into the rest of the array. This newbankRecord[i+1] is causing the seg fault.
Last edited on
The linearSearch does work I've used it in other functions. I'm guessing that's what the problem is too. How do I fix it? Oh and I edited the second part. I it's bankRecord[i] not newbankRecord[i]. my bad
Last edited on
First,
1
2
int i;
AccountInfo newbankRecord[i]
you havent given i a value, so the size of newbankRecord is undefined. Here newbankRecord[i+1] you are reading past the size of the array causing the seg fault. For the copying I would do something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
AccountInfo newbankRecord[numAccs - 1];
//...
int i =0, k = 0;
if ((i = linearSearch(acno, bankRecord, numAccs)) >0)
{
    for(int j = 0; i < numAccs;++j)
    {
        if (i != j)
        {
            newbankRecord[k] = bankRecord[j]
            ++k;
        }
    }
}

This is ugly and untested, but should give you an idea.
Last edited on
Topic archived. No new replies allowed.