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;
}

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;
}

You are adding an array to a string; keep in mind that some versions of length of string funtions count the NULL terminator, some don't...you need to know which is which. Otherwise, you get a string that looks like "Hello ThereXXXXXXXXXXXXX, etc., which will break all operations on the string. I haven't tried to compile the code but I do see a mix of string length funtions, make sure you either add the NULL or it is taken in to account.

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++;
		}

assume 'arraySize' = 5 so savings is an array of length 15. say savings[15], which means savings can go from savings[0]-savings[14]. In the while loop you have k<=15, which means k=15 is valid value, so you try to access savings[15] which is trying to access undefined memory.

This article has been dead for over six months. Start a new discussion instead.