•
searchBook( )
–
for a searching book by name or author. It asks the user to enter the
type of search he/she wants i.e. by Book name or by author name.
#include <iostream>
#include <string.h>
#include <vector>
#include <iomanip>>
using namespace std;
//Define a class Book wich stored data for Book
class Book
{
private:
char name[25];//Name Book
char author[30];//Author
public:
//Default constructor
Book()
{
 strcpy(this->name,"-");
 strcpy(this->author,"-");
}
//Parametraze constructor
Book(const char*nm,const char*a)
{
 strcpy(this->name,nm);
 strcpy(this->author,a);
}
//Copy Constructor
Book(const Book&bk)
{
 strcpy(this->name,bk.getName());
 strcpy(this->author,bk.getAuthor());
}
const char*getName()const
{
 return name; Â
}
const char*getAuthor()const{
 return this->author;
}
void setName(const char*s)
{
 strcpy(this->name,s);
}
void setAuthor(const char*ch)
{
 strcpy(this->author,ch);
}
friend ostream&operator<<(ostream& os,Book& bk)
{
 os<<setw(25)<<bk.getName()<<"|"<<setw(25)<<bk.getAuthor()<<"|\n";
 return os;
}
};
void searchBookByName(vector<Book>&bk,const char*key)
{
 for(int i=0;i<bk.size();i++)
  {
   if(strcmp(bk[i].getName(),key)==0)
    cout<<bk[i]<<endl;
  }
}
void searchBookByAuth(vector<Book>&bk,const char*key)
{
 for(int i=0;i<bk.size();i++)
  {
   if(strcmp(bk[i].getAuthor(),key)==0)
    cout<<bk[i]<<endl;
  }
}
void searchBook(vector<Book>&bk)
{
 cout<<"Please choose menu\n";
 cout<<"1 -search by Name Book\n";
 cout<<"2 -search by Author Book\n";
 int cmd;
 cin>>cmd;
 switch(cmd)
  {
   case 1:
    {
     char nm[25];
     cout<<"Please input keyword:";
     cin>>nm;
     searchBookByName(bk,nm);
     break;
    }
   case 2:
    {
     char nm[25];
     cout<<"Please input keyword:";
     cin>>nm;
     searchBookByAuth(bk,nm);
     break;
    }
   case 3:
    {
     cout<<"OO Sory Doesnt exist command!!\n";
     break;
    }
  }
}
void InputData(vector<Book>&bk,int size)
{
 for(int i=0;i<size;i++)
  {
   cout<<"Please input name Book:";
   char nam[25];//name look for book
   char auth[30];//author look for book
   cin>>nam;
   cout<<"Please input author Book:";
   cin>>auth;
   Book b(nam,auth);
   bk.push_back(b);
  }
}
int main() {
 vector<Book>bks;//Array of books we can use memory array or VLA
 InputData(bks,3);
 searchBook(bks);
 for(auto it:bks)
  cout<<it<<endl;
 return 0;
}
Comments
Leave a comment