Your code for the Library Management System is a good starting point. It allows users to add new books, view all books, and delete books. However, there's room for improvement to make it more functional and user-friendly. Here are some suggestions and enhancements:
-
Implement Borrow and Return Functionality:
Currently, the is_borrowed field in the Book struct is initialized but not utilized. You can add options to borrow and return books, updating the is_borrowed status accordingly.
-
Improve User Input Handling:
- Flush Input Buffer: When using
scanf, sometimes the newline character remains in the input buffer, which can cause issues with subsequent input. Consider using functions like fgets for string inputs.
- Validate Inputs: Ensure that inputs are valid (e.g., non-negative IDs, non-empty titles/authors).
-
Prevent Duplicate Book IDs:
Check if the entered book ID already exists in the library to prevent duplicates.
-
Persist Data Between Sessions:
Currently, all data is stored in memory and lost when the program exits. You might want to implement file handling to save and load the library data.
-
Enhance the User Interface:
Organize the output in a more readable format, possibly using table-like structures or formatting.
Updated Code with Borrow and Return Functionality
Below is the enhanced code with borrow and return features added:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 1000
#define MAX_TITLE_LENGTH 100
#define MAX_AUTHOR_LENGTH 50
struct Book {
int id;
char title[MAX_TITLE_LENGTH];
char author[MAX_AUTHOR_LENGTH];
int is_borrowed;
};
struct Library {
struct Book books[MAX_BOOKS];
int book_count;
};
void initialize_library(struct Library *lib) {
lib->book_count = 0;
}
int find_book_index_by_id(const struct Library *lib, int id) {
for (int i = 0; i < lib->book_count; i++) {
if (lib->books[i].id == id) {
return i;
}
}
return -1;
}
void add_book(struct Library *lib) {
if (lib->book_count >= MAX_BOOKS) {
printf("Library is full. Cannot add more books.\n");
return;
}
struct Book new_book;
printf("Enter book ID: ");
scanf("%d", &new_book.id);
// Check for duplicate ID
if (find_book_index_by_id(lib, new_book.id) != -1) {
printf("Book with ID %d already exists.\n", new_book.id);
return;
}
printf("Enter book title: ");
getchar(); // Clear newline from buffer
fgets(new_book.title, MAX_TITLE_LENGTH, stdin);
new_book.title[strcspn(new_book.title, "\n")] = '\0'; // Remove newline
printf("Enter book author: ");
fgets(new_book.author, MAX_AUTHOR_LENGTH, stdin);
new_book.author[strcspn(new_book.author, "\n")] = '\0'; // Remove newline
new_book.is_borrowed = 0;
lib->books[lib->book_count] = new_book;
lib->book_count++;
printf("Book added successfully.\n");
}
void view_all_books(const struct Library *lib) {
if (lib->book_count == 0) {
printf("No books in the library.\n");
return;
}
printf("\nID\tTitle\t\tAuthor\t\tStatus\n");
printf("---------------------------------------------------------------\n");
for (int i = 0; i < lib->book_count; i++) {
printf("%d\t%-16s\t%-16s\t%s\n",
lib->books[i].id,
lib->books[i].title,
lib->books[i].author,
lib->books[i].is_borrowed ? "Borrowed" : "Available");
}
}
void borrow_book(struct Library *lib) {
int id;
printf("Enter book ID to borrow: ");
scanf("%d", &id);
int index = find_book_index_by_id(lib, id);
if (index != -1) {
if (lib->books[index].is_borrowed) {
printf("Book is already borrowed.\n");
} else {
lib->books[index].is_borrowed = 1;
printf("You have borrowed '%s'.\n", lib->books[index].title);
}
} else {
printf("Book not found.\n");
}
}
void return_book(struct Library *lib) {
int id;
printf("Enter book ID to return: ");
scanf("%d", &id);
int index = find_book_index_by_id(lib, id);
if (index != -1) {
if (lib->books[index].is_borrowed) {
lib->books[index].is_borrowed = 0;
printf("You have returned '%s'.\n", lib->books[index].title);
} else {
printf("Book was not borrowed.\n");
}
} else {
printf("Book not found.\n");
}
}
void delete_book(struct Library *lib) {
int id;
printf("Enter book ID to delete: ");
scanf("%d", &id);
int index = find_book_index_by_id(lib, id);
if (index != -1) {
for (int j = index; j < lib->book_count - 1; j++) {
lib->books[j] = lib->books[j + 1];
}
lib->book_count--;
printf("Book deleted successfully.\n");
} else {
printf("Book not found.\n");
}
}
int main() {
struct Library lib;
initialize_library(&lib);
int choice;
do {
printf("\n--- Library Management System ---\n");
printf("1. Add new book\n");
printf("2. View all books\n");
printf("3. Borrow a book\n");
printf("4. Return a book\n");
printf("5. Delete a book\n");
printf("6. Exit\n");
printf("Enter your choice (1-6): ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_book(&lib);
break;
case 2:
view_all_books(&lib);
break;
case 3:
borrow_book(&lib);
break;
case 4:
return_book(&lib);
break;
case 5:
delete_book(&lib);
break;
case 6:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 6);
return 0;
}
Explanation of Changes:
-
Borrow and Return Functions:
borrow_book Function: Allows the user to borrow a book by setting is_borrowed to 1.
return_book Function: Allows the user to return a borrowed book by setting is_borrowed back to 0.
-
Input Improvements:
- Used
getchar() to clear the input buffer before reading strings.
- Changed
scanf for strings to fgets to safely read strings with spaces.
- Trimmed newline characters from strings using
strcspn.
-
Duplicate ID Check:
- Added a function
find_book_index_by_id to search for a book by ID.
- Used this function in
add_book to prevent adding books with duplicate IDs.
-
Improved Output Formatting:
- Adjusted the
view_all_books function to align the columns using formatting specifiers.
-
Menu Update:
- Updated the menu to include options for borrowing and returning books.
- Adjusted the
switch statement accordingly.
Additional Suggestions
-
File Handling for Data Persistence:
To retain the library data between program executions, implement file reading and writing. Before exiting, write the lib data to a file, and at the start of the program, read from that file to populate lib.
-
Enhanced Validation:
- Check for invalid inputs (e.g., non-integer IDs).
- Confirm actions (e.g., ask "Are you sure you want to delete this book? (y/n)").
-
User Authentication System:
If this system is intended for multiple users, consider adding simple authentication to restrict certain actions to authorized personnel.
-
Search Functionality:
Add functions to search for books by title or author.
Testing the Program
Be sure to compile and test the program thoroughly:
gcc -o library_management library_management.c
./library_management
Test all functionalities, including edge cases (e.g., borrowing a book that's already borrowed, adding a book with an existing ID, etc.).
Conclusion
By implementing these enhancements, you'll have a more functional and user-friendly Library Management System. Remember to handle all possible user inputs gracefully and keep the user interface clear and intuitive.