title: How to write to file in separate function
body: <p>I am trying to get my program to write in a separate function than the main function, and I'm having a good deal of trouble. Here is a simplified version of my program:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  using namespace std;
  
  void writeToFile(int x)
  {
      outputFile &lt;&lt; x &lt;&lt; endl;
  }
  
  int main()
  {
  ofstream outputFile;
  outputFile.open("program3data.txt");
  for (int i = 0; i &lt; 10; i++)
  {
      writeToFile(i);
  }
  outputFile.close();
  return 0;
  }
  </code></pre>