Written by Sai Sandeep Thota on February 3, 2013 in C++ > Programming

C++ Program for implementing Simple Library

GET ALERTS:

Get our Latest updates delivered to your mailbox!

This program is used to manage Library by loading books, displaying available books and searching for a book. We are using two functions here, loadbooks and display.

loadbooks is used to load the available books in the array. We are accessing this function by creating an array of objects and storing the book details in the individual object.

display is used to display the list of books available in the library.

main uses a switch case to perform/select the operations to perform. in case 3 we have written a string comparison to search for a book in the library.

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<process.h>

class books {
	public:
	int stock;
	char author[20], publisher[20];
	char bookname[20];
	float price;
	void loadbooks();
	void display();
};

void books::loadbooks() {
	cout<<"\nEnter Book Name:";
	cin>>bookname;
	cout<<"\nEnter Author Name:";
	cin>>author;
	cout<<"\nEnter Publisher Name:";
	cin>>publisher;
	cout<<"\nEnter Price:";
	cin>>price;
	cout<<"\nEnter Stock:";
	cin>>stock;
	cout<<"\n-------------\n";
}

void books::display() {
	cout<<"\nName of the Book:"<<bookname;
	cout<<"\nAuthor of the Book:"<<author;
	cout<<"\nPublisher of the Book:"<<publisher;
	cout<<"\nPrice of the Book:"<<price;
	cout<<"\nStock Present:"<<stock;
	cout<<"\n-------------\n";
}

void main() {
	books ob[10];
	int ch, n;
	clrscr();
	do{
		cout<<"\n******\n";
		cout<<"\n1.Load Books\n2.Display\n3.Search\n4.Exit\n";
		cout<<"\n\nEnter your Choice:";
		cin>>ch;
		switch(ch)
		{
		case 1: cout<<"Enter Number of Books:";
			cin>>n;
			for(int i=0;i<n;i++)
			ob[i].loadbooks();
			break;
		case 2:
		/*Display List of available Books*/
			for(i=0;i<n;i++)
			ob[i].display();
			break;
		case 3:
		/* Searching of Book*/
			char bname[20], aname[20];
			cout<<"Enter name of the Book:";
			cin>>bname;
			cout<<"Enter name of the Author:";
			cin>>aname;
			for(i=0;i<n;i++)
			{
				if(strcmp(bname, ob[i].bookname)==0&&strcmp(aname,ob[i].author))
				{
					cout<<"\nBook Present\n\n";
					cout<<"\nName of the Book:"<<ob[i].bookname;
					cout<<"\nAuthor of the Book:"<<ob[i].author;
					cout<<"\nPublisher of the Book:"<<ob[i].publisher;
					cout<<"\nPrice of the Book:"<<ob[i].price;
					cout<<"\nStock Present:"<<ob[i].stock;
					cout<<"\n-------------\n";
					break;
				}
				else
				{
					cout<<"Not Present!!";
					break;
				}
			}
			break;
			default: cout<<"Enter a valid choice!!";
		case 4: exit(1);
		}
	}while(1);
}

Output:
books-library-cpp

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: