943,733 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 538
  • C++ RSS
Jan 21st, 2009
0

Help with an overloaded operator

Expand Post »
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.
C++ Syntax (Toggle Plain Text)
  1. ie. savings[i].accountNumber;
  2. savings[i].balance;
  3. 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.
C++ Syntax (Toggle Plain Text)
  1. const char* Bank::operator+=(const char data[]){
  2. const int len = arraySize;
  3. Account *temp;
  4. temp = NULL;
  5. temp = new Account[len];
  6. int bal;
  7. char anum[15 + 1];
  8. char cust[315 + 1];
  9. int k = 0;
  10.  
  11. sscanf(data,"%15[^,],%d,%315[^;];", anum, &bal, cust);
  12.  
  13. int value = strlen(cust);
  14. cust[value] = ';';
  15. cust[value + 1] = '\0';
  16.  
  17. if(size < arraySize){
  18. cout << "(size < arraySize)" << endl;
  19. int x = size + 1;
  20. strcpy(savings[x].accountNumber, anum);
  21. savings[x].balance = bal;
  22. strcpy(savings[x].customer, cust);
  23. size++;
  24. cout << size << '\t' << arraySize << endl;
  25. }
  26.  
  27. if(size == arraySize){
  28. cout << "Start " << endl;
  29. int j = 0;
  30. while(j < arraySize){
  31. strcpy(temp[j].accountNumber, savings[j].accountNumber);
  32. temp[j].balance = savings[j].balance;
  33. strcpy(temp[j].customer, savings[j].customer);
  34. j++;
  35. }
  36.  
  37. delete [] savings;
  38. savings = new Account[arraySize + 10];
  39.  
  40. while(k <= arraySize + 10){
  41. strcpy(savings[k].accountNumber, temp[k].accountNumber);
  42. savings[k].balance = temp[k].balance;
  43. strcpy(savings[k].customer, temp[k].customer);
  44. k++;
  45. }
  46.  
  47. delete [] temp;
  48.  
  49. int i = size + 1;
  50. strcpy(savings[i].accountNumber, anum);
  51. savings[i].balance = bal;
  52. strcpy(savings[i].customer, cust);
  53. size++;
  54. arraySize = arraySize + 10;
  55. cout << size << '\t' << arraySize << endl;
  56. }
  57. return data;
  58. }
Similar Threads
Reputation Points: 11
Solved Threads: 11
Junior Poster
kenji is offline Offline
145 posts
since May 2008
Jan 22nd, 2009
0

Re: Help with an overloaded operator

Click to Expand / Collapse  Quote originally posted by kenji ...
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.
C++ Syntax (Toggle Plain Text)
  1. ie. savings[i].accountNumber;
  2. savings[i].balance;
  3. 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.
C++ Syntax (Toggle Plain Text)
  1. const char* Bank::operator+=(const char data[]){
  2. const int len = arraySize;
  3. Account *temp;
  4. temp = NULL;
  5. temp = new Account[len];
  6. int bal;
  7. char anum[15 + 1];
  8. char cust[315 + 1];
  9. int k = 0;
  10.  
  11. sscanf(data,"%15[^,],%d,%315[^;];", anum, &bal, cust);
  12.  
  13. int value = strlen(cust);
  14. cust[value] = ';';
  15. cust[value + 1] = '\0';
  16.  
  17. if(size < arraySize){
  18. cout << "(size < arraySize)" << endl;
  19. int x = size + 1;
  20. strcpy(savings[x].accountNumber, anum);
  21. savings[x].balance = bal;
  22. strcpy(savings[x].customer, cust);
  23. size++;
  24. cout << size << '\t' << arraySize << endl;
  25. }
  26.  
  27. if(size == arraySize){
  28. cout << "Start " << endl;
  29. int j = 0;
  30. while(j < arraySize){
  31. strcpy(temp[j].accountNumber, savings[j].accountNumber);
  32. temp[j].balance = savings[j].balance;
  33. strcpy(temp[j].customer, savings[j].customer);
  34. j++;
  35. }
  36.  
  37. delete [] savings;
  38. savings = new Account[arraySize + 10];
  39.  
  40. while(k <= arraySize + 10){
  41. strcpy(savings[k].accountNumber, temp[k].accountNumber);
  42. savings[k].balance = temp[k].balance;
  43. strcpy(savings[k].customer, temp[k].customer);
  44. k++;
  45. }
  46.  
  47. delete [] temp;
  48.  
  49. int i = size + 1;
  50. strcpy(savings[i].accountNumber, anum);
  51. savings[i].balance = bal;
  52. strcpy(savings[i].customer, cust);
  53. size++;
  54. arraySize = arraySize + 10;
  55. cout << size << '\t' << arraySize << endl;
  56. }
  57. return data;
  58. }
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.
Reputation Points: 13
Solved Threads: 6
Light Poster
seanhunt is offline Offline
40 posts
since Oct 2008
Jan 22nd, 2009
0

Re: Help with an overloaded operator

c++ Syntax (Toggle Plain Text)
  1. savings = new Account[arraySize + 10];
  2.  
  3. while(k <= arraySize + 10){
  4. strcpy(savings[k].accountNumber, temp[k].accountNumber);
  5. savings[k].balance = temp[k].balance;
  6. strcpy(savings[k].customer, temp[k].customer);
  7. k++;
  8. }

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.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: wxWidget
Next Thread in C++ Forum Timeline: C++ Performance Tips





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC