C++ Program to Build Bookshop Store Management System on Command Line

In this article, You are going to execute the task of “C++ Program to Build Bookshop Store Management System on Command Line”. Yes, We can see the possibilities to perform this task below.

C++ Program to Build Bookshop Store Management System on Command Line

C++ Program to Build Bookshop Store Management System on Command Line

Program Code

#include<fstream>
#include<conio.h>
#include<string.h>
#include<iomanip>
#include<iostream>
 
using namespace std;
 
class book
{
          char book_number[30];
          char book_name[50];
          char author_name[20];
          int num_copies;
 
  public:
          void get_book_details()
          {
          			cout<<"nENTER DETAILS ABOUT BOOK U WANT TO PURCHASEn";
                    cout<<"nEnter The Book Number: ";
                    cin>>book_number;
                    cout<<"nEnter The Name of The Book: ";
                     cin.ignore();
                    cin.getline(book_name,50);
                    cout<<"nEnter The Author's Name: ";
                    cin.ignore();
                    cin.getline(author_name,50);
                    fflush(stdin);
           			cout<<"nEnter No. Of Copies : ";
    				cin>>num_copies;
          }
 
          void show_book()
          {
                    cout<<"nBook Number: "<<book_number;
                    cout<<"nBook Name: "<<book_name;
                    cout<<"nAuthor's Name: "<<author_name;
                    cout<<"nCOPIES : "<<num_copies;
          }
          void modify_book()
          {
                    cout<<"nBook number : "<<book_number;
                    cout<<"nModify Book Name : ";
                    cin.ignore();
                    cin.getline(book_name,50);
                    cout<<"nModify Author's Name: ";
                    cin.ignore();
                    cin.getline(author_name,50);
                    fflush(stdin);
       				cout<<"nEnter No. Of Copies : ";
    				cin>>num_copies;
          }
          char* getbooknumber()
          {
                    return book_number;
          }
          void report()
          {cout<<book_number<<setw(30)<<book_name<<setw(30)<<author_name<<setw(30)<<num_copies<<endl;}
 
};        
 
       
 
fstream fp;
book bk;
void write_book()
{
          system("cls");
          int more_or_main;
          fp.open("book.dat",ios::out|ios::app);
          do
          {
                    bk.get_book_details();
                    fp.write((char*)&bk,sizeof(book));
                    cout<<"nPress 1 to add more books.";
                    cout<<"nPress 2 to return to main menu.n";
                    cout<<"Enter: ";
                    cin>>more_or_main;
          }while(more_or_main == 1);
          fp.close();
}
 
void display_a_book(char n[])
{
          system("cls");
          cout<<"nBOOK DETAILSn";
          int check=0;
          fp.open("book.dat",ios::in);
          while(fp.read((char*)&bk,sizeof(book)))
          {
                    if(strcmpi(bk.getbooknumber(),n)==0)
                    {
                               bk.show_book();
                              check=1;
                    }
          }
          fp.close();
          if(check==0)
                    cout<<"nnBook does not exist";
        getch();
}
 
void modify_book()
{
          system("cls");
          char n[20];
          int found=0;
          cout<<"nntMODIFY BOOK";
          cout<<"nntEnter The book number: ";
          cin>>n;
          fp.open("book.dat",ios::in|ios::out);
          while(fp.read((char*)&bk,sizeof(book)) && found==0)
          {
                    if(strcmpi(bk.getbooknumber(),n)==0)
                    {
                               bk.show_book();
                               cout<<"nEnter The New Details of book"<<endl;
                               bk.modify_book();
                               int pos=-1*sizeof(bk);
                              fp.seekp(pos,ios::cur);
                              fp.write((char*)&bk,sizeof(book));
                              cout<<"nnt Record Updated Successfully...";
                              found=1;
                    }
          }
          fp.close();
          if(found==0)
                    cout<<"nn Record Not Found ";
          getch();
}
 
void delete_book()
{
          system("cls");
          char n[20];
          int flag=0;
          cout<<"nnntDELETE BOOK";
          cout<<"nnEnter The Book's number You Want To Delete: ";
          cin>>n;
          fp.open("book.dat",ios::in|ios::out);
          fstream fp2;
          fp2.open("Temp.dat",ios::out);
          fp.seekg(0,ios::beg);
          while(fp.read((char*)&bk,sizeof(book)))
          {
                    if(strcmpi(bk.getbooknumber(),n)!=0)  
                    {
                               fp2.write((char*)&bk,sizeof(book));
                    }
                    else
                              flag=1;
          }
          fp2.close();
          fp.close();
          remove("book.dat");
          rename("Temp.dat","book.dat");
          if(flag==1)
                    cout<<"nntRecord Deleted ..";
          else
                    cout<<"nnRecord not found";
          getch();
}
 
void display_allb()
{
          system("cls");
          fp.open("book.dat",ios::in);
          if(!fp)
          {
                    cout<<"ERROR!!! FILE COULD NOT BE OPEN ";
                    getch();
                    return;
          }
          cout<<"nnttBook LISTnn";
         cout<<"============================================================================================n";
          cout<<"Book Number"<<setw(20)<<"Book Name"<<setw(25)<<"Author"<<setw(25)<<"Copies"<<endl;
          cout<<"============================================================================================n";
          while(fp.read((char*)&bk,sizeof(book)))
          {
                    bk.report();
          }
          fp.close();
          getch();
}
 
void intro()
{
     system("color 06");
     system("cls");
  cout<<"tttt*t*";
  cout<<"tttt**t**";
   cout<<"tttt***t***";
   cout<<"tttt****t****";
   cout<<"tttt*****t*****";
    cout<<"tttt******t******";
   cout<<"tttt*******t*******";
   cout<<"tttt*******t*******";
   cout<<"tttt******t******";
   cout<<"tttt*****t*****";
   cout<<"tttt****t****";
   cout<<"tttt***t***";
   cout<<"tttt**t**";
   cout<<"tttt*t*";
}
 
 
int main()
{
          int option = 0;
          for(;;)
          {
               intro();
            cout<<"tt@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";
                cout<<"nttPress 1 to TO BUY";
                cout<<"nttPress 2 to SHOW ALL BOOKS";
                cout<<"nttPress 3 to CHECK AVAILABILITY";
                cout<<"nttPress 4 to MODIFY BOOKS";
                cout<<"nttPress 5 to DELETE BOOKS";
                cout<<"nttPress 6 to Exit";
 			cout<<"ntt@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@n";
 			cout<<"nttOption: ";
 			cin>>option;
 			switch(option)
          	{
                    case 1:  system("cls");
                                         write_book();
                                                   break;
                               case 2: display_allb();
                                 break;
                    case 3:
                              char num[20];
                               system("cls");
                              cout<<"nntPlease Enter The book No. ";
                              cin>>num;
                              display_a_book(num);
                              break;
                    case 4: modify_book();break;
                    case 5: delete_book();break;
                    case 6: exit(0);
                               break;
                    default:cout<<"a";
          }     
           
          }
}

Final Words

I hope this article “C++ Program to Build Bookshop Store Management System on Command Line” will be beneficial to you. If you need any help regarding this article then let us know via the comment section. We are ready to help you. Share this article with other C/C++ developers to help them.

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment