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

基于二进制文件的用户名密码系统登录匹配失败技术求助

Troubleshooting Your Login Mismatch Issue

Let's walk through the key problems in your code that are preventing correct login matches, plus fixes for each:

1. Incorrect String Comparison (The Root Cause)

In C++, you can't compare C-style character arrays (char[]) with the == operator—this checks if the memory addresses of the arrays are the same, not if their content matches. That's why even with correct input, your condition always returns false.

Fix this by using the strcmp() function from the <cstring> header, which compares the actual string content. Replace your login check with:

if (strcmp(firstname, u[i].fname) == 0 && 
    strcmp(lastname, u[i].lname) == 0 && 
    strcmp(password, u[i].pword) == 0) {
    check = true;
    cout << "IT WORKS!!!!";
    break; // No need to check other users once we find a match
}

Don't forget to add #include <cstring> at the top of your file to use strcmp().

2. Flawed getuserno() Logic

Your current loop uses !f.eof() as the condition, which causes an extra, invalid read. The eof() flag only triggers after a failed read attempt, so you'll end up incrementing userno one too many times. This means your login loop may try to read beyond the actual number of users, leading to garbage data comparisons.

Rewrite getuserno() to read first, then check if the read succeeded:

void getuserno() { // Add explicit return type (void)
    fstream f("user.dat", ios::in | ios::binary);
    userno = 0; // Reset count before reading
    while (f.read((char*)&u[userno], sizeof(u[userno]))) {
        userno++;
    }
    f.close(); // Always close files after use to avoid leaks
}

3. Ensure getuserno() Is Called Before Login/Signup

If you don't call getuserno() at the start of your program (e.g., in main()), the global userno will stay at 0. This means your login loop won't iterate through all existing users, and new signups will overwrite existing data.

Add this to your main() function:

int main() {
    getuserno(); // Initialize user count first
    // Rest of your code (call signin() or login() as needed)
    return 0;
}

4. Minor Fixes for Robustness

  • The goto x; in your login function skips the system("cls"); right after it—move the system("cls"); before the goto if you want the screen to clear before re-prompting.
  • In signin(), your age check only re-prompts once. Use a loop to ensure the user enters a valid age:
    do {
        cout << "Enter your age\n";
        cin >> u[userno].age;
        if (u[userno].age < 18) {
            cout << "You must be 18 or older. Try again.\n";
        }
    } while (u[userno].age < 18);
    

After applying these fixes, your login function should correctly match user input against the stored binary data.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 07:23:45