title: Using dynamic memory allocation to add elements into an array
body: <p>I'm supposed to write some code to ask the user to enter an array (1.3 4 5.2 16.3 9.99 7.21 4.5 7.43 11.21 12.5).</p>
  
  <p>After that, I create a new array with a bigger size (double the size), copy all the elements from the old array to the new one, then ask the user to continue to enter 5 more elements to the new array: 1.5 4.5 9.5 16.5 7.5 11.5, and then print out the final array (15 elements).</p>
  
  <p>Here is my code:</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  double* read_data(int&amp; size) 
  {
      int max = 10;         
      double* a = new double[max];  // allocated on heap
      size = 0;
  
      cout &lt;&lt; "Enter the array: " &lt;&lt; endl;
      while (cin &gt;&gt; a[size]) 
      {
          size++;
      }
      if (size &gt;= max) 
      {
          double* temp = new double[max * 2];
          for (int i = 0; i &lt; size; i++) 
          {
              temp[i] = a[i];       
          }
          delete[] a;             
          a = temp;   
          max = max * 2;  
      }
      return a;
  }
  
  int main () 
  {
      int input1, input2, input3, input4, input5;
  
      int size = 0;
      double* arr = read_data(size);
  
      cout &lt;&lt; "Please enter 5 more elements: " &lt;&lt; endl;
      cin &gt;&gt; input1 &gt;&gt; input2 &gt;&gt; input3 &gt;&gt; input4 &gt;&gt; input5;
      arr[10] = input1;
      arr[11] = input2;
      arr[12] = input3;
      arr[13] = input4;
      arr[14] = input5;
  
      cout &lt;&lt; "The final array is: " &lt;&lt; endl;
  
      for (int i = 0; i &lt; 15; i++)
      {
          cout &lt;&lt; arr[i];
      }
  
      system("pause");
      return 0;
  }
  </code></pre>
  
  <p>It doesn't let me enter 5 more elements and I don't know why.
  Please help.</p>