title: String error when expanding a dynamic array
body: <p>I'm having a problem with trying to append an extra element to a dynamic array. I know I can use vectors, but for academic purposes, I have to use arrays.</p>
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  template &lt;class T&gt;
  class MyList
  {
  protected:
      T* elems;
      int itsSize;
  public:
      MyList(int itsSize):itsSize(itsSize)
      {
          elems = new T[itsSize];
      }
  
      ~MyList()
      {
          delete [] elems;
      }
  
      int getSize()
      {
          return itsSize;
      }
  
      virtual void addElem(){}
      virtual void getElem(){}
  };
  
  template &lt;class T&gt;
  class MyStack : public MyList&lt;T&gt;
  {
      int counter;
  public:
      MyStack(int size):MyList&lt;T&gt;::MyList(size){counter=0;}
  
      void addElem()
      {
          T* tmp = new T[counter+1];
  
          for (int i = 0; i&lt;counter+1; i++)
              tmp[i] = MyList&lt;T&gt;::elems[i];
          counter++;
  
          delete [] MyList&lt;T&gt;::elems;
          MyList&lt;T&gt;::elems = tmp;
  
          cin &gt;&gt; MyList&lt;T&gt;::elems[counter-1];
          if(cin.fail())
          {
              cin.clear();
              string line;
              getline(cin, line);
              throw &quot;Wrong String Input--&gt; will enter 0&quot;;
          }
  
      }
  
      void getElem()
      {
          for(int i=counter-1; i&gt;=0; i--)
          {
              cout &lt;&lt; &quot;Element--&gt;&quot; &lt;&lt; MyList&lt;T&gt;::elems[i] &lt;&lt; endl;
          }
      }
  
  };
  
  
  int main()
  {
          int storeChoice;
          cout &lt;&lt; &quot;Would you like to store integers, strings or rectangles (1,2, or 3)?&quot; &lt;&lt; endl;
          cin &gt;&gt; storeChoice;
  
          if(storeChoice==1)
          {
              MyStack&lt;int&gt; numList(1);
  
              cout &lt;&lt; &quot;Enter num:&quot;;
              numList.addElem();
  
              bool choiceAddLoop = true;
              while(choiceAddLoop == true)
              {
                  try
                  {
                      char choiceAdd;
                      cout &lt;&lt; &quot;Would you like to enter another elem?(y/n)&quot; &lt;&lt; endl;
                      cin &gt;&gt; choiceAdd;
                      if(choiceAdd=='y')
                      {
                          try
                          {
                           cout &lt;&lt; &quot;Enter num:&quot;;
                           numList.addElem();
                          }
                          catch(const char* wrongInput)
                          {
                           cout &lt;&lt; wrongInput &lt;&lt; endl;
                          }
                      }
                      else if(choiceAdd=='n')
                      {
                          choiceAddLoop=false;
                          break;
                      }
                      else
                          throw &quot;Invalid Input.&quot;;
  
                  }
                  catch(const char* invalidChoice)
                  {
                      cout &lt;&lt; invalidChoice;
                  }
              }
  
                   cout &lt;&lt; endl &lt;&lt; &quot;All Elements&quot; &lt;&lt; endl;
                   numList.getElem();
          }
          else if(storeChoice==2)
          {
              MyStack&lt;string&gt; stringList(1);
  
              cout &lt;&lt; &quot;Enter string:&quot;;
              stringList.addElem();
  
              bool choiceAddLoop = true;
              while(choiceAddLoop == true)
              {
                  try
                  {
                      char choiceAdd;
                      cout &lt;&lt; &quot;Would you like to enter another elem?(y/n)&quot; &lt;&lt; endl;
                      cin &gt;&gt; choiceAdd;
                      if(choiceAdd=='y')
                      {
                           cout &lt;&lt; &quot;Enter string:&quot;;
                           stringList.addElem();
                      }
                      else if(choiceAdd=='n')
                      {
                          choiceAddLoop=false;
                          break;
                      }
                      else
                          throw &quot;Invalid Input.&quot;;
  
                  }
                  catch(const char* invalidChoice)
                  {
                      cout &lt;&lt; invalidChoice;
                  }
              }
  
                cout &lt;&lt; endl &lt;&lt; &quot;All Elements&quot; &lt;&lt; endl;
                stringList.getElem();
          }
          }
  </code></pre>
  <p>When I select the first choice ( integer ) the code will work:</p>
  <pre><code>Would you like to store integers, strings or rectangles (1,2, or 3)?
  1
  Enter num:22
  Would you like to enter another elem?(y/n)
  y
  Enter num:3
  Would you like to enter another elem?(y/n)
  n
  
  All Elements
  Element--&gt;3
  Element--&gt;22
  
  Process returned 0 (0x0)   execution time : 5.162 s
  Press any key to continue.
  </code></pre>
  <p>A problem occurs in the second choice (string):</p>
  <pre><code>Would you like to store integers, strings or rectangles (1,2, or 3)?
  2
  Enter string:hello
  Would you like to enter another elem?(y/n)
  y
  Enter string:terminate called after throwing an instance of 'std::length_error'
    what():  basic_string::_M_create
  
  Process returned 3 (0x3)   execution time : 6.761 s
  Press any key to continue.
  </code></pre>
  <p>It's the same code for both, why does it work only with integers?</p>