C++ String Subscript Out of Range Error问题排查求助
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
Ifadmin.txtdoesn't exist, is in the wrong folder, or you don't have permission to read it,adminInwill fail to open. Trying to read from a closed file can result in empty strings, and accessing their subscripts (likefileUsername[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),usernameorpasswordwill be empty. Trying to accessusername[0]orpassword[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
Ifadmin.txthas blank lines, or you keep reading past the end of the file, you'll end up with emptyfileUser/filePassstrings. 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 likeif (username[0] == fileUser[0])instead ofif (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

