title: C++ append to string and write to file
body: <p>Why does the following code not work</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  using namespace std;
  
  int main(){
      string data;
      int i=0;
  
      while(i &lt;= 5){
        i++;
        data += i;
        data += "\n";
      }
  
      ofstream myfile;
      myfile.open ("data.txt");
      myfile &lt;&lt; data;
      myfile.close();
  }
  </code></pre>
  
  <p>It <strong><em>should</em></strong> append a <strong>number</strong> then <strong>newline</strong> and write it to a file <strong>(that doesn't yet exist)</strong>.</p>
  
  <p>The file should look like this...</p>
  
  <pre><code>1
  2
  3
  4
  5
  </code></pre>
  
  <p>What is wrong with the code?</p>