Question
IN C++ help adding functions My program is a library management program, for option 5 when you search for a book i want it so
IN C++ help adding functions
My program is a library management program, for option 5 when you search for a book i want it so that it shows the availability and who borrowed the book.
Currently, option 5 only shows availability.
Second change is for option 6 not to list how many books a user has borrowed but the names of the book borrowed. (feel free to change the menu and formatting)
currently, option 6 only shows the number of books borrowed.
CODE:
#include
#include
using namespace std;
//structure to store library data
class BookDetails
{
public: //function 5
struct library//book data for one book
{
int code;
char title[20];
int status;//availibility
};
library book_details[100];//total library capacity
};
//structure to store user data
class UserDetails : public BookDetails
{
public: //function 6
struct users//user data
{
int id;
char name[20];
int booksNumber;
};
users user_details[100];
};
int main()
{
UserDetails ob1;//variable of class userdetails
UserDetails *ob;//pointer of userdetails
ob = &ob1;//assign address of ob to ob1
int tempId, i, flag = 0, j;
char tempName[20];
int tempCode;
//variable to hold 100 userdetails
//variable to hold 100 book details
int user_count = 0, books_count = 0;
int choice;
//this loop runs until 7 is entered
do
{
cout << "*************MENU************* ";
cout << "1. Add a new book ";
cout << "2. Add a new user ";
cout << "3. Borrow a book ";
cout << "4. Return a book ";
cout << "5. Search a book ";
cout << "6. Display user info ";
cout << "7. Exit ";
while (1)
{
cout << "Enter your choice : ";
cin >> choice;
if (cin.fail())//for invalid inputs
{
cin.clear();//clears the error state
cin.ignore(numeric_limits
cout << "You have entered wrong input.Please enter a valid Integer ";
}
else
break;
}
switch (choice)
{
//Adds a new book in to the database and increase book count by 1
case 1:
cout << "**********ADD A BOOK********** ";
cout << "Bookname : ";
//cin.ignore() is used to clean the buffer to take input
cin.ignore();
//cin.getline is used to take input with space. Enter key is used to finish taking input. 100 is size of input character
cin.getline(ob->book_details[books_count].title, 100);
cout << "Bookcode : ";
cin >> ob->book_details[books_count].code;
while (true)
{
bool taken = false;//a new unique book
int code = ob->book_details[books_count].code;
// iterate over all book codes
for (int i = 0; i < books_count; i++)//to find if the book code is taken
{
// if book code is already taken
if (ob->book_details[i].code == code)
{
taken = true;
cout << "Book ID already taken ";
cout << "Bookcode : ";
cin >> ob->book_details[books_count].code;//enters new code
break;
}
}
if (!taken)
break;
}
ob->book_details[books_count].status = 1;
books_count++;//adds to book count
break;
case 2:
//adds a new user details in to database
cout << "**********ADD A USER********** ";
cout << "Enter UserId : ";
cin >> tempId;
//flag variable to check whether user exists or not
flag = 1;//username not taken
for (i = 0; i < user_count; i++)
{
if (tempId == ob->user_details[i].id)//to find if userid has been used
{
cout << "UserId already taken ";
flag = 0;
break;
}
}
//if user is not taken creating user id and increase user_count by 1
if (flag)
{
ob->user_details[user_count].id = tempId;
cout << "Enter Username : ";
cin.ignore();
cin.getline(ob->user_details[user_count].name, 100);
ob->user_details[user_count].booksNumber = 0;
cout << "Welcome "<< ob->user_details[user_count].name <<" ";
user_count++;
}
break;
case 3:
cout << "*********BORROW A BOOK******** ";
cout << "Please enter your userId : ";
cin >> tempId;
flag = 0;
//check whether user exists or not using flag variable
for (i = 0; i < user_count; i++)
{
if (ob->user_details[i].id == tempId)
{
flag = 1;
break;
}
}
//if user exists then print his name and check for the requested book
if (flag)
{
cout << "Welcome " << ob->user_details[i].name << " ";
//if user has taken less than 5 books
if (ob->user_details[i].booksNumber <= 5)
{
cout << "Please enter book code you want to borrow : ";
cin >> tempCode;
for (j = 0; j < 100; j++)
{
if (ob->book_details[j].code == tempCode && ob->book_details[j].status != 0)//find the book and check the availibility
{
ob->book_details[j].status = 0;//sets availability to unavailable
ob->user_details[i].booksNumber++;//adds one to user books borrowed
flag = 0;
break;
}
}
if (flag)//cannot find book in book detail
{
cout << "Book not found or it has been currently borrowed ";
}
else
{
cout << "thank you for borrowing a book ";
}
}
else
{
cout << "Username incorrect ;";
}
}
break;
case 4:
// to return the book issued to a user
cout << "*********RETURN A BOOK******** ";
cout << "Please enter book code :";
cin >> tempCode;
flag = 0;
for (i = 0; i < user_count; i++)
{
if (ob->book_details[i].code == tempCode)//searches through database for book
{
flag = 1;
break;
}
}
//once book is found then check for user id
if (flag)
{
if (ob->book_details[i].status == 0)//checks if book already borrowed
{
cout << "Enter user id: ";
cin >> tempId;
flag = 0;
for (j = 0; j < user_count; j++)
if (ob->user_details[j].id == tempId)
//if user exists
{
flag = 1;
break;
}
//if user and book details are found
if (flag)
{
ob->user_details[j].booksNumber--;
//user # of books borrowed goes down by 1
ob->book_details[i].status = 1;
//book becomes available
}
else
{
cout << "User not found ";
}
}
else
{
cout << "Book hasn't been borrowed ";
}
}
else
{
cout << "Invalid code ";
}
break;
case 5:
//Searching a book using bookcode
cout << "*********SEARCH A BOOK******** ";
cout << "Enter book code : ";
cin >> tempCode;
cout << "Book ID Book Name Availability ";
for (i = 0; i < 100; i++)
{
if (ob->book_details[i].code == tempCode)
//if book exists
{
cout << ob->book_details[i].code << "\t" << ob->book_details[i].title << "\t" << ob->book_details[i].status << " ";
break;
}
}
cout<<"This book does not exist ";
break;
case 6:
//printing details of user using user_details structure
cout << "*****DISPLAY ALL USER INFO**** ";
cout << "UserID UserName Books borrowed ";
for (i = 0; i < user_count; i++)
{
cout << ob->user_details[i].id << "\t" << ob->user_details[i].name << "\t" << ob->user_details[i].booksNumber << " ";
}
cout << " ";
break;
case 7:
cout << "*************EXIT************* ";
cout << "Thank you for using the library!";
break;
//if user input doesnot match with any of the case then ask him to enter data again
default:
cout << "Please enter valid input ";
}
} while (choice != 7);
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started