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

using namespace std;

class book
{
    public:
    string title;
    string author;
    string publisher;
    int year;
    string isbn;
    book* next;
    book(string, string, string, int, string, book*);
};

book::book(string tempTitle, string tempAuthor, string tempPublisher, int tempYear, string tempIsbn, book* tempNext)
{
    title=tempTitle;
    author=tempAuthor;
    publisher=tempPublisher;
    year=tempYear;
    isbn=tempIsbn;
    next=tempNext;
}

typedef book* bookPtr;

void getline(istream &stream, string &str, char delimiter)
{    char temp[500];

    stream.get(temp, 500, delimiter);
    stream.ignore(500, delimiter);
    str = temp;
}

void getline(istream &stream, int &num, char delimiter)
{    int temp;

    stream >> temp;
    stream.ignore(500, delimiter);
    num= temp;
}

void readFile(bookPtr &root);
void insert (bookPtr &root);
void delTitle(bookPtr &root);
bookPtr locateNode(bookPtr temp, string titl);
void delIsbn(bookPtr &root);
bookPtr locateNodeIsbn(bookPtr temp, string isb);
void searchIsbn(bookPtr temp);
void printList(bookPtr temp);
void printAuthor(bookPtr temp);
void saveFile(bookPtr temp);
int countNodes(bookPtr temp);


void readFile(bookPtr &root)
{
    int numBooks, yea;
    string titl, aut, pub, isb;
    ifstream infile ("books.txt", ios::in);
    infile >> numBooks;
    infile.ignore(500,'\n');
    for (int count = 0; count < numBooks; count++)
    {
        getline(infile, titl, '\n');
        getline(infile, aut, '\n');
        getline(infile, pub, '\n');
        getline(infile,yea, '\n');
        getline(infile, isb, '\n');

        root = new book (titl, aut, pub, yea, isb, root);
    }
}

void insert (bookPtr &root)
{
    string titl, aut, pub, isb;
    int yea;

    cout << "Title:\t\t\t";
    cin.ignore(500,'\n');
    getline(cin, titl, '\n');
    cout << "Author:\t\t\t";
    getline(cin, aut, '\n');
    cout << "Publisher:\t\t";
    getline(cin,pub, '\n');
    cout << "Year:\t\t\t";
    getline(cin,yea, '\n');
    cout << "ISBN:\t\t\t";
    getline(cin, isb, '\n');

    root = new book (titl, aut, pub, yea, isb, root);
}

void delTitle(bookPtr &root)
{
    string titl;

    cout << "Book Title:\t\t\t";
    cin.ignore(500,'\n');
    getline(cin, titl, '\n');

    bookPtr p = locateNode(root, titl);

    if (p == NULL)
        cout << "\nDeletion cannot be done.\n\n";
    else if (root == p)
        root = p->next;
    else
    {
        bookPtr q = root;
        while (q->next != p)
            q = q->next;
        q->next = p->next;
    }
    delete p;
}

bookPtr locateNode(bookPtr temp, string titl)
{
    while (temp != NULL)
    {
        if (temp->title == titl)
        {
            return temp;
        }
        temp = temp->next;
    }
    return NULL;
}

void delIsbn(bookPtr &root)
{
    string isb;

    cout << "Book ISBN:\t\t\t";
    cin.ignore(500,'\n');
    getline(cin, isb, '\n');

    bookPtr p = locateNodeIsbn(root, isb);

    if (p == NULL)
        cout << "\nDeletion cannot be done.\n\n";
    else if (root == p)
        root = p->next;
    else
    {
        bookPtr q = root;
        while (q->next != p)
            q = q->next;
        q->next = p->next;
    }
    delete p;
}

bookPtr locateNodeIsbn(bookPtr temp, string isb)
{
    while (temp != NULL)
    {
        if (temp->isbn == isb)
        {
            return temp;
        }
        temp = temp->next;
    }
    return NULL;
}

