Help with an overloaded operator

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2008
Posts: 128
Reputation: kenji is an unknown quantity at this point 
Solved Threads: 10
kenji's Avatar
kenji kenji is offline Offline
Junior Poster

Help with an overloaded operator

 
0
  #1
Jan 21st, 2009
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.
  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.
  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. }
And she said "Let there be light" and on the seveth day Windows booted.
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 40
Reputation: seanhunt is an unknown quantity at this point 
Solved Threads: 6
seanhunt seanhunt is offline Offline
Light Poster

Re: Help with an overloaded operator

 
0
  #2
Jan 22nd, 2009
Originally Posted by kenji View 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.
  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.
  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 459
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 71
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: Help with an overloaded operator

 
0
  #3
Jan 22nd, 2009
  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.
thanks
-chandra
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC