the issue function is not working well instead of modifying the book record it is adding another book with a issue value 2 can any one correct it Code: // **library management** // // **library members can add,delete,modify and search book** // // **library members can operate users account** // // **user can manage his record,issue and return a book** // // **user can take only 1 book at a time** // #include<fstream.h> #include<iostream.h> #include<conio.h> #include<string.h> #include<stdio.h> #include<process.h> #include<math.h> #include<dos.h> #include<graphics.h> #include<iomanip.h> #include<stdlib.h> void mainmenu(void); struct libdate // *defining date* // { int day; int month; int year; }; struct address // *defining address* // { int hno ; char area[100]; char city[100]; long int pincode; }; class book // *defining class book* // { int bcode; char bname[100]; char author[100]; char category[100]; int issue; libdate doe; // *structure as class member* // public : void modify(void); int getissue() { return issue; } void cissuey(void) { issue=1; } void cissuen(void) { issue=2; } void getdata(void)//*inputing data* // { int gdriver = EGA, gmode = EGAHI; int bkcol, maxcolor,color; initgraph(&gdriver, &gmode,""); maxcolor = getmaxcolor(); bkcol=maxcolor=6; setbkcolor(bkcol); color=maxcolor; setcolor(color); cout<<"ENTER NAME:"<<endl; gets(bname); cout<<"ENTER CODE :"<<endl; cin>>bcode; cout<<"AUTHOR NAME :"<<endl; gets(author); cout<<"CATEGORY :"<<endl; gets(category); cout<<"DATE OF ENTRY "<<endl; { cout<<"DAY:"<<endl; cin>>doe.day; cout<<"MONTH :"<<endl; cin>>doe.month; cout<<"YEAR :"<<endl; cin>>doe.year; } cout<<"the current status of the book is unissued by default so enter 1"<<endl; cin>>issue; } void putdata(void) { int gdriver = EGA, gmode = EGAHI; int bkcol, maxcolor,color; initgraph(&gdriver, &gmode,""); maxcolor = getmaxcolor(); bkcol=maxcolor=6; setbkcolor(bkcol); color=maxcolor; setcolor(color); cout<<" "<<bname<<" has the code "<<bcode<<endl; cout<<" written by "<<author<<endl; cout<<" is currently in the category "<<category<<endl; cout<<" is entered in library on "<<doe.day<<"."<<doe.month<<"."<<doe.year<<endl; cout<<" the current status of the book is "<<issue<<endl; getch(); } char*getauthname() { return author; } int getbcode(void) { return bcode; } }; book b1,b2; void book::modify(void) //*to modify a book record*// { int gdriver = EGA, gmode = EGAHI; int bkcol, maxcolor,color; initgraph(&gdriver, &gmode,""); maxcolor = getmaxcolor(); bkcol=maxcolor=2; setbkcolor(bkcol); color=maxcolor; setcolor(color); cout<<" "<<bname<<" has the code "<<bcode<<endl; cout<<" written by "<<author<<endl; cout<<" is currently in the category "<<category<<endl; cout<<" is entered in library on "<<doe.day<<"."<<doe.month<<"."<<doe.year<<endl; cout<<" the current status of the book is "<<issue<<endl; cout<<"enter new details"<<endl; int mbc; char an[100],cat[100],nm[100]; date dot; cout<<"enter new details"<<endl; cout<<"enter the new book code:(enter 1 to retain old one)"<<endl; cin>>mbc; cout<<"enter the new name:(press '.' to retain old one)"<<endl; gets(nm); cout<<"enter the new author:(press '.' to retain old one)"<<endl; gets(an); cout<<"enter the new category:(press '.' to retain old one)"<<endl; gets(cat); if(strcmp(nm,".")!=0) strcpy(bname,nm); if(strcmp(an,".")!=0) strcpy(author,an); if(strcmp(cat,".")!=0) strcpy(category,cat); if(mbc!=1) bcode=mbc; } int searchbook(void) //*to search a book*// { int gdriver = EGA, gmode = EGAHI; int bkcol, maxcolor,color; initgraph(&gdriver, &gmode,""); maxcolor = getmaxcolor(); bkcol=maxcolor=5; setbkcolor(bkcol); color=maxcolor; setcolor(color); ifstream finbook; finbook.open("lib.dat",ios::in|ios::binary); finbook.seekg(0,ios::beg); int n,m,choice; char found='n'; char*mc; cout<<"for searching through book code press 1 or by author name press 2"<<endl; cin>>choice; switch(choice) { case 1: { cout<<"enter the book code to be searched"<<endl; cin>>m; while(!finbook.eof()) { finbook.read((char*) &b1,sizeof(b1)); if(b1.getbcode()==m) { b1.putdata(); getch(); found='y'; return b1.getbcode(); finbook.close(); mainmenu(); } } if(found=='n') cout<<"above book is not present in library!!"<<endl; getch(); finbook.close(); return 0; mainmenu(); } case 2: { cout<<"enter the author's name for searching book"<<endl; gets(mc); while(!finbook.eof()) { finbook.read((char*)&b1,sizeof(b1)); if(strcmpi(mc,b1.getauthname())==0) { b1.putdata(); getch(); found='y'; return b1.getbcode(); finbook.close(); mainmenu(); break; } } if(found=='n') cout<<"above book is not present in library!!"<<endl; getch(); finbook.close(); mainmenu(); return 0; } } } void addbook(void) //* add a book* // { int gdriver = EGA, gmode = EGAHI; int bkcol, maxcolor,color; initgraph(&gdriver, &gmode,""); maxcolor = getmaxcolor(); bkcol=maxcolor=10; setbkcolor(bkcol); color=maxcolor; setcolor(color); ofstream foutbook("lib.dat",ios::app|ios::binary); { char ans='y'; while(ans=='y') { b1.getdata(); foutbook.write((char*) &b1,sizeof(b1)); cout<<"record added to library.\n"<<endl; cout<<"do you want to enter more records?(y/n).."<<endl; cin>>ans; } } foutbook.close(); mainmenu(); } void deletebook(void) //*to delete an existing book* // { int gdriver = EGA, gmode = EGAHI; int bkcol, maxcolor,color; initgraph(&gdriver, &gmode,""); maxcolor = getmaxcolor(); bkcol=maxcolor=12; setbkcolor(bkcol); color=maxcolor; setcolor(color); ifstream finbook; ofstream foutbook; finbook.open("lib.dat",ios::in); foutbook.open("temp.dat",ios::out); int bcode; char found='f',confirm='n'; cout<<"enter the book code of the book which has to be deleted from library"<<endl; cin>>bcode; while(!finbook.eof()) { finbook.read((char*) &b1,sizeof(b1)); if(b1.getbcode()==bcode) { b1.putdata(); found='t'; cout<<"want to delete,sure?(y/n).."<<endl; cin>>confirm; if(confirm=='n') foutbook.write((char*) &b1,sizeof(b1)); } else foutbook.write((char*) &b1,sizeof(b1)); } { if(found=='f') cout<<"record not found!!\n"; getch(); } getch(); finbook.close(); foutbook.close(); remove("lib.dat"); rename("temp.dat","lib.dat"); //* old file is removed and temp is renamed *// finbook.open("lib.dat",ios::in); cout<<"record updated"<<endl; cout<<"now the file contains"<<endl; getch(); while(!finbook.eof()) { finbook.read((char*)&b2,sizeof(b2)); if(finbook.eof()) break; b2.putdata(); getch(); } finbook.close(); mainmenu(); } void modifybook(void) //* to modify a book record in library* // { int bcd; long pos; char found='f'; cout<<"enter the code of the book which has to be modified"<<endl; cin>>bcd; fstream fio("lib.dat",ios::in|ios::out|ios::binary); fio.seekg(0,ios::beg); while(!fio.eof()) { pos=fio.tellg(); fio.read((char*)&b1,sizeof(b1)); if(b1.getbcode()==bcd) { b1.modify(); fio.seekg(pos); //*file pointer positioned to beginning position of record being modified,before writing back modified record*// fio.write((char*)&b1,sizeof(b1)); found='t'; break; } } if(found=='f') cout<<"record not found!!"<<endl; fio.seekg(0); cout<<"now the file contains"<<endl; while(!fio.eof()) { fio.read((char*) &b2,sizeof(b2)); b2.putdata(); getch(); mainmenu(); } } void dispallbook(void) { ifstream finbook; finbook.open("lib.dat",ios::in|ios::binary); finbook.seekg(0,ios::beg); cout<<"file contains"<<endl; while(!finbook.eof()) { finbook.read((char*)&b2,sizeof(b2)); if(finbook.eof()) { break; } else b2.putdata(); getch(); } finbook.close(); mainmenu(); } class account { int id; char name[100]; int status; address haddress; public: libdate doi,doer; int bid; libdate dor; int getid(void) { return id; } int getstatus(void) { return status; } void cstatusy(void) { status=1; } void cstatusn(void) { status=2; } void enterdata(void) //*entering the data of user*// { cout<<"enter the name of the user"<<endl; gets(name); cout<<"enter the library ID"<<endl; cin>>id; cout<<"enter the address of the user"<<endl; { cout<<"enter the house no."<<endl; cin>>haddress.hno; cout<<"enter the area in which the user is currently living"<<endl; gets(haddress.area); cout<<"enter the city in which the user is currently living"<<endl; gets(haddress.city); cout<<"enter the pincode of the city"<<endl; cin>>haddress.pincode; } cout<<"enter the date of entry"<<endl; { cout<<"enter the day"<<endl; cin>>doer.day; cout<<"enter the month"<<endl; cin>>doer.month; cout<<"enter the year"<<endl; cin>>doer.year; } cout<<"defualt value of status is 1 so enter 1"<<endl; cin>>status; } void display(void) { int gdriver = EGA, gmode = EGAHI; int bkcol, maxcolor,color; initgraph(&gdriver, &gmode,""); maxcolor = getmaxcolor(); bkcol=maxcolor=2; setbkcolor(bkcol); color=maxcolor; setcolor(color); cout<<name<<" has the library ID "<<id<<endl; cout<<" who currently lives in "<<endl; cout<<haddress.hno<<" "<<haddress.area<<" "<<haddress.city<<endl; cout<<haddress.pincode<<endl; } void modifyuser(void) { cout<<name<<"has the id"<<id<<"living in"<<haddress.hno<<" "<<haddress.area<<" "<<haddress.city<<" "<<haddress.pincode<<endl; cout<<"enter new details"<<endl; char nm[100]; int idc; address ha; cout<<"enter the new name(press '.' to retain old one)"<<endl; gets(nm); cout<<"enter the new id(press 1 to retain old one)"<<endl; cin>>idc; cout<<"enter the new address"<<endl; { cout<<"enter the new homeno(press 2 to retain old one)"<<endl; cin>>ha.hno; cout<<"enter the new area(press '.' to retain old one)"<<endl; gets(ha.area); cout<<"enter the new city(press '.' to retain old one)"<<endl; gets(ha.city); cout<<"enter the new pincode.(press '3' to retain old one"<<endl; cin>>ha.pincode; } if(strcmp(nm,".")!=0) strcpy(name,nm); if(strcmp(ha.area,".")!=0) strcpy(haddress.area,ha.area); if(strcmp(ha.city,".")) strcpy(haddress.city,ha.city); if(idc!=1) id=idc; if(ha.hno!=2) haddress.hno=ha.hno; if(ha.pincode!=3) haddress.pincode=ha.pincode; } }; account s1,s2; void adduser(void) //*add a user*// { ofstream foutuser; foutuser.open("user.dat",ios::app|ios::binary); char ans='y'; while(ans=='y') { s1.enterdata(); foutuser.write((char*)&s1,sizeof(s1)); cout<<"user added to library"<<endl; cout<<"do you want to enter more users?(y/n).."<<endl; cin>>ans; } foutuser.close(); mainmenu(); } void searchuser(void) //*to search a user*// { ifstream finuser; finuser.open("user.dat",ios::in|ios::binary); finuser.seekg(0,ios::beg); int uid; char found='n'; cout<<"enter the library ID of the user to be searched"<<endl; cin>>uid; while(!finuser.eof()) { finuser.read((char*)&s1,sizeof(s1)); if(s1.getid()==uid) { s1.display(); getch(); found='y'; mainmenu(); } } if(found=='n') cout<<"user not registered"<<endl; getch(); finuser.close(); mainmenu(); } void deleteuser(void) { ifstream fio("user.dat",ios::in|ios::binary); ofstream file("temp.dat",ios::out|ios::binary); int uid; char found='f',confirm='n'; cout<<"enter the ID of the user whom you want to delete"<<endl; cin>>uid; while(!fio.eof()) { fio.read((char*) &s1,sizeof(s1)); if(s1.getid()==uid) { s1.display(); found='t'; cout<<"want to continue sure(y/n).."<<endl; cin>>confirm; if(confirm=='n') file.write((char*)&s1,sizeof(s1)); } else file.write((char*)&s1,sizeof(s1)); } if(found=='f') cout<<"user not found!!"<<endl; getch(); fio.close(); file.close(); remove("user.dat"); rename("temp.dat","user.dat"); fio.open("lib.dat",ios::in|ios::binary); cout<<"record updated"<<endl; cout<<"now the file contains"<<endl; getch(); while(!fio.eof()) { fio.read((char*) &s2,sizeof(s2)); if(fio.eof()) break; s2.display(); getch(); } mainmenu(); fio.close(); } void modifyaccount(void) { fstream fio("user.dat",ios::in|ios::out|ios::binary); int uid; long pos; char found='f'; fio.seekg(0,ios::beg); cout<<"enter the id of the user whose record is to be modified"<<endl; cin>>uid; while(!fio.eof()) { pos=fio.tellg(); fio.read((char*)&s1,sizeof(s1)); if(s1.getid()==uid) { s1.modifyuser(); fio.seekg(pos); fio.write((char*)&s1,sizeof(s1)); found='t'; break; } } if(found=='f') cout<<"record not found!!"<<endl; fio.seekg(0); cout<<"now the user file contains"<<endl; while(!fio.eof()) { fio.read((char*)&s2,sizeof(s2)); s2.display(); getch(); } mainmenu(); fio.close(); } void issuebook(void) { char ch; int m,n,uid; int rec1=0,rec2=0; libdate temp; cout<<"Enter ur library ID"<<endl; cin>>uid; fstream iofile("user.dat",ios::in|ios::app|ios::binary); fstream ifile("lib.dat",ios::in|ios::app|ios::binary); while(!iofile.eof()) { if(iofile.eof()) { mainmenu(); } iofile.read((char*)&s1,sizeof(s1)); rec1++; if(uid==s1.getid()) { n=s1.getstatus(); if(n==2) { cout<<"you already have issued one book"<<endl; getch(); mainmenu(); } else cout<<".....Search the book you want to issue"<<endl; getch(); int bbcode = searchbook(); while(bbcode!=0) { cout<<"are u sure u want to take this book(y/n)"<<endl; cin>>ch; if(ch=='y'||ch=='Y') { while(!ifile.eof()) { if(ifile.eof()) {mainmenu();} ifile.read((char*)&b1,sizeof(b1)); rec2++; if(bbcode==b1.getbcode()) { m=b1.getissue(); if(m==2) { cout<<"Already issued...\n"; mainmenu(); } else s1.bid=bbcode; cout<<"ENTER THE DATE OF ISSUE"<<endl; { cout<<"ENTER THE DAY"<<endl; cin>>temp.day; cout<<"ENTER THE MONTH"<<endl; cin>>temp.month; cout<<"ENTER THE YEAR"<<endl; cin>>temp.year; } s1.doi=temp; b1.cissuen(); s1.cstatusn(); iofile.seekg((rec1-1)*sizeof(s1),ios::beg); ifile.seekg((rec2-1)*sizeof(b1),ios::beg); ifile.write((char*)&b1,sizeof(book)); iofile.write((char*)&s1,sizeof(account)); cout<<" Status updated..\n"; getch(); } } ifile.close(); mainmenu(); } else { cout<<"SORRY u can search some other time"<<endl; ifile.close(); iofile.close(); getch(); mainmenu(); } } } else cout<<"WRONG ID ENTERED!!!..PLZ ENTER YOUR VALID ID"<<endl; iofile.close(); getch(); mainmenu(); } iofile.close(); } void returnbook(void) //* to return a book from user * // { int uid,bbcode; int m; libdate temp; m=b1.getissue(); cout<<"ENTER UR LIBRARY ID"<<endl; cin>>uid; fstream iofile("user.dat",ios::in|ios::app|ios::binary); fstream ifile("lib.dat",ios::in|ios::app|ios::binary); while(iofile) { iofile.read((char*)&s1,sizeof(s1)); if(uid==s1.getid()) { cout<<"valid ID....."<<endl; cout<<"enter the book code of the book u want to return"<<endl; cin>>bbcode; while(ifile) { ifile.read((char*)&b1,sizeof(b1)); if(bbcode==b1.getbcode()) { cout<<"book found checking its status"<<endl; getch(); if(m==2) { cout<<"calculating fine"<<endl; { int m,n,p; if(s1.dor.year==s1.doi.year) { if(s1.dor.month==s1.doi.month) { if(s1.dor.day-s1.doi.day<4) { cout<<"you do not have to pay any fine"<<endl; } else m=s1.dor.day-s1.doi.day; cout<<"you have to pay a fine of rs."<<m; } else { if(s1.dor.month-s1.doi.month==1) { n=30-s1.doi.day; if(n+s1.dor.day<4) { cout<<"you do not have to pay any fine"<<endl; } else cout<<"you have to pay a fine of rs."<<n+s1.dor.day<<endl; } else cout<<"you have to pay a fine of rs. 200"<<endl; } } else { if(s1.dor.year-s1.doi.year==1) { if((s1.doi.month==12)&&(s1.dor.month==1)) { p=30-s1.doi.day; if(p+s1.dor.day<4) { cout<<"you do not have to pay any fine"<<endl; } else cout<<"you have to pay the fine of"<<p+s1.dor.day; } else cout<<"you have to pay the fine of rs. 200"<<endl; } else cout<<"you have to pay the fine of rs. 500"<<endl; } } cout<<"now changing the status of this book"<<endl; getch(); b1.cissuey(); s1.cstatusy(); cout<<"ENTER THE DATE OF RETURN"<<endl; { cout<<"ENTER THE DAY"<<endl; cin>>temp.day; cout<<"ENTER THE MONTH"<<endl; cin>>temp.month; cout<<"ENTER THE YEAR"<<endl; cin>>temp.year; } s1.dor=temp; ifile.write((char*)&b1,sizeof(b1)); iofile.write((char*)&s1,sizeof(s1)); mainmenu(); } else cout<<"error!this book is free to issue"<<endl; getch(); mainmenu(); } else cout<<"u haven't issued this book kindly check the book id again"<<endl; getch(); mainmenu(); } ifile.close();} else cout<<"WRONG USER ID ENTRED CHECK AGAIN"<<endl; getch(); mainmenu(); } iofile.close(); } void record(void) { int m; int uid,ubcode; m=s1.getstatus(); fstream iofile("user.dat",ios::in|ios::app|ios::binary); while(iofile) { iofile.read((char*)&s1,sizeof(s1)); cout<<"Enter your valid library ID"<<endl; cin>>uid; if(uid==s1.getid()) { cout<<"valid ID....checking ur record.."<<endl; getch(); if(m==1) { cout<<"you have not taken any book currently"<<endl; getch(); mainmenu(); } else { cout<<"you have taken the following book"<<endl; getch(); ubcode=s1.bid; ifstream ifile; ifile.open("lib.dat",ios::in|ios::binary); ifile.read((char*)&b1,sizeof(b1)); if(ubcode==b1.getbcode()) { b1.putdata(); getch(); } else cout<<"!!ERROR!!"<<endl; getch(); mainmenu(); } } else cout<<"WRONG LIBRARY ID PLZ TRY AGAIN"<<endl; getch(); mainmenu(); } iofile.close(); } void dispalluser(void) { ifstream fio("user.dat",ios::in|ios::binary); fio.seekg(0,ios::beg); cout<<" the file contains"<<endl; while(!fio.eof()) { fio.read((char*)&s1,sizeof(s1)); if(fio.eof()) { break; } else s1.display(); getch(); } fio.close(); } void mainmenu(void) { int m,n,mn,ch; int ans; int gdriver = EGA, gmode = EGAHI; int bkcol, maxcolor,color; char msg[80]; initgraph(&gdriver, &gmode,""); maxcolor = getmaxcolor(); bkcol=maxcolor=4; setbkcolor(bkcol); color=maxcolor; setcolor(color); gotoxy(25,5); cout<<"M A I N M E N U"; gotoxy(17,10); cout<<"1. BOOKS MENU"; gotoxy(17,12); cout<<"2. ACCOUNT MENU"; gotoxy(17,14); cout<<"3. USER MENU"; gotoxy(17,16); cout<<"4. EXIT"; gotoxy(40,24); cout<<"Enter Option(1-4):"; cin>>m; switch(m) { case 1: { int gdriver = EGA, gmode = EGAHI; int bkcol, maxcolor,color; initgraph(&gdriver, &gmode,""); maxcolor = getmaxcolor(); bkcol=maxcolor=2; setbkcolor(bkcol); color=maxcolor; setcolor(color); gotoxy(20,4); cout<<" 1 : ADD A BOOK"; gotoxy(20,6); cout<<" 2 : SEARCH A BOOK"; gotoxy(20,8); cout<<" 3 : MODIFY A BOOK'S RECORD"; gotoxy(20,10); cout<<" 4 : DELETE"; gotoxy(20,12); cout<<" 5 : DISPLAY ALL "; gotoxy(20,14); cout<<" 6 : RETURN TO MAIN MENU"<<endl; gotoxy(20,16); cout<<"ENTER OPTION(1-7)"<<endl; cin>>n; switch(n) { case 1: { addbook(); break; } case 2: { searchbook(); break; } case 3: { modifybook(); break; } case 4: { deletebook(); break; } case 5: { dispallbook(); break; } case 6: { mainmenu(); } default: cout<<"INVALID ENTRY!!"<<endl; } break; } case 2: { int gdriver = EGA, gmode = EGAHI; int bkcol, maxcolor,color; initgraph(&gdriver, &gmode,""); maxcolor = getmaxcolor(); bkcol=maxcolor=11; setbkcolor(bkcol); color=maxcolor; setcolor(color); cout<<"1 : CREATE NEW ACCOUNT"<<endl; cout<<"2 : SEARCH A USER"<<endl; cout<<"3 : MODIFY A USER'S INFO"<<endl; cout<<"4 : DELETE A USER"<<endl; cout<<"5 : DISPLAY ALL USERS"<<endl; cout<<"6 : RETURN TO MAIN MENU"<<endl; cout<<"ENTER OPTION(1-6)"<<endl; cin>>mn; switch(mn) { case 1: { adduser(); break; } case 2: { searchuser(); break; } case 3: { modifyaccount(); break; } case 4: { deleteuser(); break; } case 5: { dispalluser(); break; } case 6: { mainmenu(); } default : cout<<"INVALID ENTRY!!"<<endl; } break; } case 3: { int gdriver = EGA, gmode = EGAHI, errorcode; int bkcol, maxcolor,color; initgraph(&gdriver, &gmode,""); maxcolor = getmaxcolor(); bkcol=maxcolor=9; setbkcolor(bkcol); color=maxcolor; setcolor(color); cout<<"welcome to user's manual"<<endl; cout<<"1: ISSUE BOOK"<<endl; cout<<"2: RETURN BOOK"<<endl; cout<<"3: CURRENT RECORD"<<endl; cout<<"4:RETURN TO MAIN MENU"<<endl; cout<<"ENTER OPTION(1-4)"<<endl; cin>>ch; switch(ch) { case 1: { issuebook(); break; } case 2: { returnbook(); break; } case 3: { record(); break; } case 4: { mainmenu(); } default: cout<<"INVALID ENTRY!!"<<endl; } break; } case 4: { cout<<"press any key to continue"<<endl; exit(0); break; } default:cout<<"INVALID ENTRY!!"<<endl; } } void main() { clrscr(); mainmenu(); getch(); }