title: How to write code results (that are within a void function) to a text file in C++
body: <p>I am trying to create a program that loads a "grades.txt" file that contains 20 numbers and calculates the standard deviation and outputs the results both on the screen as well as in a separate text file titled "results.txt"</p>
  
  <p>The programs works great and outputs the results I am looking for. the only thing is, i cannot seem to figure out how to take the calculated results (i.e sum, mean, standard deviation) and output it into a text file. </p>
  
  <p>The output text file i have in the code is successfully outputting the "results" message i am sending to it. </p>
  
  <p>I just do not know how to take the results that are within the "void filereadernc(ifstream &amp;file)" function and output in the text file of the "void filewriter(ofstream &amp;file)" function</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  
  // calling std name space
  using namespace std;
  
  void fileopenner(ifstream &amp;file);
  void filereader(ifstream &amp;file);
  void filecloser(ifstream &amp;file);
  void filereadernc(ifstream &amp;file);
  void filecreate(ofstream &amp;file);
  void filewriter(ofstream &amp;file);
  void filecloser(ofstream &amp;file);
  
  int main()
  {
  // declaration
  ifstream ecet;
  ofstream result;
  
  // opening onc again
  fileopenner(ecet);
  filereadernc(ecet);
  filecloser(ecet);
  filecreate(result);
  filewriter(result);
  filecloser(result);
  
  cout &lt;&lt; endl;
  
  return 0;
  } // the end of the main function
  
  void fileopenner(ifstream &amp;file)
  {
  file.open("C:/Users/Desktop/grades.txt"); // file location on flash drive
  }
  
  void filereader(ifstream &amp;file) // reading the values from the text file
  {
  
  int list[20];
  for (int i = 0; i &lt; 20; i++)
      file &gt;&gt; list[i];
  }
  
  void filereadernc(ifstream &amp;file) // file reader
  {
  float sum = 0.0, mean = 0.0, standardDeviation = 0.0;
  
  int list[100];
  int j = 0;
  file &gt;&gt; list[j];
  
  
  while (!file.eof())
  {
      j++;
      file &gt;&gt; list[j];
  }
  cout &lt;&lt; "The input values: "; // display the values that were in the text file to the user
  
  for (int i = 0; i &lt;= j; i++)
      cout &lt;&lt; list[i] &lt;&lt; " ";
  
  for (int i = 0; i &lt;= j; i++) // calculate the sum and mean
  {
      sum += list[i];
      mean = sum / 20;
  }
  for (int i = 0; i &lt;= j; i++) // after finding the mean, we compute the standard deviation
      standardDeviation += pow(list[i] - mean, 2);
  
  cout &lt;&lt; endl;
  cout &lt;&lt; "The sum of all our values = " &lt;&lt; sum; // display the mean of all the values in the text file
  cout &lt;&lt; endl;
  cout &lt;&lt; "The mean of all our values = " &lt;&lt; mean; // display the mean of all the values in the text file
  cout &lt;&lt; endl;
  cout &lt;&lt; "The standard deviation = " &lt;&lt; sqrt(standardDeviation / 20); // display the standard deviation
  }
  
  void filecloser(ifstream &amp;file)
  {
  file.close();
  }
  
  void filecreate(ofstream &amp;file)
  {
  file.open("C:/Users/Desktop/results.txt");
  }
  
  void filewriter(ofstream &amp;file)
  {
  
  file &lt;&lt; "results";
  
  }
  
  void filecloser(ofstream &amp;file)
  {
  file.close();
  }
  </code></pre>