I'm currently in the second sequence of a C++ course. I'm building my own string class using c-strings & dynamic memory.
I have a majority of my code working. I'm reading in a file and putting each word in a vector of my class type "ATString". We are supposed to combine 5 of the words read in, into a jumbo word, and putting that into another vector. When I use the debugger to step through my code, I see the words combining and it is copying the words over to the new ATString variable "tempJumbo". After a few lines run, the program crashes and tells me my program has triggered a breakpoint, leaving me on line 96 of the addition operator function.
Here the operator+ definition:
ATString ATString::operator+ (ATString& string1) {
   int newSize = length() + string1.length();
   ATString newString;
   if (newSize > 20) { // increase capacity if size is greater than 20 characters
       newString.increase_cap();
   }
   else {
       cap = 20;
   }
   newString.strPtr = new char[newString.cap];
   newString.end = newSize;
   for (int i = 0; i < length(); i++) {
       newString[i] = strPtr[i];
       for (int j = length(); j < newSize; j++) {
           newString[j] = string1.strPtr[j-end];
       }   
return newString;
   } 
And here is main, reading in the file and attempting to combine the words into a jumbo word.
int main() {
   vector<ATString> words(100);
   vector<ATString> lines(100);        // calls default constructor 100 times    
   ifstream fin("infile3.txt");
   int index = 0;
   int wordCount = 0;
   //READ
   if (fin.fail()) {
       cout << "Couldn't open infile2.txt" << endl;
       system("pause");
       exit(1);
   }
   while (!fin.eof()) {
       fin >> words[index];
       index++;
   }
   wordCount = index;
   words.resize(wordCount);
   //COMBINE 5 WORDS INTO ONE JUMBO
   ATString tempJumbo;
   int j = 0;
   for (int i = 0;i < wordCount; i++) {
       tempJumbo = words[i] + words[i+1] + words[i+2] + words[i+3] + words[i+4];
       lines[j] = tempJumbo; // putting big word into lines vector
       tempJumbo = " ";  //resetting variable to hold jumbo word?
       i = i + 4;
       j++;
       if (i == wordCount) {
           break;
       }
   }
return 0;
  }
I am also have issues with the destructor I wrote.. it's pretty simple and when I have this activated, it triggers an error as well and crashes the program. Not sure what is going here.
ATString::~ATString() {  
   delete strPtr;
}
Below is my header file:
#ifndef ATSTRING_H
#define ATSTRING_H
#include <istream>
using namespace std;
class ATString {
public: 
   ATString();// default constructor
   ATString(const char* cstr); // cstring constructor
   ATString(const ATString& argstr);  // copy constructor
   ~ATString();  // destructor 
   ATString& operator = (const ATString& objToCopy); // assignment operator =
   ATString operator + (ATString& string1); // addition operator +
   int length() const;
   int capacity() const;
   void increase_cap();
   char& operator [] (int index); // indexing operator
   const char& operator [] (int index) const; // const indexing operator
   bool operator <(const ATString& argstr) ;
   bool operator > (const ATString& argstr) ;
   bool operator ==(const ATString& argstr);
   friend istream& operator >> (istream& inStrm, ATString& argstr); // extraction operator
   friend const ostream& operator << (ostream& outStrm, const ATString& argstr); // insertion opertator
private:
   char* strPtr;
   int end;
   int cap;
   int compareTo(const ATString& argStr);
};
#endif
THANK YOU!
 
    