Advertisement
Aniket_Goku

book

Oct 9th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. //exapmle  of array of object
  2. #include<iostream>
  3. #include<string.h>
  4. #include<iomanip>
  5. using namespace std;
  6. class book
  7. {
  8.    
  9.     int b_no;
  10.     char author[20];
  11.     float price;
  12.    
  13.     public:
  14.         char title[20];
  15.    
  16.         void getdata();
  17.         void putdata();
  18.    
  19. };
  20. void book::getdata()
  21. {
  22.     cout<<"\n Enter book no: ";
  23.     cin>>b_no;
  24.     cout<<"\n Enter book title: ";
  25.     cin>>title;
  26.     cout<<"\n Enter book author: ";
  27.     cin>>author;
  28.     cout<<"\n Enter book price: ";
  29.     cin>>price;
  30.        
  31. }
  32. void book::putdata()
  33. {
  34.     cout<<endl<<b_no<<setw(10)<<title<<setw(10)<<author<<"\t"<< price;
  35.        
  36. }
  37.  
  38. int main()
  39. {
  40.    
  41.     book b[3],t;
  42.     int ch,i,temp=0;
  43.     do
  44.     {
  45.         cout<<"\n Menu driven program for book application";
  46.         cout<<"\n 1.Insert book data";
  47.         cout<<"\n 2.Display book data";
  48.         cout<<"\n 3.Sorting by Title";
  49.         cout<<"\n 4.Exit";
  50.         cout<<"\n Enter your choice: ";
  51.         cin>>ch;
  52.         switch(ch)
  53.         {
  54.             case 1:
  55.                 if(temp>=3)                    
  56.                 {
  57.                     cout<<"\nonly 3 entries allowed[ insertion ]";
  58.                 }
  59.                 else
  60.                 {                      
  61.                     b[temp].getdata();
  62.                     temp++;
  63.                 }
  64.                 break; 
  65.             case 2:
  66.                 if(temp==0)
  67.                 {
  68.                    
  69.                     cout<<"\n Please enter data";
  70.                 }
  71.                 else
  72.                 {
  73.                     for(i=0;i<temp;i++)
  74.                     {
  75.                         b[i].putdata();
  76.                     }  
  77.                 }
  78.             break;
  79.            
  80.             case 3://sorting by titles
  81.                 int j,df;
  82.                 for(i=0;i<temp;i++)
  83.                 {  
  84.                     if(i!=temp)
  85.                     {
  86.                         j=i+1;
  87.                     }
  88.                    
  89.                        
  90.                     while(strcmp(b[i].title,b[j].title)>=0)
  91.                     {
  92.                         t=b[j];                    
  93.                         b[j]=b[i];
  94.                         b[i]=t;
  95.                    
  96.                     }
  97.                    
  98.                 }
  99.                 for(i=0;i<temp;i++)
  100.                 {
  101.                         b[i].putdata();    
  102.                 }
  103.            
  104.             break; 
  105.         }
  106.     }while(ch>=1 && ch<=3);
  107.     return 0;
  108. }
  109.  
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement