title: How do I open a file from a function for the whole code?
body: <p>I'm working on a simple c++ script and wanted to put the whole process of opening up a file inside a function. However, when I try, I get errors in my main function. Can anyone help me? This is my code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;iomanip&gt;
  
  using namespace std;
  
  string openFile(string fileName);
  int main(void)
  {
      string fileName;
      cout &lt;&lt; "Please input the file name (including the extension) for this code to read from." &lt;&lt; endl;
      cin &gt;&gt; fileName;
  
      openFile(fileName);
  
      fout &lt;&lt; "File has been opened" &lt;&lt; endl;
      return 0;
  }
  
  string openFile(string fileName)
  {
      ifstream fin(fileName);
      if (fin.good())
      {
          ofstream fout("Output");
          cout &lt;&lt; fixed &lt;&lt; setprecision(1);
          fout &lt;&lt; fixed &lt;&lt; setprecision(1);
          //Set the output to console and file to be to two decimal places and 
          //not in scientific notation
      }
      else 
      {
          exit(0);
      }
  }
  </code></pre>