By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,983 Members | 2,082 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,983 IT Pros & Developers. It's quick & easy.

Library Management C++ program...?

SSJTactics
P: 3
When the info of student is displayed the No. of books issued is showing the value 0, even when book is issued. I cannot find the problem in the program.

Expand|Select|Wrap|Line Numbers
  1. //***************************************************************
  2. //                   HEADER FILE USED IN PROJECT
  3. //****************************************************************
  4.  
  5. #include<fstream.h>
  6. #include<conio.h>
  7. #include<stdio.h>
  8. #include<process.h>
  9. #include<string.h>
  10. #include<iomanip.h>
  11.  
  12. //***************************************************************
  13. //                   CLASS USED IN PROJECT
  14. //****************************************************************
  15.  
  16.  
  17.  
  18. class book
  19. {
  20.     char bno[6];
  21.     char bname[50];
  22.     char aname[20];
  23.   public:
  24.     void create_book()
  25.     {
  26.         cout<<"\n\t--------------NEW BOOK ENTRY--------------\n";
  27.         cout<<"\nEnter the book no.: ";
  28.         cin>>bno;
  29.         cout<<"\n\nEnter the name of the Book: ";
  30.         gets(bname);
  31.         cout<<"\n\nEnter the Author's name: ";
  32.         gets(aname);
  33.         cout<<"\n\n\nBook Created..";
  34.     }
  35.  
  36.     void show_book()
  37.     {
  38.         cout<<"\nBook No. : "<<bno;
  39.         cout<<"\nBook Name : ";
  40.         puts(bname);
  41.         cout<<"Author Name : ";
  42.         puts(aname);
  43.     }
  44.  
  45.     void modify_book()
  46.     {
  47.         cout<<"\nBook no. : "<<bno;
  48.         cout<<"\nModify Book name : ";
  49.         gets(bname);
  50.         cout<<"\nModify Author's name of Book : ";
  51.         gets(aname);
  52.     }
  53.  
  54.     char* retbno()
  55.     {
  56.         return bno;
  57.     }
  58.  
  59.     void report()
  60.     {cout<<bno<<setw(30)<<bname<<setw(30)<<aname<<endl;}
  61.  
  62.  
  63. };         //class ends here
  64.  
  65.  
  66.  
  67.  
  68. class student
  69. {
  70.     char admno[6];
  71.     char name[20];
  72.     char stbno[6];
  73.     int token;
  74. public:
  75.     void create_student()
  76.     {
  77.         clrscr();
  78.          cout<<"\n\t----------------NEW STUDENT ENTRY----------------\n";
  79.         cout<<"\nEnter The admission no. : ";
  80.         cin>>admno;
  81.         cout<<"\n\nEnter the name of the Student: ";
  82.         gets(name);
  83.         token=0;
  84.         stbno[0]='/0';
  85.         cout<<"\n\nStudent Record Created..";
  86.     }
  87.  
  88.     void show_student()
  89.     {
  90.         cout<<"\nAdmission no. : "<<admno;
  91.         cout<<"\nStudent Name : ";
  92.         puts(name);
  93.         cout<<"\nNo. of Book issued : "<<token;
  94.         if(token==1)
  95.             cout<<"\nBook No. "<<stbno;
  96.     }
  97.  
  98.     void modify_student()
  99.     {
  100.         cout<<"\nAdmission no. : "<<admno;
  101.         cout<<"\nModify Student Name : ";
  102.         gets(name);
  103.     }
  104.  
  105.     char* retadmno()
  106.     {
  107.         return admno;
  108.     }
  109.  
  110.     char* retstbno()
  111.     {
  112.         return stbno;
  113.     }
  114.  
  115.     int rettoken()
  116.     {
  117.         return token;
  118.     }
  119.  
  120.     void addtoken()
  121.     {token=1;}
  122.  
  123.     void resettoken()
  124.     {token=0;}
  125.  
  126.     void getstbno(char t[])
  127.     {
  128.         strcpy(stbno,t);
  129.     }
  130.  
  131.     void report()
  132.     {cout<<"\t"<<admno<<setw(20)<<name<<setw(10)<<token<<endl;}
  133.  
  134. };         //class ends here
  135.  
  136.  
  137.  
  138.  
  139. //***************************************************************
  140. //        global declaration for stream object, object
  141. //****************************************************************
  142.  
  143.     fstream fp,fp1;
  144.     book bk;
  145.     student st;
  146.  
  147.  
  148. //***************************************************************
  149. //        function to write in file
  150. //****************************************************************
  151.  
  152. void write_book()
  153. {
  154.     char ch;
  155.     fp.open("book.dat",ios::out|ios::app);
  156.     do
  157.     {
  158.         clrscr();
  159.         bk.create_book();
  160.         fp.write((char*)&bk,sizeof(book));
  161.         cout<<"\n\nDo you want to add more record..(y/n?)";
  162.         cin>>ch;
  163.     }while(ch=='y'||ch=='Y');
  164.     fp.close();
  165. }
  166.  
  167. void write_student()
  168. {
  169.     char ch;
  170.     fp.open("student.dat",ios::out|ios::app);
  171.     do
  172.     {
  173.         st.create_student();
  174.         fp.write((char*)&st,sizeof(student));
  175.         cout<<"\n\nDo you want to add more record..(y/n?)";
  176.         cin>>ch;
  177.     }while(ch=='y'||ch=='Y');
  178.     fp.close();
  179. }
  180.  
  181.  
  182. //***************************************************************
  183. //        function to read specific record from file
  184. //****************************************************************
  185.  
  186.  
  187. void display_spb(char n[])
  188. {
  189.     cout<<"\nBOOK DETAILS\n";
  190.     int flag=0;
  191.     fp.open("book.dat",ios::in);
  192.     while(fp.read((char*)&bk,sizeof(book)))
  193.     {
  194.         if(strcmpi(bk.retbno(),n)==0)
  195.         {
  196.             bk.show_book();
  197.              flag=1;
  198.         }
  199.     }
  200.  
  201.     fp.close();
  202.     if(flag==0)
  203.         cout<<"\n\nBook does not exist";
  204.     getch();
  205. }
  206.  
  207. void display_sps(char n[])
  208. {
  209.     cout<<"\nSTUDENT DETAILS\n";
  210.     int flag=0;
  211.     fp.open("student.dat",ios::in);
  212.     while(fp.read((char*)&st,sizeof(student)))
  213.     {
  214.         if((strcmpi(st.retadmno(),n)==0))
  215.         {
  216.             st.show_student();
  217.             flag=1;
  218.         }
  219.     }
  220.  
  221.     fp.close();
  222.     if(flag==0)
  223.             cout<<"\n\nStudent does not exist";
  224.      getch();
  225. }
  226.  
  227.  
  228. //***************************************************************
  229. //        function to modify record of file
  230. //****************************************************************
  231.  
  232.  
  233. void modify_book()
  234. {
  235.     char n[6];
  236.     int found=0;
  237.     clrscr();
  238.     cout<<"\n\n\t\t-----------------MODIFY BOOK RECORD-------------------- ";
  239.     cout<<"\n\n\tEnter the Book No. of the Book";
  240.     cin>>n;
  241.     fp.open("book.dat",ios::in|ios::out);
  242.     while(fp.read((char*)&bk,sizeof(book)) && found==0)
  243.     {
  244.         if(strcmpi(bk.retbno(),n)==0)
  245.         {
  246.             bk.show_book();
  247.             cout<<"\nEnter the new details of Book:"<<endl;
  248.             bk.modify_book();
  249.             int pos=-1*sizeof(bk);
  250.                 fp.seekp(pos,ios::cur);
  251.                 fp.write((char*)&bk,sizeof(book));
  252.                 cout<<"\n\n\t Record Updated";
  253.                 found=1;
  254.         }
  255.     }
  256.  
  257.         fp.close();
  258.         if(found==0)
  259.             cout<<"\n\n Record Not Found ";
  260.         getch();
  261. }
  262.  
  263.  
  264. void modify_student()
  265. {
  266.     char n[6];
  267.     int found=0;
  268.     clrscr();
  269.     cout<<"\n\n\t\t---------------MODIFY STUDENT RECORD------------------";
  270.     cout<<"\n\n\tEnter the Admission No. of the Student";
  271.     cin>>n;
  272.     fp.open("student.dat",ios::in|ios::out);
  273.     while(fp.read((char*)&st,sizeof(student)) && found==0)
  274.     {
  275.         if(strcmpi(st.retadmno(),n)==0)
  276.         {
  277.             st.show_student();
  278.             cout<<"\nEnter the new details of Student"<<endl;
  279.             st.modify_student();
  280.             int pos=-1*sizeof(st);
  281.             fp.seekp(pos,ios::cur);
  282.             fp.write((char*)&st,sizeof(student));
  283.             cout<<"\n\n\t Record Updated";
  284.             found=1;
  285.         }
  286.     }
  287.  
  288.     fp.close();
  289.     if(found==0)
  290.         cout<<"\n\n Record Not Found ";
  291.     getch();
  292. }
  293.  
  294. //***************************************************************
  295. //        function to delete record of file
  296. //****************************************************************
  297.  
  298.  
  299. void delete_student()
  300. {
  301.     char n[6];
  302.     int flag=0;    
  303.     clrscr();
  304.         cout<<"\n\n\n\t\t---------------DELETE STUDENT---------------";
  305.         cout<<"\n\nEnter The Admission No. of the Student you want to delete : ";
  306.         cin>>n;
  307.         fp.open("student.dat",ios::in|ios::out);
  308.         fstream fp2;
  309.         fp2.open("Temp.dat",ios::out);
  310.         fp.seekg(0,ios::beg);
  311.         while(fp.read((char*)&st,sizeof(student)))
  312.     {
  313.         if(strcmpi(st.retadmno(),n)!=0)
  314.                  fp2.write((char*)&st,sizeof(student));
  315.         else
  316.                flag=1;
  317.     }
  318.  
  319.     fp2.close();
  320.         fp.close();
  321.         remove("student.dat");
  322.         rename("Temp.dat","student.dat");
  323.         if(flag==1)
  324.              cout<<"\n\n\tRecord Deleted ..";
  325.         else
  326.              cout<<"\n\nRecord not found";
  327.         getch();
  328. }
  329.  
  330.  
  331. void delete_book()
  332. {
  333.     char n[6];
  334.     clrscr();
  335.     cout<<"\n\n\n\t\t---------------DELETE BOOK---------------";
  336.     cout<<"\n\nEnter the Book No. of the Book you want to delete : ";
  337.     cin>>n;
  338.     fp.open("book.dat",ios::in|ios::out);
  339.     fstream fp2;
  340.     fp2.open("Temp.dat",ios::out);
  341.     fp.seekg(0,ios::beg);
  342.     while(fp.read((char*)&bk,sizeof(book)))
  343.     {
  344.         if(strcmpi(bk.retbno(),n)!=0)  
  345.         {
  346.             fp2.write((char*)&bk,sizeof(book));
  347.         }
  348.     }
  349.  
  350.     fp2.close();
  351.         fp.close();
  352.         remove("book.dat");
  353.         rename("Temp.dat","book.dat");
  354.         cout<<"\n\n\tRecord Deleted ..";
  355.         getch();
  356. }
  357.  
  358.  
  359.  
  360. //***************************************************************
  361. //        function to display all students list
  362. //****************************************************************
  363.  
  364. void display_alls()
  365. {
  366.     clrscr();
  367.          fp.open("student.dat",ios::in);
  368.          if(!fp)
  369.          {
  370.                cout<<"ERROR!!! FILE COULD NOT BE OPEN ";
  371.                getch();
  372.                return;
  373.          }
  374.  
  375.     cout<<"\n\n\t\tSTUDENT LIST\n\n";
  376.     cout<<"==================================================================\n";
  377.     cout<<"\tAdm. No."<<setw(10)<<"Name"<<setw(20)<<"Book Issued\n";
  378.     cout<<"==================================================================\n";
  379.  
  380.     while(fp.read((char*)&st,sizeof(student)))
  381.     {
  382.         st.report();
  383.     }
  384.  
  385.     fp.close();
  386.     getch();
  387. }
  388.  
  389.  
  390. //***************************************************************
  391. //        function to display Books list
  392. //****************************************************************
  393.  
  394. void display_allb()
  395. {
  396.     clrscr();
  397.     fp.open("book.dat",ios::in);
  398.     if(!fp)
  399.     {
  400.         cout<<"ERROR!!! FILE COULD NOT BE OPEN ";
  401.                getch();
  402.                return;
  403.          }
  404.  
  405.     cout<<"\n\n\t\tBook LIST\n\n";
  406.     cout<<"=========================================================================\n";
  407.     cout<<"Book Number"<<setw(20)<<"Book Name"<<setw(25)<<"Author\n";
  408.     cout<<"=========================================================================\n";
  409.  
  410.     while(fp.read((char*)&bk,sizeof(book)))
  411.     {
  412.         bk.report();
  413.     }
  414.          fp.close();
  415.          getch();
  416. }
  417.  
  418.  
  419.  
  420. //***************************************************************
  421. //        function to issue book
  422. //****************************************************************
  423.  
  424. void book_issue()
  425. {
  426.     char sn[6],bn[6];
  427.     int found=0,flag=0;
  428.     clrscr();
  429.     cout<<"\n\nBOOK ISSUE ...";
  430.     cout<<"\n\n\tEnter the Student's Admission No.: ";
  431.     cin>>sn;
  432.     fp.open("student.dat",ios::in|ios::out);
  433.         fp1.open("book.dat",ios::in|ios::out);
  434.         while(fp.read((char*)&st,sizeof(student)) && found==0)
  435.            {
  436.         if(strcmpi(st.retadmno(),sn)==0)
  437.         {
  438.             found=1;
  439.             if(st.rettoken()==0)
  440.             {
  441.                       cout<<"\n\n\tEnter the Book No. : ";
  442.                 cin>>bn;
  443.                 while(fp1.read((char*)&bk,sizeof(book))&& flag==0)
  444.                 {
  445.                        if(strcmpi(bk.retbno(),bn)==0)
  446.                     {
  447.                         bk.show_book();
  448.                         flag=1;
  449.                         st.addtoken();
  450.                         st.getstbno(bk.retbno());
  451.                                int pos=-1*sizeof(st);
  452.                         fp.seekp(pos,ios::cur);
  453.                         fp.write((char*)&st,sizeof(student));
  454.                         cout<<"\n\n\t Book issued successfully\n\nPlease Note: Write current datein backside of book and submit within 15 days. Fine is Rs. 1 for each day after 15 days period.";
  455.                     }
  456.                     }
  457.                   if(flag==0)
  458.                         cout<<"Book no does not exist!";
  459.             }
  460.                 else
  461.                   cout<<"You have not returned the last book.";
  462.  
  463.         }
  464.     }
  465.           if(found==0)
  466.         cout<<"Student record does not exist...";
  467.     getch();
  468.       fp.close();
  469.       fp1.close();
  470. }
  471.  
  472. //***************************************************************
  473. //        function to deposit book
  474. //****************************************************************
  475.  
  476. void book_deposit()
  477. {
  478.     char sn[6],bn[6];
  479.     int found=0,flag=0,day,fine;
  480.     clrscr();
  481.     cout<<"\n\nBOOK DEPOSIT ...";
  482.     cout<<"\n\n\tEnter the Studentís Admission No.";
  483.     cin>>sn;
  484.     fp.open("student.dat",ios::in|ios::out);
  485.     fp1.open("book.dat",ios::in|ios::out);
  486.     while(fp.read((char*)&st,sizeof(student)) && found==0)
  487.        {
  488.         if(strcmpi(st.retadmno(),sn)==0)
  489.         {
  490.             found=1;
  491.             if(st.rettoken()==1)
  492.             {
  493.             while(fp1.read((char*)&bk,sizeof(book))&& flag==0)
  494.             {
  495.                if(strcmpi(bk.retbno(),st.retstbno())==0)
  496.             {
  497.                 bk.show_book();
  498.                 flag=1;
  499.                 cout<<"\n\nBook deposited in no. of days";
  500.                 cin>>day;
  501.                 if(day>15)
  502.                 {
  503.                    fine=(day-15)*1;
  504.                    cout<<"\n\nFine has to deposited Rs. "<<fine;
  505.                 }
  506.                     st.resettoken();
  507.                     int pos=-1*sizeof(st);
  508.                     fp.seekp(pos,ios::cur);
  509.                     fp.write((char*)&st,sizeof(student));
  510.                     cout<<"\n\n\t Book deposited successfully";
  511.             }
  512.             }
  513.           if(flag==0)
  514.             cout<<"Book No. does not exist";
  515.               }
  516.          else
  517.             cout<<"No book is issued..please check!!";
  518.         }
  519.        }
  520.       if(found==0)
  521.     cout<<"Student record not exist...";
  522.     getch();
  523.   fp.close();
  524.   fp1.close();
  525.   }
  526.  
  527.  
  528.  
  529.  
  530. //***************************************************************
  531. //        INTRODUCTION FUNCTION
  532. //****************************************************************
  533.  
  534. void intro()
  535. {
  536.     clrscr();
  537.     cout<<"********************************************************************************";
  538.     gotoxy(35,4);
  539.     cout<<"LIBRARY";
  540.     gotoxy(34,6);
  541.     cout<<"MANAGEMENT";
  542.     gotoxy(35,8);
  543.     cout<<"SYSTEM";
  544.     cout<<"\n\n\n\n\n\n\n\nMADE BY : xyz";
  545.     cout<<"\n\nSCHOOL : abc";
  546.     gotoxy(1,24);
  547.     cout<<"********************************************************************************";
  548.     getch();
  549. }
  550.  
  551.  
  552.  
  553. //***************************************************************
  554. //        ADMINISTRATOR MENU FUNCTION
  555. //****************************************************************
  556.  
  557. void admin_menu()
  558. {
  559.     clrscr();
  560.     int ch2;
  561.     cout<<"***************************************************************************************************************************";
  562.     cout<<"\n\n\n\t\t\tADMINISTRATOR MENU";
  563.     cout<<"\n\n\t1.CREATE STUDENT RECORD";
  564.     cout<<"\n\n\t2.DISPLAY ALL STUDENTS RECORD";
  565.     cout<<"\n\n\t3.DISPLAY SPECIFIC STUDENT RECORD ";
  566.     cout<<"\n\n\t4.MODIFY STUDENT RECORD";
  567.     cout<<"\n\n\t5.DELETE STUDENT RECORD";
  568.     cout<<"\n\n\t6.CREATE BOOK ";
  569.     cout<<"\n\n\t7.DISPLAY ALL BOOKS ";
  570.     cout<<"\n\n\t8.DISPLAY SPECIFIC BOOK ";
  571.     cout<<"\n\n\t9.MODIFY BOOK ";
  572.     cout<<"\n\n\t10.DELETE BOOK ";
  573.     cout<<"\n\n\t11.BACK TO MAIN MENU";
  574.     cout<<"\n\n\tPlease Enter Your Choice (1-11) ";
  575.     cin>>ch2;
  576.     switch(ch2)
  577.     {
  578.             case 1: clrscr();
  579.                 write_student();break;
  580.             case 2: display_alls(); break;
  581.             case 3:
  582.                    char num[6];
  583.                    clrscr();
  584.                    cout<<"\n\n\tPlease Enter The Admission No. ";
  585.                    cin>>num;
  586.                    display_sps(num);
  587.                    break;
  588.               case 4: modify_student();break;
  589.               case 5: delete_student();break;
  590.         case 6: clrscr();
  591.             write_book();break;
  592.         case 7: display_allb();break;
  593.         case 8: {
  594.                    char num[6];
  595.                    clrscr();
  596.                    cout<<"\n\n\tPlease Enter The book No. ";
  597.                    cin>>num;
  598.                    display_spb(num);
  599.                    break;
  600.             }
  601.               case 9: modify_book();break;
  602.               case 10: delete_book();break;
  603.              case 11: return;
  604.               default:cout<<"\a";
  605.        }
  606.        admin_menu();
  607. }
  608.  
  609.  
  610. //***************************************************************
  611. //        THE MAIN FUNCTION OF PROGRAM
  612. //****************************************************************
  613.  
  614.  
  615. void main()
  616. {
  617.     int ch;
  618.     intro();
  619.     do
  620.     {
  621.         clrscr();
  622.         cout<<"\n\n\n\t\t\t  MAIN MENU";
  623.         cout<<"\n\t\t\t~~~~~~~~~";
  624.         cout<<"\n\n\t01. BOOK ISSUE";
  625.         cout<<"\n\n\t02. BOOK DEPOSIT";
  626.           cout<<"\n\n\t03. ADMINISTRATOR MENU";
  627.           cout<<"\n\n\t04. EXIT";
  628.           cout<<"\n\n\n\t\tPlease Select Your Option (1-4) ";
  629.           ch=getche();
  630.           switch(ch)
  631.           {
  632.             case '1':clrscr();
  633.                  book_issue();
  634.                     break;
  635.               case '2':book_deposit();
  636.                      break;
  637.               case '3':admin_menu();
  638.                  break;
  639.               case '4':exit(0);
  640.               default :cout<<"\a";
  641.         }
  642.         } while(ch!='4');
  643. }
  644.  
  645. //***************************************************************
  646. //                END OF PROJECT
  647. //***************************************************************
Jan 28 '16 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
The first thing I see is there are no student or book objects created in the code. All I see is one global object for book and one global object for student.

Data is loaded into these objects by a write state that uses sizeof(book) and sizeof(student). Question: are you sure that the number of bytes read from the disc is correct? That is, does the disc file exactly match the class definitions? The reason for asking this is that the disc file is opened as a text file and not a binary file. Using sizeof in a write requires a binary write.

Use your debugger and verify the data read from the disc file is exactly correct.

Post again and tell me what you find out.

Also read this: https://bytes.com/topic/c/insights/7...obal-variables
Jan 28 '16 #2

Post your reply

Sign in to post your reply or Sign up for a free account.