void displayAdmin()
{
//this will display if admin is chosen
int choice;
do{
cout<<endl;
cout<<"\t\t================LIBRARY==================\n";
cout<<"\t\tSearch by:\n\n";
cout<<"\t\t[1] Title\n\n";
cout<<"\t\t[2] Author\n\n";
cout<<"\t\t[3] Add Book\n\n";
cout<<"\t\t[4] Quit\n\n";
cout<<"\t\t=========================================\n\n";
cout<<"\t\tEnter your Choice: ";
cin>>choice;
}while(choice<1&&choice>4);
system("cls");
switch(choice)
{
case 1:
{
string acctr;
int ctr = 0;
ifstream mainfile("library.dat");//open file
if (!mainfile.good())//if not existing file
{
//if not existing
cerr<<"Can not load data files."<<endl;
exit(1);
}
while (!mainfile.eof())
{
//execute while it is not the end of file
mainfile>>rary[ctr].title>>rary[ctr].author>>rary[ctr].category;//read file
ctr++;//every line
}
mainfile.close();
//close file
cout<<"Enter Title: ";
cin>>acctr;
system("cls");
for (int ind=0; ind<200; ind++)
{
if (acctr == rary[ind].title)//find match on the file from title input
{
cout<<"Title: "<<rary[ind].title<<endl;
cout<<"Author: "<<rary[ind].author<<endl;
cout<<"Category: "<<rary[ind].category<<endl;
cout<<endl;
found=true;
}
}
if(!found)//if not found
cout<<"BOOK NOT FOUND!!";
}
break;
case 2:
{
string acctr;
int ctr = 0;
string pass;
int i = 0;
ifstream mainfile("library.dat");//open file
if (!mainfile.good())
{
//if not existing
cerr<<"Can not load data files."<<endl;
exit(1);
}
while (!mainfile.eof())
{
mainfile>>rary[ctr].title>>rary[ctr].author>>rary[ctr].category;
ctr++;
}
mainfile.close();
cout<<"Enter Author: ";
cin>>acctr;
system("cls");
for (int ind=0; ind<200; ind++)
{
if (acctr == rary[ind].author)//check author input if match on file
{
cout<<"Title: "<<rary[ind].title<<endl;
cout<<"Author: "<<rary[ind].author<<endl;
cout<<"Category: "<<rary[ind].category<<endl;
cout<<endl;
found=true;
}
}
if(!found)//if no match
cout<<"BOOK NOT FOUND!!";
}
break;
case 3:
{
int ctr=0;
ifstream mainfile("library.dat");
if (!mainfile.good())
{
cerr<<"Can not load data files."<<endl;
exit(1);
}
while (!mainfile.eof())
{
mainfile>>rary[ctr].title>>rary[ctr].author>>rary[ctr].category;
ctr++;
}
mainfile.close();
ofstream outfile("library.dat", ios::app);//open file for writing
//insert to text file
cout<<"Enter Book Title: ";
cin.ignore();
getline(cin,rary[ctr].title);
cout<<"Enter Book Author: ";
cin.ignore();
getline(cin,rary[ctr].author);
cout<<"Enter Book Category: ";
cin.ignore();
getline(cin,rary[ctr].category);
outfile<<rary[ctr].title<<" ";
outfile<<rary[ctr].author<<" ";
outfile<<rary[ctr].category<<endl;
cout<<"BOOK HAS BEEN ADDED!!"<<endl;
system("pause");
system("cls");
displayAdmin();
}
case 4:exit(0);
default:cout<<"Invalid Input";
}
}
program can't search properly if the title or author has a space