-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibrary_management.cpp
104 lines (91 loc) · 2.82 KB
/
Library_management.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <string>
using namespace std;
// Define the structure for a book
struct Book {
string title;
string author;
string isbn;
string publisher;
int total_copies;
int available_copies;
Book* next;
};
// Function to add a new book to the linked list
void addBook(Book*& head) {
Book* new_book = new Book;
cout << "Enter the book information:\n";
cout << "Title: ";
getline(cin, new_book->title);
cout << "Author: ";
getline(cin, new_book->author);
cout << "ISBN: ";
getline(cin, new_book->isbn);
cout << "Publisher: ";
getline(cin, new_book->publisher);
cout << "Total copies: ";
cin >> new_book->total_copies;
new_book->available_copies = new_book->total_copies;
new_book->next = nullptr;
// If the list is empty, set the new book as the head
if (head == nullptr) {
head = new_book;
cout << "Book added successfully!\n";
return;
}
// Otherwise, add the new book to the end of the list
Book* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = new_book;
cout << "Book added successfully!\n";
}
// Function to search for a book by title
void searchBook(Book* head) {
string title;
cout << "Enter the title of the book you are looking for: ";
getline(cin, title);
// Traverse the list to find the book with the given title
Book* current = head;
while (current != nullptr) {
if (current->title == title) {
cout << "Title: " << current->title << endl;
cout << "Author: " << current->author << endl;
cout << "ISBN: " << current->isbn << endl;
cout << "Publisher: " << current->publisher << endl;
cout << "Total copies: " << current->total_copies << endl;
cout << "Available copies: " << current->available_copies << endl;
return;
}
current = current->next;
}
cout << "Book not found in the library.\n";
}
int main() {
Book* head = nullptr;
int choice;
while (true) {
cout << "\n-- Library Management System --\n";
cout << "1. Add a new book\n";
cout << "2. Search for a book\n";
cout << "3. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
cin.ignore();
switch (choice) {
case 1:
addBook(head);
break;
case 2:
searchBook(head);
break;
case 3:
cout << "Exiting program...\n";
return 0;
default:
cout << "Invalid choice. Please try again.\n";
}
}
return 0;
}