Today we shall write a cpp program for a library usage….!!!!
Here the program mainly consists of get the input data about
number of books present in the library, displaying those books back to show a
student about number of books, issuing of the book, returning of the book.
Now we shall learn to write the program step by step such
that we may not get confused and understand it easily. Here our first task is
to get the knowledge about class and the variable, functions to be present in
the class. By observing the program we came know that we need variables to
store the author, title, publisher, price, number of books and rack no of the
book. These all are the variables which we need to know them because we need to
get the information about the library. To get these variables we need
functions. Therefore the class of the program is as follows:
class book
{
public:
char
author[20],title[20],publisher[20];
//These are the variables need to get the data.
float
price;
//These variables are called as member variables.
int
stock,rackno;
public: //These
are functions in the class.
void
getdata();
//Called as member fuctions.
void
display();
void
issue();
};
So, we have created a class. Now our task is to make functions
to get the data. We get these inputs through CIN and COUT statements as
follows:
void book:: getdata() //In this function we requesting to
enter
//
the details of all the books present in the class
{
cout<<"\n**Enter the author of the book:";
cin>>author;
cout<<"\n**Enter the title of the book:";
cin>>title;
cout<<"\n**Enter the publisher of the book:";
cin>>publisher;
cout<<"\n**Enter the price of the book:";
cin>>price;
cout<<"\n**Enter the stock present:";
cin>>stock;
cout<<"\n**Enter rack no of the book:";
cin>>rackno;
}
If a user wants to know about the details of the total
number of books present in the library we need to show him details of the book.
The display function of the program is as follows:
void book:: display() //To display all the books
present in the library.
{
cout<<"\n-- Author of the book @ " << author;
cout<<"\n-- Title of the book
@ "<<title;
cout<<"\n-- Price of the book
@ "<<price;
cout<<"\n-- Publisher name
@ "<<publisher;
cout<<"\n-- Stock present
@ "<<stock;
cout<<"\n-- Rack no is
@ "<<rackno;
}
We have completed all our tasks of getting the input data
and display the data.The main function of our program includes cases to issues
the book and submission of the book. We shall now write themain function of our program is as follows:
void main()
{
book ob[10];
int a,b,n=0;
clrscr();
do
{
cout<<"\n\n\t\t **************Welcome to
library******************";
cout<<"\n\n\t\t\t|--->1.Setdata\n\n\t\t\t|--->2.Display
total books present in the library\n\n\t\t\t|--->3.Issue a
book\n\n\t\t\t|--->4.Return the book\n\n\t\t\t|--->5.exit";
cout<<"\n\n\t\t\t
Enter your choice from above menu:";
int ch;
cin>>ch;
switch(ch)
{
case
1: cout<<"\n%Enter how many
books you need to enter:";
cin>>n;
for(int i=1; i<=n;i++)
{
cout
<<"\n\t\t\t***********Enter the detail of book " << i
<< "**********\n";
ob[i].getdata();
}
break;
case
2: if(n==0)
{
cout<<"\n\t\t\t**********No
books in library**********";
}
for(i=1;i<=n;i++)
{
cout
<<"\n\t\t\t**********Details of the book " << i <<
"**********\n";
ob[i].display();
}
break;
/*This is to issue books to students*/
case
3: cout<<"\nEnter the title
and author of the book need to be issued:";
char ititle[20],iaut[20];
cout<<"\nTitle:";
cin>>ititle;
cout<<"\nAuthor:";
cin>>iaut;
for(i=1;i<=n;i++)
{
a=strcmp(ititle,ob[i].title);
b=strcmp(iaut,ob[i].author);
if(a==0
&& b==0)
{
ob[i].stock=ob[i].stock-1;
break;
}
}
cout<<"Book found and in the
rack"<<ob[i].rackno<<"\n no.of copies available
are"<<ob[i].stock;
break;
/*This function is to return books and update the number of
books*/
case
4: cout<<"Enter your library number:";
int
libno;
cin>>libno;
cout<<"Enter
title and author of the book:"<<"\nTitle";
char
rtitle[20],rauthor[20];
cin>>rtitle;
cout<<"\nAuthor";
cin>>rauthor;
for(i=1;i<=n;i++)
{
a=strcmp(rtitle,ob[i].title);
b=strcmp(rauthor,ob[i].author);
if(a==0 && b==0)
{
ob[i].stock=ob[i].stock+1;
break;
}
}
break;
case
5: exit(0);
}
}while(1);
}
The complete program
is as follows:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<process.h>
class book
{
Public:
char
author[20],title[20],publisher[20];
//These are the varriables need to get the data.
float price; //These
variables are called as member variables.
int stock,rackno;
public: //These
are fuctions in the class.
void getdata(); //Called as member
fuctions.
void display();
void issue();
};
void book::
getdata() //In this function
we requesting to enter
//
the details of all the books present in the class
{
cout<<"\n**Enter the author of
the book:";
cin>>author;
cout<<"\n**Enter the title of
the book:";
cin>>title;
cout<<"\n**Enter the publisher of
the book:";
cin>>publisher;
cout<<"\n**Enter the price of
the book:";
cin>>price;
cout<<"\n**Enter the stock
present:";
cin>>stock;
cout<<"\n**Enter rack no of the
book:";
cin>>rackno;
}
void book::
display() //To display all
the books present in the library.
{
cout<<"\n-- Author of the book
@ " << author;
cout<<"\n-- Title of the
book @ "<<title;
cout<<"\n-- Price of the
book @ "<<price;
cout<<"\n-- Publisher name @ "<<publisher;
cout<<"\n-- Stock present @ "<<stock;
cout<<"\n-- Rack no is @ "<<rackno;
}
int
issue(char,char,book);
void main()
{
book ob[10];
int a,b,n=0;
clrscr();
do
{
cout<<"\n\n\t\t **************Welcome to
library******************";
cout<<"\n\n\t\t\t|--->1.Setdata\n\n\t\t\t|--->2.Display
total books present in the library\n\n\t\t\t|--->3.Issue a
book\n\n\t\t\t|--->4.Return the book\n\n\t\t\t|--->5.exit";
cout<<"\n\n\t\t\t
Enter your choice from above menu:";
int ch;
cin>>ch;
switch(ch)
{
case
1: cout<<"\n%Enter how many
books you need to enter:";
cin>>n;
for(int i=1; i<=n;i++)
{
cout
<<"\n\t\t\t***********Enter the detail of book " << i
<< "**********\n";
ob[i].getdata();
}
break;
case
2: if(n==0)
{
cout<<"\n\t\t\t**********No
books in library**********";
}
for(i=1;i<=n;i++)
{
cout
<<"\n\t\t\t**********Details of the book " << i <<
"**********\n";
ob[i].display();
}
break;
/*This is to issue
books to students*/
case
3: cout<<"\nEnter the title
and author of the book need to be issued:";
char ititle[20],iaut[20];
cout<<"\nTitle:";
cin>>ititle;
cout<<"\nAuthor:";
cin>>iaut;
for(i=1;i<=n;i++)
{
a=strcmp(ititle,ob[i].title);
b=strcmp(iaut,ob[i].author);
if(a==0
&& b==0)
{
ob[i].stock=ob[i].stock-1;
break;
}
}
cout<<"Book found and in the
rack"<<ob[i].rackno<<"\n no.of copies available
are"<<ob[i].stock;
break;
/*This function is to
return books and update the number of books*/
case
4: cout<<"Enter your library number:";
int
libno;
cin>>libno;
cout<<"Enter
title and author of the book:"<<"\nTitle";
char
rtitle[20],rauthor[20];
cin>>rtitle;
cout<<"\nAuthor";
cin>>rauthor;
for(i=1;i<=n;i++)
{
a=strcmp(rtitle,ob[i].title);
b=strcmp(rauthor,ob[i].author);
if(a==0 && b==0)
{
ob[i].stock=ob[i].stock+1;
break;
}
}
break;
case
5: exit(0);
}
}while(1);
}