library management system project in c++ using array
Library Management System Project in C++ Using Array
#include <iostream>
#include <string>
using namespace std;
class Book {
public:
string title;
string author;
int id;
bool available;
};
const int MAX_BOOKS = 100;
Book library[MAX_BOOKS];
int numBooks = 0;
void addBook(string title, string author, int id) {
if (numBooks < MAX_BOOKS) {
library[numBooks].title = title;
library[numBooks].author = author;
library[numBooks].id = id;
library[numBooks].available = true;
numBooks++;
cout << "Book added successfully\n";
} else {
cout << "Library is full, cannot add more books\n";
}
}
void displayBooks() {
if (numBooks == 0) {
cout << "Library is empty\n";
} else {
cout << "Books in the library:\n";
for (int i = 0; i < numBooks; i++) {
cout << "Title: " << library[i].title << ", Author: " << library[i].author << ", ID: " << library[i].id << ", Available: " << (library[i].available ? "Yes" : "No") << "\n";
}
}
}
int main() {
addBook("The Catcher in the Rye", "J.D. Salinger", 101);
addBook("To Kill a Mockingbird", "Harper Lee", 102);
addBook("1984", "George Orwell", 103);
displayBooks();
return 0;
}
This code sets up a simple library management system in C++ using an array to store the books. It defines a Book
class with attributes for title, author, ID, and availability. It also defines an array library
to store the books and a variable numBooks
to keep track of the number of books in the library. The addBook
function adds a new book to the library if there is space, and the displayBooks
function displays the books currently in the library. The main
function demonstrates adding books and displaying the library contents.