

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
this helps in C++ programming fundamental which is a full library management system
Typology: Cheat Sheet
1 / 25
This page cannot be seen from the preview
Don't miss anything!


















Session: 2025– 2029
Library Management System: A C++ program that lets users sign up, log in, and manage books. Users can add, view, search, and delete books , with all data saved in files for persistence.
This application has the following users: · Admin / Librarian (Primary User): Signs up and logs in to the system. Manages the library by adding, viewing, searching, and deleting books. Has full access to all features. · Registered Users (Optional, if extended): Can log in to view available books. May be allowed to search books. Cannot add or delete books unless given admin privileges. Login button
Figure 1:Sign up or login
Figure 3: Add book Book id Book name Author name Back to main menu
A data structure is a way of organizing and storing data efficiently. Parallel Arrays Used in This Project Inde x id[i] name[i] author[i] 0 101
Programming Bjarne Stroustrup (^1 102) Data Mark Allen
Inde x id[i] name[i] author[i] Structures Weiss Here, id[0], name[0], and author[0] together represent the first book.
Function prototypes declare functions before main() to inform the compiler. Function Prototypes Used void header(); bool signup(); bool login(); void loadData(int id[], string name[], string author[], int &count); void saveData(const int id[], const string name[], const string author[], int count); void addBook(int id[], string name[], string author[], int &count); void viewBooks(const int id[], const string name[], const string author[], int count); void searchBook(const int id[], const string name[], const string author[], int count); void deleteBook(int id[], string name[], string author[], int &count); Purpose of Each Function Function Purpose header() Displays a formatted program header on menus. signup() Allows a new user to create an account and saves credentials in users.txt. login() Lets a user log in by verifying credentials from users.txt. loadData(int id[], string name[], string author[], int &count) Loads book information from library.txt into parallel arrays. saveData(const int id[], const string name[], const string author[], int count) Saves current book data from arrays into library.txt for persistence.
On Exit → save all books to library.txt and terminate.
#include #include #include using namespace std; const int SIZE = 100; // Function declarations void header(); bool signup(); bool login(); void loadData(int id[], string name[], string author[], int &count); void saveData(const int id[], const string name[], const string author[], int count); void addBook(int id[], string name[], string author[], int &count); void viewBooks(const int id[], const string name[], const string author[], int count); void searchBook(const int id[], const string name[], const string author[], int count); void deleteBook(int id[], string name[], string author[], int &count); int main() {
int choice; // Sign-up/Login menu do { cout << "=====================================\n"; cout << " LIBRARY SYSTEM LOGIN\n"; cout << "=====================================\n"; cout << "1. Sign Up\n"; cout << "2. Login\n"; cout << "0. Exit\n"; cout << "Enter choice: "; cin >> choice; if (choice == 1) { signup(); } else if (choice == 2) { if (login()) break; // proceed to library system else return 0; // exit if login fails } else if (choice == 0) { return 0; } else { cout << "Invalid Choice!\n"; } } while (true);
cout << "2. View All Books\n"; cout << "3. Search Book\n"; cout << "4. Delete Book\n"; cout << "0. Exit Program\n"; cout << "\nEnter Choice: "; cin >> choice; system("cls"); switch (choice) { case 1: addBook(id, name, author, count); break; case 2: viewBooks(id, name, author, count); break; case 3: searchBook(id, name, author, count); break; case 4: deleteBook(id, name, author, count); break; case 0: saveData(id, name, author, count); cout << "Data Saved Successfully!\n"; break; default: cout << "Invalid Choice!\n"; } if (choice != 0) {
cout << "\nPress Enter to continue..."; cin.ignore(); cin.get(); } } while (choice != 0); return 0; } // Header void header() { cout << "============================================================\n"; cout << " LIBRARY MANAGEMENT SYSTEM (LOW COUPLING)\n"; cout << "============================================================\n n"; } // Sign-up function bool signup() { string user, pass, pass2;
ofstream outfile("users.txt", ios::app); outfile << user << " " << pass << endl; outfile.close(); cout << "Sign-up successful! You can now log in.\n"; return true; } // Login function bool login() { string user, pass; int attempts = 3; while (attempts > 0) { cout << "\nUsername: "; cin >> user; cout << "Password: "; cin >> pass; ifstream infile("users.txt"); string u, p;
bool found = false; while (infile >> u >> p) { if (u == user && p == pass) { found = true; break; } } infile.close(); if (found) { cout << "Login Successful!\n"; return true; } else { attempts--; cout << "Wrong Credentials! Attempts left: " << attempts << endl; } } return false; } // Load books from file void loadData(int id[], string name[], string author[], int &count) {
// Add books void addBook(int id[], string name[], string author[], int &count) { int choice; do { system("cls"); header(); cout << "1. Add Book(s)\n"; cout << "0. Back to Main Menu\n"; cout << "Enter Choice: "; cin >> choice; if (choice == 1) { int n; cout << "\nHow many books do you want to add? "; cin >> n; cin.ignore(); for (int i = 0; i < n; i++) { if (count >= SIZE) { cout << "Library Full! Cannot add more books.\n"; break; }
cout << "\nEntering details for Book " << (i + 1) << endl; cout << "Enter Book ID: "; cin >> id[count]; cin.ignore(); cout << "Enter Book Name: "; getline(cin, name[count]); cout << "Enter Author Name: "; getline(cin, author[count]); count++; cout << "Book Added Successfully!\n"; } cout << "\nAll possible books added.\n"; cout << "Press Enter..."; cin.get(); } } while (choice != 0);