You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

图书馆系统卡片目录增查及文件读写功能故障技术问询

Fixing Your Library Card Catalog Menu & File Operations

Hey there! Let’s work through your library card catalog code issues together—since you want to learn rather than get a complete rewrite, I’ll break down exactly what’s missing and how to fix each part step by step.

First: Fix the Core Flow in main()

Right now, your code has the wrong order of operations:

  • It starts the menu loop, but then immediately skips past it to bulk-enter 10 entries and display them. That’s why your menu choices don’t do anything—they’re just a quick detour before the program runs its hardcoded steps.
  • The menu should be the only way users interact with the system. You need to remove that bulk entry/display code from main() and let the menu trigger those actions instead.

Second: Make processMenu() Actually Do Something

Your menu currently only asks for input (like a spot number) but never executes the actual logic for adding, viewing, or saving entries. Plus, it has a critical issue: the function is declared to return a bool, but most code paths don’t return anything—this causes undefined behavior.

Let’s fix processMenu() first, then add the missing logic as reusable functions.

Step 1: Fix the Return Value & Menu Clarity

First, update the menu to show the exit option (users won’t know to type 7 otherwise) and ensure every code path returns a value. We’ll return true to keep the menu running, and false only when the user chooses to exit:

bool processMenu() {
    int choice, spot;
    cout << "\n=== Library Card Catalog Main Menu ===" << endl;
    cout << "1. Add a new entry" << endl;
    cout << "2. Review an entry" << endl;
    cout << "3. Save all entries to file" << endl;
    cout << "4. Load entries from file" << endl;
    cout << "7. Exit" << endl;
    cout << "Enter your choice: ";
    cin >> choice;

    switch (choice) {
        case 1:
            // We'll add addEntry logic here soon
            return true;
        case 2:
            // We'll add review logic here soon
            return true;
        case 3:
            // We'll add save logic here soon
            return true;
        case 4:
            // We'll add load logic here soon
            return true;
        case 7:
            cout << "Thanks for using the system! Goodbye." << endl;
            return false; // Exit the menu loop
        default:
            cout << "Invalid choice—please try again." << endl;
            return true;
    }
}

Step 2: Extract Reusable Functions

To avoid repeating code and keep things organized, create separate functions for each core action. Add these declarations before processMenu() (C++ requires functions to be declared before they’re used):

// Your struct definition stays the same (fixed typo: "libary" → "library")
struct library {
    string title;
    string author;
    string ISBN_code;
    int page_count;
    int publish_year;
} s[10];

// Function declarations
void addEntry(int index);
void displayEntry(int index);
void saveEntriesToFile();
void loadEntriesFromFile();

Function 1: Add an Entry

Take the bulk entry code you wrote and adapt it to target a specific index (the "spot" the user chooses). This lets users add entries one at a time via the menu:

void addEntry(int index) {
    if (index < 0 || index >= 10) {
        cout << "Invalid spot! Please choose 0-9." << endl;
        return;
    }

    cout << "\n=== Adding Entry " << index << " ===" << endl;
    cout << "Enter title: ";
    cin >> s[index].title;
    cout << "Enter author: ";
    cin >> s[index].author;
    cout << "Enter page count: ";
    cin >> s[index].page_count;
    cout << "Enter publish year: ";
    cin >> s[index].publish_year;
    cout << "Enter 13-digit ISBN: ";
    cin >> s[index].ISBN_code;

    // Validate ISBN length (your existing logic works great!)
    while (s[index].ISBN_code.length() != 13) {
        cout << "Error! ISBN must be 13 digits. Try again: ";
        cin >> s[index].ISBN_code;
    }
    cout << "Entry added successfully!" << endl;
}

Function 2: Display an Entry

Let users view a single entry by its spot number:

void displayEntry(int index) {
    if (index < 0 || index >= 10) {
        cout << "Invalid spot! Please choose 0-9." << endl;
        return;
    }

    cout << "\n=== Entry " << index << " Details ===" << endl;
    cout << "Title: " << s[index].title << endl;
    cout << "Author: " << s[index].author << endl;
    cout << "Page Count: " << s[index].page_count << endl;
    cout << "Publish Year: " << s[index].publish_year << endl;
    cout << "ISBN: " << s[index].ISBN_code << endl;
}

Function 3: Save Entries to File

Your current save code only writes a fixed string—we need to write the actual entry data. We’ll use newlines to separate fields so loading is easier:

void saveEntriesToFile() {
    ofstream outFile("library_entries.txt");
    if (!outFile.is_open()) {
        cout << "Error: Could not open file for saving!" << endl;
        return;
    }

    // Write all 10 entries to the file
    for (int i = 0; i < 10; i++) {
        outFile << s[i].title << endl;
        outFile << s[i].author << endl;
        outFile << s[i].page_count << endl;
        outFile << s[i].publish_year << endl;
        outFile << s[i].ISBN_code << endl;
    }

    outFile.close();
    cout << "All entries saved to library_entries.txt!" << endl;
}

Function 4: Load Entries from File

Now we’ll read the data back in, matching the format we used to save:

void loadEntriesFromFile() {
    ifstream inFile("library_entries.txt");
    if (!inFile.is_open()) {
        cout << "Error: Could not open file for loading!" << endl;
        return;
    }

    // Read each entry's fields in order
    for (int i = 0; i < 10; i++) {
        inFile >> s[i].title;
        inFile >> s[i].author;
        inFile >> s[i].page_count;
        inFile >> s[i].publish_year;
        inFile >> s[i].ISBN_code;

        // Optional: Validate ISBN after loading
        if (s[i].ISBN_code.length() != 13) {
            cout << "Warning: Entry " << i << " has an invalid ISBN in the file." << endl;
        }
    }

    inFile.close();
    cout << "All entries loaded from library_entries.txt!" << endl;
}

Step 3: Hook Functions into the Menu

Now update processMenu() to call these functions when the user selects an option:

bool processMenu() {
    int choice, spot;
    cout << "\n=== Library Card Catalog Main Menu ===" << endl;
    cout << "1. Add a new entry" << endl;
    cout << "2. Review an entry" << endl;
    cout << "3. Save all entries to file" << endl;
    cout << "4. Load entries from file" << endl;
    cout << "7. Exit" << endl;
    cout << "Enter your choice: ";
    cin >> choice;

    switch (choice) {
        case 1:
            cout << "Enter the spot (0-9) to add an entry: ";
            cin >> spot;
            addEntry(spot);
            return true;
        case 2:
            cout << "Enter the spot (0-9) to review: ";
            cin >> spot;
            displayEntry(spot);
            return true;
        case 3:
            saveEntriesToFile();
            return true;
        case 4:
            loadEntriesFromFile();
            return true;
        case 7:
            cout << "Thanks for using the system! Goodbye." << endl;
            return false;
        default:
            cout << "Invalid choice—please try again." << endl;
            return true;
    }
}

Final: Update main()

Remove the bulk entry/display code and let the menu drive everything:

int main() {
    cout << "Welcome to the Library Card Catalog System!" << endl;
    // Run the menu loop until processMenu returns false (user exits)
    while (processMenu()) {}
    return 0;
}

Key Takeaways for Learning

  • Menu-driven flow: The menu should be the central hub for all user actions—never hardcode actions that should be user-controlled.
  • Modular functions: Breaking code into small, single-purpose functions makes it easier to debug, reuse, and understand.
  • Error handling: Always check if files opened successfully, and validate user input (like spot numbers) to avoid crashes.
  • Function return values: Never leave a non-void function without a return value—this causes undefined behavior in C++.

内容的提问来源于stack exchange,提问作者thly01

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:45:21