title: Why is my program writing to the same line in .dat file
body: <p>I am creating a library Management system. The system is meant write and read from a .dat file. However the system is having problem writing the user input into the dat file. It is meant write the book name, author name and the book barcode on the same line and then when the user enters another book its meant to add it to another line but it just overwrites the existing data and also when I use the option to display all the data it displays the muddled up text.</p>
  
  <p>Can some please tell me what I am doing wrong.</p>
  
  <p>I have included some of the code.
  Main.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include "Library.h"
  
  int main() {
      Library lib1;
  
  while(1)
  {
     std::cout &lt;&lt; "*   1              Add a new book                  *\n";
     std::cout &lt;&lt; "*   2              Show all books                  *\n";
  
     std::cin &gt;&gt; name;
     bookOption = name;
  
     switch(bookOption)
     {
       case '1':
       {
         lib1.insertBook();
         break;
       }
       case '2':
       {
         lib1.showallBooks();
         break;
       }
  }
  return 0;
  };
  </code></pre>
  
  <p>Book.h</p>
  
  <pre><code>#ifndef BOOK_H_
  #define BOOK_H_
  
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include "Media.h"
  
  class Book : public Media
  {
  public:
      std::string author;
  
  public:
      void newBook();
  void report();
  };
  </code></pre>
  
  <p>Book.cpp</p>
  
  <pre><code>#include &lt;string&gt;
  #include "Book.h"
  #include "Student.h"
  
  void Book::newBook()
  {
      std::cout&lt;&lt;"Enter the Book Name: \n";
      getline(std::cin,name);
      std::cin.ignore();
      std::cout&lt;&lt;"Enter the Name of Author: \n";
      getline(std::cin,author);
      std::cin.ignore();
      std::cout&lt;&lt;"Enter the book barcode: \n";
      getline(std::cin,barcode);
      std::cin.ignore();
      std::cout&lt;&lt;"Book Added to the library\n";
  }
  void Book::report()
  {
      std::cout&lt;&lt;"______________"&lt;&lt;" __________________"&lt;&lt;" _________________\n";
      std::cout&lt;&lt;"|Name of Book| "&lt;&lt;"|Director of Book| "&lt;&lt;"|Barcode of Book| \n";
      std::cout&lt;&lt;name&lt;&lt;author&lt;&lt;barcode&lt;&lt;"\n";
  }
  </code></pre>
  
  <p>Media.h</p>
  
  <pre><code>#ifndef MEDIA_H_
  #define MEDIA_H_
  
  #include &lt;string&gt;
  
  class Media
  {
  public:
      std::string barcode;
      std::string name;
      std::string num;
  
      std::string retBarcode()
          {
              return barcode;
          }
  };
  
  #endif 
  </code></pre>
  
  <p>Library.h</p>
  
  <pre><code>#ifndef _Library_H_
  #define _Library_H_
  
  #include &lt;string&gt;
  #include &lt;stdio.h&gt;
  
  class Library
  {
  public:
      void insertBook();
      void showallBooks();
  };
  
  #endif
  </code></pre>
  
  <p>Library.cpp</p>
  
  <pre><code>void Library::insertBook()
  {
  
      std::ofstream bookData("book.dat", std::ios::app);
      if(!bookData)
      {
          std::cout&lt;&lt;"cannot open the user.dat file. \n";
      }
      book1.newBook();
      bookData &lt;&lt; book1.name &lt;&lt; book1.author &lt;&lt; book1.barcode&lt;&lt;std::endl;
      bookData.close();
  }
  void Library::showallBooks()
  {
      std::ifstream bookData("book.dat");
      if(!bookData)
      {
          std::cout&lt;&lt;"Error: File could not be opened. \n";
      }
  
      while(!bookData.eof())
      {
          bookData &gt;&gt; book1.name &gt;&gt; book1.author &gt;&gt; book1.barcode;
          book1.report();
      }
  
      bookData.close();
  }
  </code></pre>