Final 3
Final 3
Roll no.: 32
Name: Manvani Srushti Dipakbhai
Class: SYBCA Sem.: 3 Div.: A
Date of creation: 7th July 2022
Problem Definition:
Create a class called Book that contains data members such as bookno (an
int), title(array of char), author (array of char) and price (type float).
Include a member function called addbook() to get book
details from the user, and another function called showbooks() to display
all the details. Finally create a member function sortByTitle(), that will sort
the data for all the items in the ascending order of book title. Write a menu
driven program to exercise this class. Invite the user to input data for up to
5-10 Books.
#include<iostream>
#include<cstring>
using namespace std;
class Book
{
int bookno[10],i,n,j;
char title[50][10],author[50][10],t[50];
float price[10];
public:
int ch=-1;
void addbook()
{
cout<<"Enter no. of records you want to enter: ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"enter book no=";
cin>>bookno[i];
cout<<"enter book name=";
cin>>title[i];
cout<<"enter author=";
cin>>author[i];
cout<<"enter price=";
cin>>price[i];
cout<<"__________________________________________________"<
<endl;
}
}
9
TMTBCA College
304-Object Oriented Programming And Data Structures
void showbook()
{
if(ch==-1)
{
cout<<"No records found please press 1 for insert book
details";
}
else
{
for(i=0;i<n;i++)
{
cout<<"bookno="<<bookno[i]<<endl;
cout<<"book title="<<title[i]<<endl;
cout<<"author name="<<author[i]<<endl;
cout<<"price="<<price[i]<<endl;
cout<<"__________________________________________________"<
<endl;
}
}
}
void sortbytitle()
{
if(ch==-1)
{
cout<<"No records found please press 1 for insert book
details";
ch++;
}
else
{
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(strcmp(title[j-1], title[j])>0)
{
strcpy(t, title[j-1]);
strcpy(title[j-1], title[j]);
strcpy(title[j], t);
}
}
}
for(i=0;i<n;i++)
10
TMTBCA College
304-Object Oriented Programming And Data Structures
{
cout<<"book title="<<title[i]<<endl;
}
}
}
};
int main()
{
Book b;
int n;
do
{
cout<<"\n\nEnter 1 to add book";
cout<<"\nEnter 2 to show book";
cout<<"\nEnter 3 to sort book";
cout<<"\nEnter 4 to exit";
cout<<"\nEnter Your Choice: ";
cin>>n;
switch(n)
{
case 1:
b.addbook();
break;
case 2:
b.showbook();
break;
case 3:
b.sortbytitle();
break;
case 4:
exit(0);
default:
cout<<"Invalid choice";
break;
}
}while(n!=4);
}
Output:
11
TMTBCA College
304-Object Oriented Programming And Data Structures
12
TMTBCA College
304-Object Oriented Programming And Data Structures
13
TMTBCA College