Hello,
I have coded an overloaded += operator and it is giving me trouble. basic function of the operator is to add data to dynamically allocated class array which has 3 member variables.
ie. savings[i].accountNumber;
savings[i].balance;
savings[i].customer;
I have managed to get the first part of the function to work where if size (the current number of objects) is less than arraySize (total number of object that can be stored), then add the new data to size.
However once size is equal to arraySize my program crashes when it should run the second if statement instead. Can you please help me by pointing out my mistake so I can correct it.
Thanks.
const char* Bank::operator+=(const char data[]){
const int len = arraySize;
Account *temp;
temp = NULL;
temp = new Account[len];
int bal;
char anum[15 + 1];
char cust[315 + 1];
int k = 0;
sscanf(data,"%15[^,],%d,%315[^;];", anum, &bal, cust);
int value = strlen(cust);
cust[value] = ';';
cust[value + 1] = '\0';
if(size < arraySize){
cout << "(size < arraySize)" << endl;
int x = size + 1;
strcpy(savings[x].accountNumber, anum);
savings[x].balance = bal;
strcpy(savings[x].customer, cust);
size++;
cout << size << '\t' << arraySize << endl;
}
if(size == arraySize){
cout << "Start " << endl;
int j = 0;
while(j < arraySize){
strcpy(temp[j].accountNumber, savings[j].accountNumber);
temp[j].balance = savings[j].balance;
strcpy(temp[j].customer, savings[j].customer);
j++;
}
delete [] savings;
savings = new Account[arraySize + 10];
while(k <= arraySize + 10){
strcpy(savings[k].accountNumber, temp[k].accountNumber);
savings[k].balance = temp[k].balance;
strcpy(savings[k].customer, temp[k].customer);
k++;
}
delete [] temp;
int i = size + 1;
strcpy(savings[i].accountNumber, anum);
savings[i].balance = bal;
strcpy(savings[i].customer, cust);
size++;
arraySize = arraySize + 10;
cout << size << '\t' << arraySize << endl;
}
return data;
}