title: Sum and Average with Dynamic Array
body: <p>I'll preface this by noting that I'm very new to C++ and programming in general so if I'm doing something improperly or writing code in an odd way, it's because i've only learned so much so far.</p>
  
  <p>Anyways, I was given an assignment to write a program that first</p>
  
  <ul>
  <li>Reads integers from a file of the user's choice
  
  <ul>
  <li>Outputs the sum and average of all the numbers greater than or equal to zero ,</li>
  <li>Outputs the sum and average of all the numbers less than zero</li>
  <li>Outputs the sum and average of all the numbers, whether positive, negative, or zero.</li>
  <li>Then finally asks the user if they'd like to choose another file to run on again</li>
  </ul></li>
  </ul>
  
  <p>The only catch is that I have to use a dynamic array within the code, I'm assuming in order to allow the file to hold any amount of integers.</p>
  
  <p>So far, I have everything except for the implementation of the dynamic array. The code is currently programmed to only accept 10 integers (as there are no arrays in the code yet).</p>
  
  <p>Here's my code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;iomanip&gt;
  using namespace std;
  
  int main() {
  
      //Variables
      string inFile;
      int numbers, i = 0;
      double avg, neg_avg, total_sum, total_avg, sum = 0, neg_sum = 0;;
      double count = 0, neg_count = 0, pos_count = 0;
      char answer;
  
  
  
      do
      {
          //Input Question
          cout &lt;&lt; "Enter the file name.\n";
          cin &gt;&gt; inFile;  // Input from User
          ifstream fin;   // Open File
          fin.open(inFile);
  
          if (fin.fail())  // Check to see if file opens properly
          {
              cout &lt;&lt; "An error occurred while attempting to open the file.\n";
              exit(1);
          }
  
          while (count &lt; 10)
          {
              fin &gt;&gt; numbers;
              if (numbers &gt;= i)
              {
                  sum += numbers;
                  count += 1;
                  pos_count += 1;
              }
              if (numbers &lt; i)
              {
  
                  neg_sum = neg_sum + numbers;
                  count = count + 1;
                  neg_count = neg_count + 1;
              }
          }
  
  
          //Calculations
  
          avg = sum / pos_count;
          neg_avg = neg_sum / neg_count;
          total_sum = sum + neg_sum;
          total_avg = total_sum / 10.0;
  
  
          //OUTPUT
          cout &lt;&lt; "The sum of all positive numbers is: " &lt;&lt; sum &lt;&lt; endl;
          cout &lt;&lt; "The average of all positive numbers is: " &lt;&lt; setprecision(3) &lt;&lt; avg &lt;&lt; endl;
          cout &lt;&lt; "The sum of all negative numbers is: " &lt;&lt; neg_sum &lt;&lt; endl;
          cout &lt;&lt; "The average of all negative numbers is: " &lt;&lt; setprecision(3) &lt;&lt; neg_avg &lt;&lt; endl;
          cout &lt;&lt; "The sum of all numbers is: " &lt;&lt; total_sum &lt;&lt; endl;
          cout &lt;&lt; "The average of all numbers is: " &lt;&lt; setprecision(3) &lt;&lt; total_avg &lt;&lt; endl;
  
          cout &lt;&lt; "-------------------------------------------------" &lt;&lt; endl;
  
          cout &lt;&lt; "Want us to read another file?\n";
          cout &lt;&lt; "Enter 'Y' or 'y'  for yes, any other character for no." &lt;&lt; endl;
          cin &gt;&gt; answer;
      } while ((answer == 'y') || (answer == 'Y'));
  
      return 0;
  
  
  }
  </code></pre>
  
  <p>Any help would be greatly appreciated!
  Thanks in advance</p>
  
  <p>UPDATE:</p>
  
  <p>I've gotten this far but when I compile, the program runs continuously.Not sure what I'm doing wrong. </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;iomanip&gt;
  using namespace std;
  
  int main() {
  
      //Variables
      string file;
      int i = 0;
      double avg, neg_avg, total_sum, total_avg, sum = 0, neg_sum = 0;;
      double neg_count = 0, pos_count = 0, totcount = 0;
      char answer;
  
      //Input Question
  
      do
      {
          cout &lt;&lt; "Enter the file name.\n";
          cin &gt;&gt; file;  // Input from User
          ifstream fin;   // Open File
          fin.open(file);
  
          if (fin.fail())  // Check to see if file opens properly
          {
              cout &lt;&lt; "An error occurred while attempting to open the file.\n";
              exit(1);
          }
  
          while (!fin.eof())
          {
              int numbers;
              fin &gt;&gt; numbers;
              int *dynamicArray;
              dynamicArray = new int[numbers];
  
              if (numbers &gt;= i)
              {
                  sum += numbers;
                  pos_count += 1;
                  totcount += 1;
  
              }
              if (numbers &lt; i)
              {
                  neg_sum = neg_sum + numbers;
                  neg_count = neg_count + 1;
                  totcount += 1;
              }
  
              //Calculations
  
              avg = sum / pos_count;
              neg_avg = neg_sum / neg_count;
              total_sum = sum + neg_sum;
  
              total_avg = total_sum / totcount;
  
  
              //OUTPUT
              cout &lt;&lt; "The sum of all positive numbers is: " &lt;&lt; sum &lt;&lt; endl;
              cout &lt;&lt; "The average of all positive numbers is: " &lt;&lt; setprecision(3) &lt;&lt; avg &lt;&lt; endl;
              cout &lt;&lt; "The sum of all negative numbers is: " &lt;&lt; neg_sum &lt;&lt; endl;
              cout &lt;&lt; "The average of all negative numbers is: " &lt;&lt; setprecision(3) &lt;&lt; neg_avg &lt;&lt; endl;
              cout &lt;&lt; "The sum of all numbers is: " &lt;&lt; total_sum &lt;&lt; endl;
              cout &lt;&lt; "The average of all numbers is: " &lt;&lt; setprecision(3) &lt;&lt; total_avg &lt;&lt; endl;
              cout &lt;&lt; "-------------------------------------------------" &lt;&lt; endl;
  
              delete [] dynamicArray;
  
          }
  
          fin.close();
  
          cout &lt;&lt; "Want us to read another file?\n";
          cout &lt;&lt; "Enter 'Y' or 'y'  for yes, any other character for no." &lt;&lt; endl;
          cin &gt;&gt; answer;
      } while ((answer == 'y') || (answer == 'Y'));
  
      return 0;
  
  
  }
  
  </code></pre>
  
  <p>UPDATE:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;iomanip&gt;
  #include &lt;vector&gt;
  using namespace std;
  
  int main() {
  
      //Variables
      string file;
      int i = 0, value = 0, e = 0;
      double avg, neg_avg, total_sum, total_avg, sum = 0, neg_sum = 0;;
      double neg_count = 0, pos_count = 0, totcount = 0;
      char answer;
  
      //Input Question
  
      do
      {
          cout &lt;&lt; "Enter the file name.\n";
          cin &gt;&gt; file;  // Input from User
          ifstream fin;   // Open File
          fin.open(file);
  
          if (fin.fail())  // Check to see if file opens properly
          {
              cout &lt;&lt; "An error occurred while attempting to open the file.\n";
              exit(1);
          }
  
  
          //                       &lt;----------  This works to get the size of the file
          int elements;
          vector&lt;int&gt; eCount;
          while (fin &gt;&gt; elements)
          {
              eCount.push_back(elements);
          }
          int size = static_cast&lt;int&gt; (eCount.size());
          cout &lt;&lt; "size = " &lt;&lt; size &lt;&lt; endl;//  &lt;-----------Test to see if working 
  
          //From this point, size of the file is held in the variable, 'size'.
  
          int array_size = size;
          int* p;
          p = new int[array_size];
  
  
  
  
  
          int location = 0;
          while (!fin.eof())
          {
              fin &gt;&gt; p[location];
              location++;
          }
  
          cout &lt;&lt; "P[12] is equal to " &lt;&lt; p[12] &lt;&lt; endl;// &lt;----Test to see if array is initialized
  
          while (fin &gt;&gt; p[location])
          {
  
  
              if (p[e] &gt;= i)
              {
                  sum = sum + p[location];
                  pos_count = pos_count + 1;
                  totcount = totcount + 1;
              }
              else 
              {
                  neg_sum = neg_sum + p[location];
                  neg_count = neg_count + 1;
                  totcount = totcount + 1;
              }
              location++;
          }
          //Calculations
  
          avg = sum / pos_count;
          neg_avg = neg_sum / neg_count;
          total_sum = sum + neg_sum;
  
          total_avg = total_sum / totcount;
  
          fin.close();
  
          //OUTPUT
          cout &lt;&lt; "The sum of all positive numbers is: " &lt;&lt; sum &lt;&lt; endl;
          cout &lt;&lt; "The average of all positive numbers is: " &lt;&lt; setprecision(3) &lt;&lt; avg &lt;&lt; endl;
          cout &lt;&lt; "The sum of all negative numbers is: " &lt;&lt; neg_sum &lt;&lt; endl;
          cout &lt;&lt; "The average of all negative numbers is: " &lt;&lt; setprecision(3) &lt;&lt; neg_avg &lt;&lt; endl;
          cout &lt;&lt; "The sum of all numbers is: " &lt;&lt; total_sum &lt;&lt; endl;
          cout &lt;&lt; "The average of all numbers is: " &lt;&lt; setprecision(3) &lt;&lt; total_avg &lt;&lt; endl;
          cout &lt;&lt; "-------------------------------------------------" &lt;&lt; endl;
  
  
  
  
  
  
  
          cout &lt;&lt; "Want us to read another file?\n";
          cout &lt;&lt; "Enter 'Y' or 'y'  for yes, any other character for no." &lt;&lt; endl;
          cin &gt;&gt; answer;
      } while ((answer == 'y') || (answer == 'Y'));
  
          return 0;
  }
  </code></pre>
  
  <p>Thank you to everyone who's pitched in. I wish I didn't have to use a dynamic array but unfortunately I won't receive if I don't implement one. I updated my code but I can't seem to get the array working properly as it does not seem to load the input from the file properly. Anything helps!</p>