📄 libary.cpp
字号:
{
cout << "\nEnter Issue number: ";
cin >> issnum1;
finout2.seekg(0);
int i=0;
while(finout2.read((char *)&issue1, sizeof(issue1)))
{
i++;
issnum2=issue1.getissno();
percd2=issue1.getpercd();
if(percd1==percd2 && issnum1==issnum2)
{
check='n';
break;
}
else
check='y';
}
if(i==0)
check='y';
if(check=='n')
cout << "\n\n\tISSUE DETAILS ALREADY EXIST! Please re-enter.";
}
// Close the Issue.dat file.
finout2.close();
/* Invoke the function to accept issue details and pass the input
periodical code and issue number as parameters. */
issue2.getdata(percd1, issnum1);
// Reopen Issue.dat file for adding the new record.
finout2.open("Issue.dat", ios::app);
if(!finout2)
{ cout << "Could not open Issue.dat file.";
return;
}
// Write the new record to the Issue.dat file.
finout2.write((char *)&issue2, sizeof(issue2));
// Close the file.
finout2.close();
return;
}
}
/* Function to record details of books or periodicals issued to
library members */
void issueret()
{
char issueret, bookper, check='n';
system("cls");
/* Ask user whether the transaction is an issue or a return transaction. */
while(check=='n')
{
cout << "\n\nRECORD ISSUE/RETURN";
cout << "\n\nIssue or Return? (I/R): ";
cin >> issueret;
if(issueret!='I' && issueret!='i' && issueret!='R' && issueret!='r')
cout << "\n\n\tINVALID ENTRY. Enter I for Issue or R for Return.";
else
check='y';
}
/* Ask user whether the transaction is for a book or a periodical. */
check='n';
while(check=='n')
{
cout << "\n\nRECORD ISSUE DETAILS";
cout << "\n\nIssue Book or Periodical? (B/P): ";
cin >> bookper;
if(bookper!='B' && bookper!='b' && bookper!='P' && bookper!='p')
cout << "\n\n\tINVALID ENTRY. Enter B for Book issue or P for Periodical Issue.";
else
check='y';
}
if(bookper=='B' || bookper=='b')
{
// Declare object array of the book class to read data from the file.
book bookarr[100];
int vbookcd1, vbookcd2, i,j, mesgflag;
char vstatus;
// Open Book.dat for reading.
fstream finout("Book.dat", ios::in);
if(!finout)
{ cout << "Could not open Book.dat file for reading.";
return;
}
finout.seekg(0);
// Read the book records into the object array.
for(i=0;(finout.read((char *)&bookarr[i], sizeof(bookarr[i])));i++) {}
check='n';
while(check=='n')
{
// Accept the book code.
cout << "\n\nEnter Book code: ";
cin >> vbookcd1;
// Loop to process the object array.
for(j=0, mesgflag=0;j<i;j++)
{
// Obtain the book code of each object.
vbookcd2=bookarr[j].getcode();
// Check whether it matches the input book code.
if(vbookcd1==vbookcd2)
{
// If it matches, obtain its availability status.
vstatus=bookarr[j].getstatus();
// If the book is being issued, display a message if it is not available.
if(issueret=='I' || issueret=='i')
{
if(vstatus=='I')
{ cout << "\n\n\tThis book has been issued. Please reenter book code.";
mesgflag=1;
}
else
{ bookarr[j].setstatus('I');
check='y';
}
}
// If the book is being returned, display a message if it is already available.
else
{
if(vstatus=='A')
{ cout << "\n\n\tThis book has not been issued. Please reenter book code.";
mesgflag=1;
}
else
{ bookarr[j].setstatus('A');
check='y';
}
}
break;
}
}
// If the input book code is not found, display message.
if(check=='n' && mesgflag==0)
cout << "\n\n\tINVALID BOOK CODE! Please re-enter.";
}
// Close the Book.dat file.
finout.close();
// Reopen the file for overwriting existing content.
finout.open("Book.dat", ios::out);
if(!finout)
{ cout << "Could not open Book.dat file for updating.";
return;
}
// Write the object array back to the file.
for(j=0;j<i;j++)
finout.write((char *)&bookarr[j], sizeof(bookarr[j]));
// Close the file.
finout.close();
}
else
{
// Declare object array of the perissue class to read data from the file.
perissue issuearr[100];
int vpercd1, vpercd2, vissnum1, vissnum2, i,j, mesgflag;
char vstatus;
// Open Issue.dat for reading.
fstream finout("Issue.dat", ios::in);
if(!finout)
{ cout << "Could not open Issue.dat file for reading.";
return;
}
finout.seekg(0);
// Read the issue records into the object array.
for(i=0;(finout.read((char *)&issuearr[i], sizeof(issuearr[i])));i++) {}
check='n';
while(check=='n')
{
// Accept the periodical code and issue number.
cout << "\n\nEnter Periodical code: ";
cin >> vpercd1;
cout << "\nEnter Issue number: ";
cin >> vissnum1;
// Loop to process the object array.
for(j=0, mesgflag=0;j<i;j++)
{
// Obtain the periodical code and issue number of each object.
vpercd2=issuearr[j].getpercd();
vissnum2=issuearr[j].getissno();
// Check whether both values match the input values.
if(vpercd1==vpercd2 && vissnum1==vissnum2)
{
// If they match, obtain the availability status.
vstatus=issuearr[j].getstatus();
// If the periodical is being issued, display a message if it is not available.
if(issueret=='I' || issueret=='i')
{
if(vstatus=='I')
{ cout << "\n\n\tThis Periodical has been issued. Please reenter Periodical code and Issue number.";
mesgflag=1;
}
else
{ issuearr[j].setstatus('I');
check='y';
}
}
// If the periodical is being returned, display a message if it is already available.
else
{
if(vstatus=='A')
{ cout << "\n\n\tThis Periodical has not been issued. Please reenter Periodical code and Issue number.";
mesgflag=1;
}
else
{ issuearr[j].setstatus('A');
check='y';
}
}
break;
}
}
// If the input periodical code and issue number is not found, display message.
if(check=='n' && mesgflag==0)
cout << "\n\n\tINVALID PERIODICAL CODE OR ISSUE NUMBER! Please re-enter.";
}
// Close the Issue.dat file.
finout.close();
// Reopen the file for overwriting existing content.
finout.open("Issue.dat", ios::out);
if(!finout)
{ cout << "Could not open Issue.dat file for updating.";
return;
}
// Write the object array back to the file.
for(j=0;j<i;j++)
finout.write((char *)&issuearr[j], sizeof(issuearr[j]));
// Close the file.
finout.close();
}
}
/* Function that deletes outdated periodical issues from
the Issue.dat file. */
void delissue(void)
{
// Declare object array of the perissue class to read data from the file.
perissue issuearr[100];
int vpercd1, vpercd2, vissnum1, vissnum2, i,j, delctr;
// Open Issue.dat for reading.
fstream finout("Issue.dat", ios::in);
if(!finout)
{ cout << "Could not open Issue.dat file for reading.";
return;
}
finout.seekg(0);
// Read the issue records into the object array.
for(i=0;(finout.read((char *)&issuearr[i], sizeof(issuearr[i])));i++) {}
// Close the file.
finout.close();
// Reopen the file for overwriting existing content.
finout.open("Issue.dat", ios::out);
if(!finout)
{ cout << "Could not open Issue.dat file for updating.";
return;
}
// Accept the periodical code for which the issues have to be deleted.
system("cls");
cout << "\nDELETE OLD PERIODICAL ISSUES";
cout << "\n\nEnter Periodical code: ";
cin >> vpercd1;
// Accept the issue number, the records before which have to be deleted.
cout << "\nEnter Issue number: ";
cin >> vissnum1;
// Loop to process the object array.
for(j=0, delctr=0;j<i;j++)
{
// Obtain the periodical code and issue number of each object.
vpercd2=issuearr[j].getpercd();
vissnum2=issuearr[j].getissno();
/* Check the input periodical code and issue number. */
if(vpercd1!=vpercd2 || (vpercd1==vpercd2 && vissnum1<=vissnum2))
/* If the input periodical code matches and the input issue number
is equal or lesser, write the record to the file. If the input
periodical code is different, write the record to the file. */
finout.write((char *)&issuearr[j], sizeof(issuearr[j]));
else
/* Skip the array object and increment the counter to indicate that
it has not been written to file. */
delctr++;
}
// Display messages based on the counter value.
if(delctr==0)
cout << "\n\n\tNo records found for deletion.";
else
cout << "\n\n\t" << delctr << " records deleted.";
// Close the file.
finout.close();
}
/* Function that lists the books or periodicals available for
issue in the library. */
void viewavail(void)
{
char bookper, check='n', vstatus, flag;
system("cls");
// Ask user whether books or periodicals are to be viewed.
while(check=='n')
{
cout << "\n\nVIEW AVAILABLE BOOKS/PERIODICALS DETAILS";
cout << "\n\nView Books or Periodicals? (B/P): ";
cin >> bookper;
if(bookper!='B' && bookper!='b' && bookper!='P' && bookper!='p')
cout << "\n\n\tINVALID ENTRY. Enter B for Book issue or P for Periodical Issue.";
else
check='y';
}
if(bookper=='B' || bookper=='b')
{
// Declare an object of the book class.
book vbook;
flag='n';
// Open Book.dat for reading.
fstream finout("Book.dat", ios::in);
if(!finout)
{ cout << "Could not open Book.dat file for reading.";
return;
}
finout.seekg(0);
cout << "\n\nLIST OF AVAILABLE BOOKS\n";
// Loop to read all records in the file.
while(finout.read((char *)&vbook, sizeof(vbook)))
{
// Obtain the book availability status.
vstatus=vbook.getstatus();
// If available, display the details.
if(vstatus=='A')
{
flag='y';
vbook.dispdata();
}
}
// Display message if no books are available.
if(flag=='n')
cout << "\n\n\tAll the books have been issued.";
// Close the file.
finout.close();
}
else
{
// Declare an object of the perissue and the periodical class.
perissue vissue;
periodical vper;
int vcode1, vcode2;
char *vbooknm;
flag='n';
// Open Issue.dat for reading.
fstream finout1("Issue.dat", ios::in);
if(!finout1)
{ cout << "Could not open Issue.dat file for reading.";
return;
}
// Open Periodical.dat for reading.
fstream finout2("Periodical.dat", ios::in);
if(!finout2)
{ cout << "Could not open Periodical.dat file for reading.";
return;
}
finout1.seekg(0);
cout << "\n\nLIST OF AVAILABLE PERIODICAL ISSUES\n";
// Loop to read all records in the file.
while(finout1.read((char *)&vissue, sizeof(vissue)))
{
// Obtain the periodical issue availability status.
vstatus=vissue.getstatus();
// If available, display the details.
if(vstatus=='A')
{
flag='y';
// Locate the periodical name.
finout2.seekg(0);
while(finout2.read((char *)&vper, sizeof(vper)))
{
vcode1=vissue.getpercd();
vcode2=vper.getcode();
if(vcode1==vcode2)
{
vbooknm=vper.getname();
break;
}
}
// Display the issue details.
vissue.dispdata(vbooknm);
}
}
// Display message if no periodical issues are available.
if(flag=='n')
cout << "\n\n\tAll the periodicals have been issued.";
// Close the file.
finout1.close();
finout2.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -