title: How to insert a new line in a text file using C++
body: <p>I have to write a program in C++ that asks user to enter lines and store them in a text file.</p>
  
  <pre><code>void create_file(char name[80])
  {
  char line[80],op;
  ofstream fout(name);
  do
  {
      cout &lt;&lt; "Enter the line you want to enter in the file:" &lt;&lt; endl &lt;&lt; endl;
      gets(line);
      fout &lt;&lt; line &lt;&lt; endl;
      cout &lt;&lt; "\nDo you want to enter another line?" &lt;&lt; endl;
      cin &gt;&gt; op;
  }
  while(tolower(op) == 'y');
  cout &lt;&lt; "File created successfully!" &lt;&lt; endl;
  fout.close();
  }
  </code></pre>
  
  <p>The problem is that the text is not being stored in different lines.</p>
  
  <p><strong>I have to use Turbo C++ for this program.</strong></p>
  
  <p>Minimal, Reproducible Example:</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;fstream&gt;
  #include&lt;conio.h&gt;
  #include&lt;stdio.h&gt;
  #include&lt;ctype.h&gt;
  using namespace std;
  void show(char name[80])
  {
      char line[800];
      cout &lt;&lt; "Contents of the file:" &lt;&lt; endl &lt;&lt; endl;
      ifstream fin (name);
      while(!fin.eof())
      {
          fin.getline(line,80);
          if(fin.eof())
              break;
          cout &lt;&lt; line;
      }
  }
  void create_file(char name[80])
  {
      char line[80],op;
      ofstream fout(name);
      do
      {
          cout &lt;&lt; "Enter the line you want to enter in the file:" &lt;&lt; endl &lt;&lt; endl;
          fflush(stdin);
          gets(line);
          fout &lt;&lt; line &lt;&lt; endl;
          cout &lt;&lt; "\nDo you want to enter another line?" &lt;&lt; endl;
          fflush(stdin);
          cin &gt;&gt; op;
      }
      while(tolower(op) == 'y');
      cout &lt;&lt; "File created successfully!" &lt;&lt; endl;
      fout.close();
      show(name);
  }
  int main()
  {
      char name1[80];
      cout &lt;&lt;"Enter the name of the text file:" &lt;&lt; endl;
      gets(name1);
      create_file(name1);
      return 0;
  }
  </code></pre>