title: How to declare an array without size known at compile-time?
body: <p>I am trying to implement <code>random_function()</code>, which outputs the amount of random numbers within a range (user input), and <code>count_function()</code> to count for the value that the user wants to look up in the outputted random numbers. I made the random numbers to be saved in an array called <code>randomlist[]</code> so that it can be used to count for the value from the outputted random numbers. However, I cannot declare an array without its fixed size in the header file and define its size in the count_function.cpp. So I cannot use <code>randomlist[]</code> in the <code>count_function()</code> without redefining it (which is meaningless...).<br />
  Is there a way to declare array without defining its size, in the header file?</p>
  <h2>Here is the source code:</h2>
  <h5><em>header file:</em></h5>
  <pre><code>#pragma once
  
  using namespace std;
  
  class Rand
  {
    public:
      // Rand();
      int range,min,max;
      char key;
      void Random_function();
      void Count_function();
      // randomlist[]; something like this, which that is not possible in c++
  };
  </code></pre>
  <h5><em>This is randomfunction.cpp:</em></h5>
  <pre><code>#include &lt;iostream&gt;
  #include &quot;random_function.hpp&quot;
  
  
  using namespace std;
  
  void Rand::Random_function()
  {
      beginning:
      
      cout &lt;&lt; &quot;Enter amount of numbers to generate: &quot;;
      cin &gt;&gt; range;
      
      if (range&lt;=0 || cin.fail())
      {
          cout &lt;&lt;&quot;Error! Please enter valid number and try again! &quot;&lt;&lt;endl;
          goto beginning;
      }
  
      else
      {
       reenter:
  
          
          cout &lt;&lt; &quot;Enter minimum boundary: &quot;;
          cin &gt;&gt; min;
          cout &lt;&lt; &quot;Enter maximum boundary: &quot;;
          cin &gt;&gt; max;
          cout &lt;&lt; &quot;\n&quot;;
          
          srand((unsigned)time(NULL));
      
          if(max&lt;min || cin.fail())
              {
                  cout &lt;&lt; &quot;\nError! Please enter the valid value and try again! &quot; &lt;&lt; endl;
                  goto reenter;
              }
          
          else
              {
                int randomlist[range]; //I defined the size of the randomlist here but I want to use this outside of the random_fucntion() as well. 
                  
                for(int i=0 ; i &lt; range; i++)
                {
                    
                  randomlist[i] = min + (rand() % static_cast&lt;int&gt;(max - min + 1));
                
                  cout &lt;&lt;i+1&lt;&lt;&quot;. &quot;&lt;&lt; randomlist[i] &lt;&lt;endl;
                
                  
              }
      
      }
          
          cout &lt;&lt;&quot;\n&quot;&lt;&lt; &quot;Total random numbers generated: &quot; &lt;&lt; range&lt;&lt; endl;
    
      
        cout &lt;&lt;&quot;Do you want to continue? y/n&quot;&lt;&lt;endl;
        cin &gt;&gt; key;
      
      if(key=='y')
      {
              Count_function();
          
              cout &lt;&lt;&quot;Do you want to restart? y/n&quot;&lt;&lt;endl;
              cin &gt;&gt; key;
              if(key=='y')
              {
                  goto beginning;
              }
              else
              {
                  exit(0);
              }
  
      }
      
      else
      {
          cout &lt;&lt;&quot;Do you want to restart? y/n&quot;&lt;&lt;endl;
          cin &gt;&gt; key;
          
          if(key=='y')
          {
                  goto beginning;
          }
              
          else
          {
              exit(0);
          }
       
      }
    }   
      
  }
  
  
  void Rand::Count_function()
  {
      
      int n,count=0;
      
      reenter2:
      
      cout&lt;&lt;&quot;Enter the value to count for: &quot;;
      cin&gt;&gt;n;
      
  if(cin.fail())
  {
      cout&lt;&lt;&quot;Please enter valid value to count for&quot;&lt;&lt;endl;
      goto reenter2;
  }
      
  else
  {
      
      for(int i=0 ; i &lt;range; i++)
      {
          if(randomlist[i]==n)
          {
              count++;
          }
      }
  }
      cout &lt;&lt;&quot;The number of '&quot;&lt;&lt;n&lt;&lt;&quot;'s in the given list is: &quot;&lt;&lt; count &lt;&lt;endl;
  }
  </code></pre>
  <h5><em>main:</em></h5>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;ctime&gt;
  #include &lt;random&gt;
  #include &quot;random_function.hpp&quot;
  
  using namespace std;
  
  int main()
  {
      Rand call;
      call.Random_function();
  }
  </code></pre>