#include<iostream>
#include<conio.h>
#include<string.h>
#include<iomanip>
#include<iostream>
#include<fstream>
#include<Windows.h>
using namespace std;

class book
{
          char book_number[30];
          char book_name[50];
          char author_name[20];
  public:
          void create_book()
          {
                    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);
                    cout<<"\t\t\n\nBook Created Successfully...";
          }

          void show_book()
          {
                    cout<<"\nBook Number: "<<book_number;
                    cout<<"\nBook Name: "<<book_name;
                    cout<<"\nAuthor's Name: "<<author_name;
          }
          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);
          }
          char* getbooknumber()
          {
                    return book_number;
          }
          void report()
          {cout<<book_number<<setw(30)<<book_name<<setw(30)<<author_name<<endl;}

};  
class student
{

          char ID_number[20];
          char Student_name[20];
          char stbno[6];
          int token;
public:
          void create_student()
          {
                    cout<<"\nEnter The ID Number ";
                    cin>>ID_number;
                    cout<<"\n\nEnter The Name of The Student: ";
                    cin>>Student_name;
                    token=0;
                    stbno[0]='/0';
                    cout<<"\t\t\n\nStudent Record Created Successfully...";
          }
          void show_student()
          {
                    cout<<"\nID Number: "<<ID_number;
                    cout<<"\nStudent Name: ";
                    puts(Student_name);
                    cout<<"\nNo of Book issued: "<<token;
                    if(token==1)
                               cout<<"\nBook No "<<stbno;
          }
          void modify_student()
          {
                    cout<<"\nID Number: "<<ID_number;
                    cout<<"\nModify Student Name: ";
                    cin.ignore();
                    cin.getline(Student_name,50);
          }
          char* get_ID_number()
          {
                    return ID_number;
          }
          char* retstbno()
          {
                    return stbno;
          }
          int rettoken()
          {
                    return token;
          }
          void addtoken()
          {token=1;}
          void resettoken()
          {token=0;}
          void getstbno(char t[])
          {
                    strcpy(stbno,t);
          }
          void report()
          {cout<<"\t"<<ID_number<<setw(20)<<Student_name<<setw(10)<<token<<endl;}
}; 
class library
{ public:
    library()
    {
    };

