title: Expanding/shrinking a dynamic array
body: <p>I have an assignment to create a template container class with a dynamic array that can be increased and decreased. When the array is displayed, I get either garbage numbers or no numbers at all for the elements in the array where it was expanded(so if it expanded on the fifth element, the value would turn to junk after I enter the sixth). When I try to remove elements I get a Debug Assertion Failed error. </p>
  
  <p>Here is my code:</p>
  
  <pre><code>template &lt;class T&gt; class container {
  public:
     container(); // constructor
     // Post-condition: count is set to -1 and dynamic array size set to 5
     ~container(); //destructor
     void insert(T &amp;n);
     //  Pre-condition: a value is passed to the function 
     // Post-condition: if data array is not full, increment count by 1 and insert the value in the array 
     // Otherwise, display "Container full! No insertion is made."            
     void remove();
     //Post-condition: if data array is not empty, remove the data[count] element in data array and decrement count by 1; 
     //otherwise, display a message "Container empty! Nothing is removed from it."    
     void display();
     // Post-condition: if the array is not empty, displays all values in data array similar to the sample output;
     //Otherwise, display the message “Container is now empty!"
     void fillarray(container c);
     //pre-condition: a container c is passed to the function
     //post-condition: dynamic array of chosen type is created and filled continuously with user entered values
  private:
     bool empty;
     //Post-condition: returns true is the array is empty, otherwise returns false
     T *data; // dynamically allocated array used to store or contain the inserted values
     int count;   // indicates how many values have been inserted
     int max;
  };
  template &lt;class T&gt; container&lt;T&gt;::container()
  {
     count = -1;
     max = 5;
     data = new T[max];
     assert(data != NULL);
  }
  template &lt;class T&gt; container&lt;T&gt;::~container()
  {
     delete [] data;
  }
  template &lt;class T&gt; void container&lt;T&gt;::insert(T &amp;n)
  {
     if (count &gt;= (max - 1))
     {
        max = max * 2;
        cout &lt;&lt; "\nContainer full! Array size is increased by " &lt;&lt; max/2  &lt;&lt; ".";
        T *temp = new T[max];
        assert(temp != NULL);
        for (int i = 0; i &lt; count; i++)
           temp[i] = data[i];
        delete [] data;
        data = temp;
        count++;
        data[count] = n;
     }
     else
        count++;
     data[count] = n;
  }
  template &lt;class T&gt; void container&lt;T&gt;::remove()
  {
     empty = count &lt; 0;
     if (empty == 1)
     {
        cout &lt;&lt; "\nContainer empty! Nothing is removed from it.";}
     else
     {
        count--;
        T *temp1 = new T[max];
        assert(temp1 != NULL);
        for (int i = 0; i &lt; count; i++)
           temp1[i] = data[i];
        delete [] data;
        data = temp1;
     }
  }
  template &lt;class T&gt; void container&lt;T&gt;::display()
  {
     empty = count &lt; 0;
     if (empty == 1)
     {
        cout &lt;&lt; "\nContainer is now empty!";}
     else
     {
        for (int i = 0; i &lt;= count; ++i)
           cout &lt;&lt; " " &lt;&lt; data[i];
     }
  }
  template &lt;class T&gt; void container&lt;T&gt;::fillarray(container c)
  {
     char ans;
     do
     {
        T value;
        cout &lt;&lt; "\nEnter a value:";
        cin &gt;&gt; value;
        c.insert(value);
        cout &lt;&lt; "\nAfter inserting, the \"container\" contains:" &lt;&lt; endl;
        c.display();
        cout &lt;&lt; "\nEnter more? (Y/y or N/n)";
        cin &gt;&gt; ans;
     } while (ans == 'Y' || ans == 'y');
     for (int i = 0; i &lt;= count; i++) 
     {
        c.remove();
        cout &lt;&lt; "\nAfter removing a value from it, the \"container\" contains:" &lt;&lt; endl;
        c.display();
        cout &lt;&lt; endl;
     }
  }
  // The main driver function to be used to test implementation
  int main()
  {
     char choice;
     cout &lt;&lt; "\nEnter S for string container, D for double";
     cin &gt;&gt; choice;
     if (choice == 'S' || choice == 's')
     {
        container&lt;string&gt; c;
        c.display();
        c.fillarray(c);
     }
     else if(choice == 'D' || choice == 'd')
     {
        container&lt;double&gt; c;
        c.display();
        c.fillarray(c);
     }
     return 0;
  }
  </code></pre>