title: C++ Writing to a CSV file
body: <p>The purpose of the code is to prompt the user to enter how many connections/nets &amp; write to a CSV file.</p>
  <p>The code runs and writes to the CSV file on the first column but after the user is prompted
  to input the # of nets again, it writes on the same column instead of the next one which has the header
  &quot;Connector 2&quot; or Column B.</p>
  <p><strong>How do I get my code to write to the first column, move on to the next column and prompt the user to
  input the # of nets/connections?</strong></p>
  <p>Here is my code</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  
  int main() {
  
  int nets;
  
  ofstream myFile;
  myFile.open(&quot;My File.csv&quot;); // Create file
  
  myFile &lt;&lt; &quot;Connector 1&quot; &lt;&lt; &quot;,&quot; &lt;&lt; &quot;Connector 2&quot; &lt;&lt; endl; // Headers
  
   while (true) {
  
      cout &lt;&lt; &quot;# of Nets: &quot; &lt;&lt; endl;
      cin &gt;&gt; nets;
  
      if (nets &gt; 0) {
  
          for (int i = 1; i &lt;= nets; i++) {
  
              myFile &lt;&lt; i &lt;&lt; endl;
              myFile &lt;&lt; &quot;,&quot;;
          
          }
      }
  
      else {
  
          break;
  
          }
      }
  }
  </code></pre>