    book bk;
    student st;
    fstream fp,fp1;
    void write_book()
{
          system("cls");
          int more_or_main;
          fp.open("book.dat",ios::out|ios::app);
          do
          {
                    bk.create_book();
                    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 write_student()
{
        system("cls");
          int more_or_main;
          fp.open("student.dat",ios::out|ios::app);
          do
          {
                    st.create_student();
                    fp.write((char*)&st,sizeof(student));
                    cout<<"\nPress 1 to add more students.";
                    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 DETAILS\n";
          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<<"\n\nBook does not exist";
        getch();
}
void display_a_student(char n[])
{
          system("cls");
          cout<<"\nSTUDENT DETAILS\n";
          int check=0;
          fp.open("student.dat",ios::in);
          while(fp.read((char*)&st,sizeof(student)))
          {
                    if((strcmpi(st.get_ID_number(),n)==0))
                    {
                               st.show_student();
                               check=1;
                    }
          }
          fp.close();
          if(check==0)
                    cout<<"\n\nStudent does not exist";
          getch();
}
void modify_book()
{
          system("cls");
          char n[20];
          int found=0;
          cout<<"\n\n\tMODIFY BOOK";
          cout<<"\n\n\tEnter 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<<"\n\n\t Record Updated Successfully...";
                              found=1;
                    }
          }
          fp.close();
          if(found==0)
                    cout<<"\n\n Record Not Found ";
          getch();
}
void modify_student()
{
          system("cls");
          char n[20];
          int found=0;
          cout<<"\n\n\tMODIFY STUDENT RECORD... ";
          cout<<"\n\n\tEnter Student's ID number: ";
          cin>>n;
          fp.open("student.dat",ios::in|ios::out);
          while(fp.read((char*)&st,sizeof(student)) && found==0)
          {
                    if(strcmpi(st.get_ID_number(),n)==0)
                    {
                               st.show_student();
                               cout<<"\nEnter The New Details of student"<<endl;
                               st.modify_student();
                               int pos=-1*sizeof(st);
                               fp.seekp(pos,ios::cur);
                               fp.write((char*)&st,sizeof(student));
                               cout<<"\n\n\t Record Updated Successfully...";
                               found=1;
                    }
          }
          fp.close();
          if(found==0)
                    cout<<"\n\n Record Not Found ";
          getch();
}
void delete_student()
{
          system("cls");
          char n[20];
          int flag=0;
          cout<<"\n\n\n\tDELETE STUDENT";
          cout<<"\n\nEnter The ID number of the Student You Want To Delete: ";
          cin>>n;
          fp.open("student.dat",ios::in|ios::out);
          fstream fp2;
          fp2.open("Temp.dat",ios::out);
          fp.seekg(0,ios::beg);
          while(fp.read((char*)&st,sizeof(student)))
          {
                    if(strcmpi(st.get_ID_number(),n)!=0)
                              fp2.write((char*)&st,sizeof(student));
                    else
                              flag=1;
          }
          fp2.close();
          fp.close();
          remove("student.dat");
          rename("Temp.dat","student.dat");
          if(flag==1)
                    cout<<"\n\n\tRecord Deleted ..";
          else
                    cout<<"\n\nRecord not found";
        getch();
}
void delete_book()
{
          system("cls");
          char n[20];
          int flag=0;
          cout<<"\n\n\n\tDELETE BOOK";
          cout<<"\n\nEnter 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<<"\n\n\tRecord Deleted ..";
          else
                    cout<<"\n\nRecord not found";
          getch();
}
void display_all_students()
{
              system("cls");
          fp.open("student.dat",ios::in);
          if(!fp)
          {
                    cout<<"ERROR!!! FILE COULD NOT BE OPEN ";
                    getch();
                    return;
          }
          cout<<"\n\n\t\tSTUDENT LIST\n\n";
          cout<<"==================================================================\n";
          cout<<"\tID Number."<<setw(10)<<"Name"<<setw(20)<<"Book Issued\n";
          cout<<"==================================================================\n";
          while(fp.read((char*)&st,sizeof(student)))
          {
                    st.report();
          }
          fp.close();
          getch();
}
void display_allb()
{
          system("cls");
          fp.open("book.dat",ios::in);
          if(!fp)
          {
                    cout<<"ERROR!!! FILE COULD NOT BE OPEN ";
                    getch();
                    return;
           }
          cout<<"\n\n\t\tBook LIST\n\n";
         cout<<"=========================================================================\n";
          cout<<"Book Number"<<setw(20)<<"Book Name"<<setw(25)<<"Author\n";
          cout<<"=========================================================================\n";
          while(fp.read((char*)&bk,sizeof(book)))
          {
                    bk.report();
          }
          fp.close();
          getch();
}
void issue_book()
{
          system("cls");
          char sn[20],bn[20];
          int found=0,flag=0;
   cout<<"\t\t\nBOOK ISSUE";
          cout<<"\n\n\tEnter student's ID number: ";
          cin>>sn;
          fp.open("student.dat",ios::in|ios::out);
          fp1.open("book.dat",ios::in|ios::out);
          while(fp.read((char*)&st,sizeof(student)) && found==0)
          {
                    if(strcmpi(st.get_ID_number(),sn)==0)
                    {
                               found=1;
                               if(st.rettoken()==0)
                               {
                                        cout<<"\n\n\tEnter book number: ";
                                         cin>>bn;
                                         while(fp1.read((char*)&bk,sizeof(book))&& flag==0)
                                         {
                                                   if(strcmpi(bk.getbooknumber(),bn)==0)
                                                 {
                                                           bk.show_book();
                                                             flag=1;
                                                             st.addtoken();
                                                             st.getstbno(bk.getbooknumber());
                                                             int pos=-1*sizeof(st);
                                                             fp.seekp(pos,ios::cur);
                                                             fp.write((char*)&st,sizeof(student));
                                                             cout<<"\n\n\t Book issued successfully...";
                                                   }
                                        }
                                        if(flag==0)
                                                   cout<<"Book number does not exist";
                               }
                              else
                                        cout<<"You have not returned the last book ";
                    }
          }
          if(found==0)
                    cout<<"Student record not exist...";
          getch();
          system("cls");
          fp.close();
          fp1.close();
}
void book_return()
{
    system("cls");
    char sn[20],bn[20];
    int found=0, flag=0, day, fine;
   cout<<"\t\tRETURN BOOKS\n";
    cout<<"\n\n\tEnter The student’s ID Number: ";
    cin>>sn;
    fp.open("student.dat",ios::in|ios::out);
    fp1.open("book.dat",ios::in|ios::out);
    while(fp.read((char*)&st,sizeof(student)) && found==0)
       {
                       if(strcmpi(st.get_ID_number(),sn)==0)
              {
                        found=1;
                        if(st.rettoken()==1)
                        {
                               while(fp1.read((char*)&bk,sizeof(book))&& flag==0)
                               {
                                         if(strcmpi(bk.getbooknumber(),st.retstbno())==0)
                               {
                                         bk.show_book();
                                         flag=1;
                                         cout<<"\n\nBook returned in no. of days";
                                         cin>>day;
                                         if(day>15)
                                         {
                                            fine=(day-15)*1;
                                            cout<<"\n\nThe Book is last. You have to pay a fine of  "<<fine;
                                         }
                                                   st.resettoken();
                                                   int pos=-1*sizeof(st);
                                                   fp.seekp(pos,ios::cur);
                                                   fp.write((char*)&st,sizeof(student));
                                                  cout<<"\n\n\t Book returned successfully...";
                               }
                        }
                      if(flag==0)
                        cout<<"Book number does not exist";
                          }
                     else
                               cout<<"No book is issued..please check!!";
                    }
             }
      if(found==0)
          cout<<"Student record not exist...";
          getch();
          system("cls");
  fp.close();
  fp1.close();
  }
void intro()
{

        system("color 30");
        cout<<"\n\n\t\t\t LIBRARY MANAGEMENT SYSTEM ";
        cout<<"\n\t\t\t******************************\n\n"<<endl;
        Sleep(600);
        cout<<"\n Group Members: "<<endl;
        Sleep(400);


}
void book_menu()
{  CHAR ch;
          int option;
          cout<<endl;
            cout<<"\t\t==========================================";

                cout<<"\n\t\tPress 1 to CREATE BOOKS";
                cout<<"\n\t\tPress 2 to DISPLAY ALL STUDENTS RECORD";
                cout<<"\n\t\tPress 3 to MODIFY BOOKS";
                cout<<"\n\t\tPress 4 to DELETE BOOKS";
                cout<<"\n\t\tPress 5 to MODIFY STUDENT RECORD";
                cout<<"\n\t\tPress 6 to DELETE STUDENT RECORD";
                cout<<"\n\t\t Press 7 to search specific student";
                cout<<"\n\t\tPress 7 to GO BACK TO MAIN MENU"<<endl;
               cout<<"====================================================";

 cout<<"\n\t\tOption: ";
 cin>>option;

     switch(option)
          {
                    case 1:  system("cls");
                                         write_book();
                                                   break;
                    case 2: display_all_students();
                                 break;
                    case 3: modify_book();break;
                    case 4: delete_book();break;
                    case 5: modify_student();break;
                    case 6: delete_student();break;
                    case 7: char num[20];
                               system("cls");
                        cout<<"\n\n\tPlease Enter The ID Number: ";
                              cin>>num;
                         display_a_student(num);
                         break;

                    case 8:system("cls");
                    default:cout<<"\a";
          }

 }
void student_menu()
{
          int option;
          cout<<endl;
           cout<<"\t\t================================================"<<endl;
           cout<<"\t\t================================================"<<endl;
                cout<<"\n\t\tPress  1 to REGISTRATION OF STUDENT RECORD"<<endl;
                cout<<"\n\t\tPress  2 to ISSUE BOOKS"<<endl;
                cout<<"\n\t\tPress  3 to RETURN BOOKS"<<endl;
                cout<<"\n\t\tPress  4 to DISPLAY ALL BOOKS"<<endl;
                cout<<"\n\t\tPress  5 to DISPLAY SPECIFIC BOOK"<<endl;
                cout<<"\n\t\tPress 6 to GO BACK TO MAIN MENU"<<endl;
                cout<<"\n\t\t=========================================\n";
 cout<<endl<<endl<<"Enter option"<<endl;
 cin>>option;
 switch(option)
          {
                    case 1:  system("cls");
                            write_student();
                             break;
                    case 2: system("cls");

                        issue_book();
                            break;
                    case 3:book_return();
                        break;
                    case 4:
                        display_allb();
                        break;
                    case 5:
                              char num[20];
                               system("cls");
                              cout<<"\n\n\tPlease Enter The book No. ";
                              cin>>num;
                              display_a_book(num);
                              break;


                    default:cout<<"\a";
          }
}};
int main()
{ library l;
          int option = 0;


                   l.intro();
                   cout<<endl<<endl;
         for(;;)
         {
             cout<<"         ***MENU OPTIONS***              "<<endl<<endl;
              cout<<"        1: Administration               "<<endl;
              cout<<"        2: Student module               "<<endl;
             cout<<"         Enter your choice "<<endl;
           cin>>option;

 switch(option)
                    {
                               case 1:system("cls");
                                   l.book_menu();
                                          break;
                              case 2:system("cls");
                                  l.student_menu();
                                   break;
                              case 5:exit(0);
                                         break;
                              default :cout<<"\a";
                                         exit(0);
                    }
          }

}

By analysing it and writing it more efficiently.

#include<iostream>
#include<conio.h> // don't use to keep code portable
#include<string.h> // use <cstring>
#include<iomanip>
#include<iostream>
#include<fstream>
#include<Windows.h> // don't use to keep code portable
using namespace std; // don't use to avoid 'name clashes as code grows in size //

// and use ... .h and .cpp clas files and compile as a project //
// use C++ string every where to accomodate any string length 
// and change your file writes and reads to match //

Asking people to analyze over 500 lines of code for you is not a "nice" thing to do. As David W. suggests, use std::string objects instead of char arrays. They are much more efficient and C++ capable.

Another non-portable construct is system("cls"); - that is a Windows-only construct. If you do want to do that to clear the terminal, then you should use an

#ifdef _WIN32
   system("cls");
#else
   system ("clear");
#endif

conditional compilation block. That said, since you use this in many places, then you should define your own clear_screen() function that calls the appropriate code to clear the screen. Then, all you need in the rest of the code is to call clear_screen().

commented: I am sorry that you have to go through all this :P +0

@Hamza_9 - thanks for the up-vote. Not necessary but appreciated. Let's start small. Choose a function or method that you want to improve, post it here, and then we can work with that, and then go on to others.

Also, self-analysis of your own code can be very illuminating. So, along with the code, post your on analysis and critique of the code. Then we can have a real conversation about this.

Don't know if anybody mentioned this, but you could probably break some parts out into other source code files. Classes generally live in their own code file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.