title: Using C++ stream for cout or text file
body: <p>I have a very simple program where I ask the user if they want to print to screen or a file. Rather than create two sets of output sections, I thought I could switch a stream to either cout or an ofstream and then output to that stream. However, I'm getting screen output no matter what.</p>
  
  <pre><code>ostream &amp;out = cout;
  
  do
  {
      cout &lt;&lt; "Write to file (f) or screen (s)?";
      cin &gt;&gt; yes_or_no;
  } while (yes_or_no != 'f' &amp;&amp; yes_or_no !='s');
  
  if (yes_or_no=='f')
  {
      ofstream out;
      out.open("Report.txt");
      cout &lt;&lt; "Writing report to Report.txt" &lt;&lt; endl;
      system("pause");
  }
  
  out &lt;&lt; "Day:        Current Value         ROI" &lt;&lt; endl;
  out &lt;&lt; "------------------------------------------" &lt;&lt; endl;
  out &lt;&lt; setw(5) &lt;&lt; 0;
  out &lt;&lt; "$" &lt;&lt; setw(20) &lt;&lt; setprecision (2) &lt;&lt; fixed &lt;&lt; initial_value;
  out &lt;&lt; setw(12) &lt;&lt; "1.00" &lt;&lt; endl;
  for (int day = 1 ; day &lt; number_of_days ; day++)
  {
      current_value = generateNextStockValue(current_value, volatility, trend);
      out &lt;&lt; setw(5) &lt;&lt; day;
      out &lt;&lt; setw(20) &lt;&lt; setprecision (2) &lt;&lt; fixed &lt;&lt; current_value;
      out &lt;&lt; setw(12) &lt;&lt; setprecision (2) &lt;&lt; fixed &lt;&lt; current_value / initial_value;
      out &lt;&lt; endl;
  }
  </code></pre>