Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //exapmle of array of object
- #include<iostream>
- #include<string.h>
- #include<iomanip>
- using namespace std;
- class book
- {
- int b_no;
- char author[20];
- float price;
- public:
- char title[20];
- void getdata();
- void putdata();
- };
- void book::getdata()
- {
- cout<<"\n Enter book no: ";
- cin>>b_no;
- cout<<"\n Enter book title: ";
- cin>>title;
- cout<<"\n Enter book author: ";
- cin>>author;
- cout<<"\n Enter book price: ";
- cin>>price;
- }
- void book::putdata()
- {
- cout<<endl<<b_no<<setw(10)<<title<<setw(10)<<author<<"\t"<< price;
- }
- int main()
- {
- book b[3],t;
- int ch,i,temp=0;
- do
- {
- cout<<"\n Menu driven program for book application";
- cout<<"\n 1.Insert book data";
- cout<<"\n 2.Display book data";
- cout<<"\n 3.Sorting by Title";
- cout<<"\n 4.Exit";
- cout<<"\n Enter your choice: ";
- cin>>ch;
- switch(ch)
- {
- case 1:
- if(temp>=3)
- {
- cout<<"\nonly 3 entries allowed[ insertion ]";
- }
- else
- {
- b[temp].getdata();
- temp++;
- }
- break;
- case 2:
- if(temp==0)
- {
- cout<<"\n Please enter data";
- }
- else
- {
- for(i=0;i<temp;i++)
- {
- b[i].putdata();
- }
- }
- break;
- case 3://sorting by titles
- int j,df;
- for(i=0;i<temp;i++)
- {
- if(i!=temp)
- {
- j=i+1;
- }
- while(strcmp(b[i].title,b[j].title)>=0)
- {
- t=b[j];
- b[j]=b[i];
- b[i]=t;
- }
- }
- for(i=0;i<temp;i++)
- {
- b[i].putdata();
- }
- break;
- }
- }while(ch>=1 && ch<=3);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement