-1

I have this C++ working C++ project.I'm really new to programming, learning c++. I would love to add a sorting feature in this project . For example, sorts out based on student id/book id. How I should approach the problem .OR any other features for this sort of projects. Sort by date: Sorts your tasks by due date. Sort by student id. Sort alphabetically: Sorts your tasks alphabetically by task name.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>

using namespace std;

class Book {
    int BookID;
    int BookISBN;
    char BookName[20];
    char BookAuthor[50];
    int BookRackNo;
    int BookSlotNo;
    float BookCost;
    char date[10];

  public:
    void getData ();
    void showData ();
    int getBookId () {
        return BookID;
}}
b;

void Book::getData ()
{
    cout << "\n\nEnter Book Details......\n";
    cout << "Enter Book ID : ";
    cin >> BookID;
    cout << "Enter Book ISBN : ";
    cin >> BookISBN;
    cout << "Enter Book Name : ";
    cin.ignore ();
    cin.getline (BookName, 20);
    cout << "Enter Book Author : ";
    cin.ignore ();
    cin.getline (BookAuthor, 50);
    cout << "Enter Book Location Rack #: ";
    cin >> BookRackNo;
    cout << "Enter Book Location Slot #: ";
    cin >> BookSlotNo;
    cout << "Enter Book Cost: $ ";
    cin >> BookCost;
    cout << "Enter Book Purchase Date MM/DD/YYYY: ";
    cin >> date;
    cout << endl;
}

void Book::showData ()
{
    cout << "\n\n.......Book Details......\n";
    cout << "Book ID : " << BookID << endl;
    cout << "Book ISBN : " << BookISBN << endl;
    cout << "Book Name : " << BookName << endl;
    cout << "Book Author : " << BookAuthor << endl;
    cout << "Book Location Rack #: " << BookRackNo << endl;
    cout << "Book Location Slot #: " << BookSlotNo << endl;
    cout << "Book Cost: $ " << BookCost << endl;
    cout << "Book Purchase Date: " << date << endl;
    cout << endl;
}

void addBook ()
{
    ofstream fout;
    fout.open ("Book.dat", ios::binary | ios::out | ios::app);
    b.getData ();
    fout.write ((char *) &b, sizeof (b));
    fout.close ();
    cout << "\n\nData Successfully Saved....\n";
}

void displayBook ()
{
    ifstream fin;
    fin.open ("Book.dat", ios::in | ios::binary);
    while (fin.read ((char *) &b, sizeof (b))) {
        b.showData ();
    }
    fin.close ();
//cout << "\n\nData Reading from File Successfully Done....\n";
}

void searchBook ()
{
    int n, flag = 0;
    ifstream fin;
    fin.open ("Book.dat", ios::in | ios::binary);
    cout << "Enter Book ID you want to search : ";
    cin >> n;

    while (fin.read ((char *) &b, sizeof (b))) {
        if (n == b.getBookId ()) {
            cout << "The details of Book ID " << n << " are:\n";
            b.showData ();
            flag++;
        }
    }
    fin.close ();
    if (flag == 0)
        cout << "The Book Id " << n << " not found....\n\n";
    // cout << "\n\nData Reading from File Successfully Done....\n";
}

void deleteBook ()
{
    int n, flag = 0, result;
    ifstream fin;
    ofstream fout, tout;

    fin.open ("Book.dat", ios::in | ios::binary);
    fout.open ("TempBook.dat", ios::out | ios::app | ios::binary);
    tout.open ("TrashBook.dat", ios::out | ios::app | ios::binary);

    cout << "Enter Book ID you want to move to Trash : ";
    cin >> n;

    while (fin.read ((char *) &b, sizeof (b))) {
        if (n == b.getBookId ()) {
            cout << "The Following Book ID " << n <<
                " has been moved to Trash:\n";
            b.showData ();
            tout.write ((char *) &b, sizeof (b));
            flag++;
        } else {
            fout.write ((char *) &b, sizeof (b));
        }
    }
    fout.close ();
    tout.close ();
    fin.close ();
    if (flag == 0)
        cout << "The Book ID " << n << " not found....\n\n";
    remove ("Book.dat");
    result = rename ("tempBook.dat", "Book.dat");
    cout << result;
}

