vendredi 1 mars 2019

Add record to file and read it back

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>


using namespace std;

class Book
{
    public:
        char ISBN [5];
        char Title [20];
        char authorName [20];
        char Price [10];
        char Year [10];
        char NumOfPages [10];
        char delimiter = ',';
};


void AddBook ()
{
    fstream file;
    file.open("Records.txt", ios::out|ios::app);
    Book b;
    cout << "Enter ISBN: " << endl;
    cin>>b.ISBN;
    cin.ignore();
    cout << "Enter Title: " << endl;
    cin.getline(b.Title,sizeof(b.Title));
    cout << "Enter Author's Name: " << endl;
    cin.getline(b.authorName , sizeof(b.authorName));
    cout << "Enter Price: " << endl;
    cin.getline(b.Price,sizeof(b.Price));
    cout << "Enter Year: " << endl;
    cin.getline(b.Year,10);
    cout << "Enter Number Of Pages: " << endl;
    cin.getline(b.NumOfPages , sizeof(b.NumOfPages));
    cin.ignore();
    file.write(reinterpret_cast<char*>(&b), sizeof(b));
    file.write(reinterpret_cast<char*>(&b.delimiter), sizeof(b.delimiter));
    file.close();

}


void DeleteBook ()
{

}
void UpdateBook ()
{

}
void PrintBook ()
{

}
void PrintAll ()
{
    ifstream file;
    file.open("Records.txt", ios::in);
    Book b;
    while (!file.eof())
    {
        cout << "ISBN :" << b.ISBN <<endl;
        cout << "Title :" << b.Title <<endl;
        cout << "Author's Name :" << b.authorName <<endl;
        cout << "Price :" << b.Price <<endl;
        cout << "Year :" << b.Year <<endl;
        cout << "Number of Pages "<< b.NumOfPages <<endl;
        file.read(reinterpret_cast <char*> (&b), sizeof(b));
    }
    file.close();
}

int main()
{
    int choice;
    do
    {
        cout << "The Menu for Book Store" << endl;
        cout << "1. Add Book: " << endl;
        cout << "2. Delete Book: " << endl;
        cout << "3. Update Book:" << endl;
        cout << "4. print a Book:" <<endl;
        cout << "5. print all Books " << endl;
        cout << "6. Exit the program "<<endl;
        cout << "Enter your choice here "<<endl;
        cin >> choice;

        switch (choice)
        {
            case 1:
                AddBook();
                break;
            case 2:
                DeleteBook();
                break;
            case 3:
                UpdateBook();
                break;
            case 4:
                PrintBook();
                break;
            case 5:
                PrintAll();
                break;
            default:
                cout << "Invalid Selection" << endl;
        }
    }
    while
        (choice != 6);

    return 0;
}

The output looks strange in file and the output is being outputted twice on in strange chars and the other is good but ISBN is attached to title i need a solution please how i can fix it as it's not obvious where's the logical error here for me

Output

 ISBN :
 ╞3╧v`≡o F╙v└²a
Title :
≡o F╙v└²a
Author's Name 
:
Price :
F╙
Year :
oÇ≡o╘≡o   
 
Number of Pages   
 
ISBN :12345Jungle House
Title :Jungle House
Author's Name :ASad asad
Price :240
Year :2019
Number of Pages 300

Aucun commentaire:

Enregistrer un commentaire