0

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

#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

#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

#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

#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

#ifndef _Library_H_
#define _Library_H_

#include <string>
#include <stdio.h>

class Library
{
public:
    void insertBook();
    void showallBooks();
};

#endif

Library.cpp

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();
}
manuk
  • 23
  • 3
  • Shouldn't you add some escaping? What happens if the name of the book is "Harry Potter" (has space character). A shot in the dark: what happens if you replace `std::endl` with `\n`? – Lord Zsolt Apr 09 '15 at 11:31
  • 1
    http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong – Barmar Apr 09 '15 at 11:31
  • `std::cin >> name;` I don't see any declaration of a free-floating `name` variable. Give us code that will compile. – AndyG Apr 09 '15 at 12:18

1 Answers1

0

If you are going to be extracting data from a file using the >> there needs to white space in between the data in the file. Currently you are using:

bookData << book1.name << book1.author << book1.barcode<<std::endl;

Which puts all of your data on one line with nothing between them. So "foo" "foobar" "barcode" becomes "foofoobarbarcode" in the file which is going to be imposible to read back in using the >> operator. If you change your code to:

bookData << book1.name << " " << book1.author << " " << book1.barcode << std::endl;

Now the data will be seperated by spaces and can easily read back in.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402