title: Read a .txt file, calculate sum and mean
body: <p>I have a .txt file which contains numbers and looks like this:</p>
  
  <blockquote>
    <p>1<br>
    2<br>
    3</p>
  </blockquote>
  
  <p>etc</p>
  
  <p>The numbers are unimportant but they all start on a new line.</p>
  
  <p>I want to then find the sum &amp; mean of the numbers in the text file.</p>
  
  <p>Here's what I have so far:</p>
  
  <pre><code>#include&lt;cmath&gt;
  #include&lt;cstdlib&gt;
  #include&lt;iomanip&gt;
  #include&lt;string&gt;
  #include&lt;fstream&gt;
  using namespace std;
  
  int main(int argc, char * argv[])
  {
      std::fstream myfile("numbers.txt", std::ios_base::in);
  
      float a;
      while (myfile &gt;&gt; a)
      {
          printf("%f ", a);
      }
  
      getchar();
  
      return 0;
  
  int sum(0);
  int sumcount(0);
  double average(0);
  int even(0);
  int odd(0);
  
  ifstream fin;
  
  string file_name;
  
  int x(0);
  
  cout&lt;&lt;"numbers.txt";
  cin&gt;&gt; file_name;
  fin.open(file_name.c_str(),ios::in);
  
  if (!fin.is_open())
  {
      cerr&lt;&lt;"Unable to open file "&lt;&lt;file_name&lt;&lt;endl;
      exit(10);
  
  }
  
  fin&gt;&gt;x;
      while (!fin.fail())
      {
          cout&lt;&lt;"Read integer: "&lt;&lt;x&lt;&lt;endl;
          fin&gt;&gt;x;
          sum=sum+x;
          sumcount++;
          if(x%2==0)
              even++;
          else
              odd++;
  
      }
  
      fin.close();
      average=(double)sum/sumcount;
      cout&lt;&lt;"Sum of integers: "&lt;&lt;sum&lt;&lt;endl;
      cout&lt;&lt;"Average: "&lt;&lt;average&lt;&lt;endl;
      cout&lt;&lt;"Number of even integers: "&lt;&lt;even&lt;&lt;endl;
      cout&lt;&lt;"Number of odd integers: "&lt;&lt;odd&lt;&lt;endl;
  
      return 0;
  }
  </code></pre>
  
  <p>The start loads the numbers but it won't execute the next step.</p>
  
  <p>i have looked at a lot of other code and tried to implement other ideas to solve the problem; however, some people use loops and arrays and other things and I'm not sure which to use.</p>
  
  <p>How can I get my program to find the sum and mean of numbers in the file?</p>
  
  <p>also any other links for help would be appreciated </p>
  
  <p>EDIT: although the numbers are intergers the mean may not be </p>