title: C++ array size by user input
body: <p>I'm trying to write a program that will have a user input size for an array, and then take values into that array. I initially tried  </p>
  
  <pre><code>int sz = 51;
  double Arr[sz];
  </code></pre>
  
  <p>Which led to compilation errors. Apparently dynamic allocation of the variable has to happen, and I'd rather avoid that if possible. So I modified my code (current state as shown below) which now only throws "expected primary-expression before ']' token". Is there a way to fix this and I'm just not seeing it, or do I need to use dynamic allocation?</p>
  
  <p>Thanks for your time!</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;iomanip&gt; //for setprecision
  
  using namespace std;
  
  int sz = 51;
  double n=0;
  double Arr[0];
  
  void get_input(double Arr[], int &amp;sz){  //gets input
  do{
      cout&lt;&lt; "Enter size: "&lt;&lt; endl;
      cin&gt;&gt; sz;
      if (sz&lt;0 || sz&gt;50){
          cout&lt;&lt; "Invalid size, enter a value between 0 and 50"&lt;&lt;endl;
      }
  }while(sz&lt;0 || sz&gt;50);
  
  for( int i=0; i&lt;sz; i++){
      cin&gt;&gt; Arr[i];
  }
  }
  
  double calcSum( double Arr[], int sz){ //finds sum
  for(int i=0; i&lt;sz; i++){
      n+= Arr[i];
  }
  return(n);
  }
  
  void printArray(double Arr[], int sz){ //prints array elements
  for(int i=0; i&lt;sz; i++){
      cout&lt;&lt; Arr[i]&lt;&lt; setprecision(2)&lt;&lt;" ";
      if(i%7 == 0)
          cout&lt;&lt; endl;
  }
  }
  
  
  int main()
  {
  double Arr[sz];
  get_input(Arr[], sz); //error here
  printArray(Arr[], sz); //error here
  
      return 0;
  }
  </code></pre>