void getTrashBook ()
{
    ifstream fin;
    fin.open ("TrashBook.dat", ios::in | ios::binary);
    while (fin.read ((char *) &b, sizeof (b))) {
        b.showData ();
    }
    fin.close ();
    //cout << "\n\nData Reading from Trash File Successfully Done....\n";
}

void modifyBookData ()
{
    int n, flag = 0, pos;
    fstream fio;

    fio.open ("Book.dat", ios::in | ios::out | ios::binary);

    cout << "Enter Book ID you want to Modify : ";
    cin >> n;

    while (fio.read ((char *) &b, sizeof (b))) {
        pos = fio.tellg ();
        if (n == b.getBookId ()) {
            cout << "The Following Book ID " << n <<
                " will be modified with new data:\n";
            b.showData ();
            cout << "\n\n Enter the New Details....\n";
            b.getData ();
            const __int64 Z = pos - static_cast < __int64 > (sizeof (b));
            fio.seekg (Z);
            //fio.seekg(pos-sizeof(b));
            fio.write ((char *) &b, sizeof (b));
            flag++;
        }
    }
    fio.close ();

    if (flag == 0)
        cout << "The Book ID " << n << " not found....\n\n";
}

void project ()
{
    int ch;
    do {
        system ("cls");
        cout << "======================================"
                "====================================\n";
        cout << "...............UNIVERSITY LIBRARY "
                "DATABASE MANAGEMENT SYSTEM..............\n";
        cout << "======================================"
                "====================================\n";
        cout << "0. Exit from Program\n";
        cout << "1. Add New Book\n";
        cout << "2. List all Books\n";
        cout << "3. Search a Book \n";
        cout << "4. Delete a Book \n";
        cout << "5. List of Deleted Books\n";
        cout << "6. Modify Details of a Book\n";
        cout << "Enter your choice : ";
        cin >> ch;
        system ("cls");
        switch (ch) {
        case 1:
            addBook ();
            break;
        case 2:
            displayBook ();
            break;
        case 3:
            searchBook ();
            break;
        case 4:
            deleteBook ();
            break;
        case 5:
            getTrashBook ();
            break;
        case 6:
            modifyBookData ();
            break;
        }
        system ("pause");
    } while (ch);
}

int main ()
{
    project ();
}
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85

1 Answers1

2

The C++ standard library already has an implementation for sorting sequences or containers: std::sort.

std::sort() works "out of the box" for types that already have a less (<) comparison operator, e. g. for int or std::string. But one can also provide a own comparison method that works on custom types. For example, if you want to sort a std::vector of Books, you could do it like this:

#include <algorithm> // provides std::sort
#include <vector>    // provides std::vector

...

std::vector<Books> all_books;

// ... (Fill all_books with some data here.)

std::sort(all_books.begin(), all_books.end(),
          // lambda expression for comparison
          [](const Book& a, const Book& b)
          {
              return a.BookID < b.BookID;
          }
);

This would sort the vector of Books by BookID.

Alternative option:

If you want to implement the operator < for Book instead, you could do this, too. This would eliminate the need to specify a comparison method for std::sort. A possible implementation - again sorting by BookId - could be:

bool Book::operator< (const Book& other) const
{
  return BookID < other.BookID;
}

That would allow you to use sort just like this:

std::vector<Books> all_books;

// ... (Fill all_books with some data here.)

std::sort(all_books.begin(), all_books.end());

Bonus round:

Sorting by multiple data members is possible, too. For example, if someone wanted to sort by BookCost first, and if the cost is equal by BookID, then the comparison method could look like this:

std::sort(all_books.begin(), all_books.end(),
          // lambda expression for comparison
          [](const Book& a, const Book& b)
          {
              // Try BookCost as first criterion.
              if (a.BookCost < b.BookCost)
                  return true;
              if (a.BookCost > b.BookCost)
                  return false;
              // BookCost is equal for both books
              // (or at least one BookCost is NaN,
              //  but let's not get into that here).
              // Let's use the BookID as tie-breaker.
              return a.BookID < b.BookID;
          }
);
Striezel
  • 3,693
  • 7
  • 23
  • 37