Untitled

 avatar
unknown
plain_text
2 years ago
4.4 kB
4
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Structure to represent a book
struct Book {
    char title[100];
    char author[100];
    int year;
};

// Function to add books to the library
void addBooks(struct Book books[], int *count) {
    printf("Enter the details of the book:\n");
    printf("Title: ");
    scanf("%s", books[*count].title);
    printf("Author: ");
    scanf("%s", books[*count].author);
    printf("Year: ");
    scanf("%d", &books[*count].year);
    (*count)++;
    printf("Book added successfully!\n");
}

// Function to search for books in the library
void searchBooks(struct Book books[], int count) {
    char title[100];
    printf("Enter the title of the book to search: ");
    scanf("%s", title);
    int found = 0;
    for (int i = 0; i < count; i++) {
        if (strcmp(books[i].title, title) == 0) {
            printf("Book found!\n");
            printf("Title: %s\n", books[i].title);
            printf("Author: %s\n", books[i].author);
            printf("Year: %d\n", books[i].year);
            found = 1;
            break;
        }
    }
    if (!found) {
        printf("Book not found!\n");
    }
}

// Function to view all books in the library
void viewBooks(struct Book books[], int count) {
    if (count == 0) {
        printf("No books in the library!\n");
    } else {
        printf("Books in the library:\n");
        for (int i = 0; i < count; i++) {
            printf("Title: %s\n", books[i].title);
            printf("Author: %s\n", books[i].author);
            printf("Year: %d\n", books[i].year);
            printf("--------------\n");
        }
    }
}

// Function to delete a book from the library
void deleteBook(struct Book books[], int *count) {
    char title[100];
    printf("Enter the title of the book to delete: ");
    scanf("%s", title);
    int found = 0;
    for (int i = 0; i < *count; i++) {
        if (strcmp(books[i].title, title) == 0) {
            found = 1;
            for (int j = i; j < *count - 1; j++) {
                strcpy(books[j].title, books[j + 1].title);
                strcpy(books[j].author, books[j + 1].author);
                books[j].year = books[j + 1].year;
            }
            (*count)--;
            printf("Book deleted successfully!\n");
            break;
        }
    }
    if (!found) {
        printf("Book not found!\n");
    }
}

// Function to update the password
void updatePassword(char password[]) {
    char currentPassword[100];
    printf("Enter the current password: ");
    scanf("%s", currentPassword);
    if (strcmp(currentPassword, password) == 0) {
        char newPassword[100];
        printf("Enter the new password: ");
        scanf("%s", newPassword);
        strcpy(password, newPassword);
        printf("Password updated successfully!\n");
    } else {
        printf("Incorrect password!\n");
    }
}

int main() {
    struct Book books[100];
    int count = 0;
    char password[] = "admin";

    char enteredPassword[100];
    printf("Enter the password: ");
    scanf("%s", enteredPassword);

    if (strcmp(enteredPassword, password) != 0) {
        printf("Incorrect password. Exiting...\n");
        return 0;
    }

    int choice;
    do {
        printf("\nLibrary Management System Menu:\n");
        printf("1. Add Books\n");
        printf("2. Search Books\n");
        printf("3. View Books\n");
        printf("4. Delete Books\n");
        printf("5. Update Password\n");
        printf("6. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                addBooks(books, &count);
                break;
            case 2:
                searchBooks(books, count);
                break;
            case 3:
                viewBooks(books, count);
                break;
            case 4:
                deleteBook(books, &count);
                break;
            case 5:
                updatePassword(password);
                break;
            case 6:
                printf("Exiting...\n");
                break;
            default:
                printf("Invalid choice! Please try again.\n");
        }
    } while (choice != 6);

    return 0;
Editor is loading...