void searchIsbn(bookPtr temp)
{
    string isb;

    cout << "Book ISBN:\t\t\t";
    cin.ignore(500,'\n');
    getline(cin, isb, '\n');

    while (temp != NULL)
    {
        if (isb == temp->isbn)
        {
            cout << temp->title << "\n";
            cout << temp->author << "\n";
            cout << temp->publisher << "\n";
            cout << temp->year << "\n";
            cout << temp->isbn << "\n\n";
        }
        temp = temp->next;
    }
    cout << "\n";
}

void printList(bookPtr temp)
{
    while (temp != NULL)
    {
        cout << temp->title << "\n";
        cout << temp->author << "\n";
        cout << temp->publisher << "\n";
        cout << temp->year << "\n";
        cout << temp->isbn << "\n\n";
        temp = temp->next;
    }
    cout << "\n";
}

void printAuthor(bookPtr temp)
{
    string aut;

    cout << "Author name:\t\t\t";
    cin.ignore(500,'\n');
    getline(cin, aut, '\n');

    while (temp != NULL)
    {
        if (temp->author == aut)
        {
            cout << temp->title << "\n";
            cout << temp->author << "\n";
            cout << temp->publisher << "\n";
            cout << temp->year << "\n";
            cout << temp->isbn << "\n\n";
        }
        temp = temp->next;
    }
    cout << "\n";
}

void saveFile(bookPtr temp)
{
    int count = countNodes(temp);
    ofstream outFile("saved.txt",ios::out);

    outFile << count << "\n";
    while (temp != NULL)
    {
        outFile << temp->title << "\n";
        outFile << temp->author << "\n";
        outFile << temp->publisher << "\n";
        outFile << temp->year << "\n";
        outFile << temp->isbn << "\n";
        temp = temp->next;
    }
    cout << "\n";
}

int countNodes(bookPtr temp)
{
    int countB = 0;
    while (temp != NULL)
    {
        countB++;
        temp = temp->next;
    }
    return countB;
}
int main()
{
    int choice;
    bookPtr root = NULL;
    readFile(root);

    do
    {
        cout << "\t\t<<=========================>>";
        cout << "\n\t\t>>LIBRARY MANAGEMENT SYSTEM<<";
        cout << "\n\t\t<<=========================>>";
        cout << "\n\nMenu: Select your option\n\n";
        cout << "(1) Add a book to the list\n";
        cout << "(2) Delete a book based on Title\n";
        cout << "(3) Delete a book based on ISBN\n";
        cout << "(4) Search for a book by ISBN.\n";
        cout << "(5) List all books.\n";
        cout << "(6) List all books by an author.\n";
        cout << "(7) Quit.\n\n";
        cout << "Enter your choice ---> ";

        cin >> choice;

        if (1 <= choice && choice <= 6)
        {
            switch (choice)
            {
            case 1:
                insert(root);
                break;
            case 2:
                delTitle(root);
                break;
            case 3:
                delIsbn(root);
                break;
            case 4:
                searchIsbn(root);
                break;
            case 5:
                printList(root);
                break;
            case 6:
                printAuthor(root);
                break;
            default:
                cout << "Invalid choice.  Enter again.\n\n";
                break;
            }
        }
    }
    while (choice != 7);
    saveFile(root);
    return 0;
} 

C++ Online Compiler

Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!

Read inputs from stdin

OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string name;
    cout << "Enter name:";
    getline (cin, name);
    cout << "Hello " << name;
    return 0;
}

About C++

C++ is a widely used middle-level programming language.

  • Supports different platforms like Windows, various Linux flavours, MacOS etc
  • C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction.
  • Case-sensitive
  • C++ is a compiler based language
  • C++ supports structured programming language
  • C++ provides alot of inbuilt functions and also supports dynamic memory allocation.
  • Like C, C++ also allows you to play with memory using Pointers.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition If-Else is used.

if(conditional-expression) {
   //code
}
else {
   //code
}

You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to If-Else-If ladder.

switch(conditional-expression){    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
......    
    
default:     
 code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  //code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while (condition) {  
// code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {  
 // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.

How to declare a Function:

return_type function_name(parameters);

How to call a Function:

function_name (parameters)

How to define a Function:

return_type function_name(parameters) {  
 // code
}