Code
#include <iostream>
#include <string>
using namespace std;
// display the menu
void displayMenu() {
cout << "\n Library Loan Management" << endl;
cout << "1. Check Out Book" << endl;
cout << "2. Return Book" << endl;
cout << "3. Check Book Status" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice (1-4): ";
}
// Function to display the book selection
void displayBookSelection(const string titles[], int size) {
cout << "\n Book Selection " << endl;
for (int i = 0; i < size; ++i) {
cout << i << ". " << titles[i] << endl;
}
cout << "Enter the book number (0 to " << size - 1 << "): ";
}
int main() {
//"0" Available, "1" Loaned Out
// 1. Define multiple book titles
const int NUM_BOOKS = 3;
string book_titles[NUM_BOOKS] = {
"The C++ Text book",
"Probablity Text book",
"marketing text book"
};
// 2. book statuses
int book_statuses[NUM_BOOKS] = {0, 0, 0}; // All are Available
int choice;
int book_num; // user's book choice
do {
displayMenu();
cin >> choice;
cout << "________________________________________" << endl;
// Only ask for book number
if (choice >= 1 && choice <= 3) {
// Display books
displayBookSelection(book_titles, NUM_BOOKS);
cin >> book_num;
// Validate the book number
if (book_num < 0 || book_num >= NUM_BOOKS) {
cout << "ERROR: Invalid book number selected." << endl;
cout << "________________________________" << endl;
continue;
}
// Logic using if-else statements
if (choice == 1) {
// Check Out Book
if (book_statuses[book_num] == 0) {
book_statuses[book_num] = 1;
cout << "SUCCESS: '" << book_titles[book_num] << "'The book has been checked out." <<
endl;
} else {
cout << "FAILURE: '" << book_titles[book_num] << "'The book is currently on loan and
unavailable for now." << endl;
}
} else if (choice == 2) {
// check in
if (book_statuses[book_num] == 1) {
book_statuses[book_num] = 0;
cout << "SUCCESS: '" << book_titles[book_num] << "'The book has been successfully
returned." << endl;
} else {
cout << "FAILURE: '" << book_titles[book_num] << "'The book is already in the library."
<< endl;
}
} else if (choice == 3) {
// Book Status
if (book_statuses[book_num] == 0) {
cout << "STATUS: '" << book_titles[book_num] << "' is Available." << endl;
} else {
cout << "STATUS: '" << book_titles[book_num] << "' is Loaned Out." << endl;
}
}
} else if (choice == 4) {
// Exit
cout << "Thank you for using the Library System. Goodbye (adios)!!!" << endl;
} else {
// ERROR
cout << "ERROR: Invalid choice. Please enter a number between 1 and 4." << endl;
}
cout <<"_________________________________________" << endl;
} while (choice != 4);
return 0;
}