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

C++ String Subscript Out of Range Error问题排查求助

Troubleshooting "String Subscript Out of Range" in Your Login Program

Hey there! Let's figure out why you're hitting that "String subscript out of range" error. Your code snippet cuts off mid-password input, but based on what you shared and common issues with this kind of login program, here are the most likely causes and fixes:

Common Causes & Fixes

  • You're not checking if the file opened successfully
    If admin.txt doesn't exist, is in the wrong folder, or you don't have permission to read it, adminIn will fail to open. Trying to read from a closed file can result in empty strings, and accessing their subscripts (like fileUsername[0]) will trigger the error. Add this check right after opening the file:

    adminIn.open("admin.txt");
    if (!adminIn.is_open()) {
        cerr << "Error: Couldn't open admin.txt! Make sure it's in the right folder." << endl;
        return 1; // Exit or handle the error appropriately
    }
    
  • You're accessing string subscripts without checking if the string is empty
    If a user skips entering a username/password (hits enter without typing anything), username or password will be empty. Trying to access username[0] or password[0] in this case will throw the subscript error. Always validate input before using it:

    cout << "Username: ";
    cin >> username;
    if (username.empty()) {
        cerr << "Error: Username can't be empty!" << endl;
        // Ask for input again or exit
    }
    
    cout << "Password: ";
    cin >> password;
    if (password.empty()) {
        cerr << "Error: Password can't be empty!" << endl;
        // Handle the empty input
    }
    
  • Your file-reading logic is handling empty/incomplete lines
    If admin.txt has blank lines, or you keep reading past the end of the file, you'll end up with empty fileUser/filePass strings. Accessing their subscripts will cause the error. Use a loop that checks if the read was successful:

    string fileUser, filePass;
    bool loginFound = false;
    // Only proceed if we successfully read both a username and password from the file
    while (adminIn >> fileUser >> filePass) {
        // Compare full strings, not individual characters!
        if (username == fileUser && password == filePass) {
            loginFound = true;
            break;
        }
    }
    
  • You're comparing individual characters instead of full strings
    If you wrote something like if (username[0] == fileUser[0]) instead of if (username == fileUser), you're risking subscript errors if either string is empty. Always compare full strings directly for login checks—it's safer and more accurate.

Example Fixed Code

Here's a polished version of your program that addresses all these issues:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
    string username, password;
    ifstream adminIn;

    adminIn.open("admin.txt");
    // Check file open status first
    if (!adminIn) {
        cerr << "Failed to open admin.txt. Ensure the file exists in your program's directory." << endl;
        return 1;
    }

    cout << "Admin and Librarian Login\n";
    cout << "Username: ";
    cin >> username;
    if (username.empty()) {
        cerr << "Error: Username cannot be empty!" << endl;
        adminIn.close();
        return 1;
    }

    cout << "Password: ";
    cin >> password;
    if (password.empty()) {
        cerr << "Error: Password cannot be empty!" << endl;
        adminIn.close();
        return 1;
    }

    string fileUser, filePass;
    bool loginSuccess = false;
    while (adminIn >> fileUser >> filePass) {
        if (username == fileUser && password == filePass) {
            loginSuccess = true;
            break;
        }
    }

    if (loginSuccess) {
        cout << "Login successful! Welcome back." << endl;
    } else {
        cout << "Invalid username or password. Please try again." << endl;
    }

    adminIn.close();
    return 0;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:43:25