Why is my program writing to the same line in .dat file

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.

Can some please tell me what I am doing wrong.

I have included some of the code.
Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    #include <iostream>
    #include <string>
    #include "Library.h"
    
    int main() {
        Library lib1;
    
    while(1)
    {
       std::cout << "*   1              Add a new book                  *\n";
       std::cout << "*   2              Show all books                  *\n";
    
       std::cin >> name;
       bookOption = name;
    
       switch(bookOption)
       {
         case '1':
         {
           lib1.insertBook();
           break;
         }
         case '2':
         {
           lib1.showallBooks();
           break;
         }
    }
    return 0;
    };


Book.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    #ifndef BOOK_H_
    #define BOOK_H_
    
    #include <string>
    #include <iostream>
    #include <fstream>
    #include "Media.h"
    
    class Book : public Media
    {
    public:
        std::string author;
    
    public:
        void newBook();
    void report();
    };


Book.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    #include <string>
    #include "Book.h"
    #include "Student.h"
    
    void Book::newBook()
    {
        std::cout<<"Enter the Book Name: \n";
        getline(std::cin,name);
        std::cin.ignore();
        std::cout<<"Enter the Name of Author: \n";
        getline(std::cin,author);
        std::cin.ignore();
        std::cout<<"Enter the book barcode: \n";
        getline(std::cin,barcode);
        std::cin.ignore();
        std::cout<<"Book Added to the library\n";
    }
    void Book::report()
    {
        std::cout<<"______________"<<" __________________"<<" _________________\n";
        std::cout<<"|Name of Book| "<<"|Director of Book| "<<"|Barcode of Book| \n";
        std::cout<<name<<author<<barcode<<"\n";
    }

Media.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    #ifndef MEDIA_H_
    #define MEDIA_H_
    
    #include <string>
    
    class Media
    {
    public:
        std::string barcode;
        std::string name;
        std::string num;
    
        std::string retBarcode()
        	{
        		return barcode;
        	}
    };

    #endif  

Library.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    #ifndef _Library_H_
    #define _Library_H_
    
    #include <string>
    #include <stdio.h>
    
    class Library
    {
    public:
        void insertBook();
        void showallBooks();
    };
    
    #endif 


Library.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    void Library::insertBook()
    {
        
        std::ofstream bookData("book.dat", std::ios::app);
        if(!bookData)
        {
            std::cout<<"cannot open the user.dat file. \n";
        }
        book1.newBook();
        bookData << book1.name << book1.author << book1.barcode<<std::endl;
        bookData.close();
    }
    void Library::showallBooks()
    {
        std::ifstream bookData("book.dat");
        if(!bookData)
        {
            std::cout<<"Error: File could not be opened. \n";
        }
        
        while(!bookData.eof())
        {
            bookData >> book1.name >> book1.author >> book1.barcode;
            book1.report();
        }
        
        bookData.close();
    }
However the system is having problem writing the user input into the dat file.
Hm, I'd think that it is less the system and more the programmer...

You should write each item with a new line. And then read each item with getline(...) similar to newBook():

bookData << book1.name << '\n' << book1.author << '\n' << book1.barcode<<std::endl;
Topic archived. No new replies allowed.