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

C++控制台登录注册系统退出功能异常问题求助

问题分析与修复方案

问题根源

  1. forgotPassword函数的case 2分支缺失break语句:当选择2回到主菜单并执行退出后,程序会回到之前未执行完的forgotPassword函数,继续执行后续的default分支,此时option的值仍是2,触发无效输入提示。
  2. 递归调用main()属于不良设计:这种写法会导致调用栈堆积,流程逻辑混乱,还可能引发栈溢出问题。

快速修复(针对直接报错)

在forgotPassword函数的case 2分支末尾添加break语句,阻止程序继续执行到default分支:

case 2: {
    system("cls");
    main();
    break; // 添加此语句
}

最优重构方案(彻底解决流程问题)

用循环代替递归调用main(),让程序流程更清晰可控:

重构后的main函数

#include <iostream>
#include <fstream>

using namespace std;

// 函数声明
void login();
void registration();
void forgotPassword();

int main()
{
    while (true) {
        int choice;

        cout << "-------------- WELCOME --------------\n";
        cout << "::Select Action::\n\n";

        cout << "1. Login\n";
        cout << "2. Register\n";
        cout << "3. Forgot Password\n";
        cout << "4. Exit\n\n";

        cout << "Enter your choice: ";
        cin >> choice;

        cout << endl;

        switch (choice) {
            case 1:
                login();
                break;

            case 2:
                registration();
                break;

            case 3:
                system("cls");
                forgotPassword();
                break;

            case 4:
                system("cls");
                cout << "Thank you for using this app." << endl;
                return 0; // 直接退出程序

            default:
                system("cls");
                cout << "-- Invalid number " << choice << " --" << endl;
        }
    }
    return 0;
}

重构后的login函数

移除递归调用main(),执行完后自动返回主循环:

void login() {
    int count = 0;
    string userId, password, id, pass;

    system("cls");

    cout << "::Enter the username and password::\n\n" << endl;

    cout << "Username: ";
    cin >> userId;

    cout << "Password: ";
    cin >> password;

    ifstream input("users.txt");

    while (input >> id >> pass) {
        if (id == userId && pass == password) {
            count = 1;
            system("cls");
        }
    }

    input.close();

    if (count == 1) {
        cout << "Login Successful!\n\n" << userId << endl;
    }
    else {
        cout << "\n-- Username or password is invalid. --" << endl;
    }
}

重构后的registration函数

同样移除递归调用main():

void registration() {
    string userId, password, id, pass;
    system("cls");

    cout << "::Enter the username and password::\n\n" << endl;

    cout << "Username: ";
    cin >> userId;

    cout << "Password: ";
    cin >> password;

    ofstream newUser("users.txt", ios::app);

    newUser << userId << ' ' << password << endl;
    system("cls");

    cout << "-- Registration Successful! --\n\n" << endl;
}

重构后的forgotPassword函数

case 2分支直接返回,回到主循环:

void forgotPassword() {
    int option;
    
    cout << "::Forgot Password::\n\n" << endl;

    cout << "1. Search by username" << endl;
    cout << "2. Go Back\n" << endl;
    
    cout << "Select option: ";
    cin >> option;
    

    switch (option) {
    case 1 : {
        int count = 0;
        string userId, password, id, pass;

        cout << "Enter your username: ";
        cin >> userId;

        ifstream users("users.txt");

        while (users >> id >> pass) {
            if (id == userId) {
                count = 1;
            }
        }

        users.close();

        if (count == 1) {
            system("cls");
            cout << "-- User Found --\n" << endl;
            cout << "Username: " << userId << endl;
            cout << "Password: " << pass << endl;
        }
        else {
            system("cls");
            cout << "Account doesn't exist!\n" << endl;
        }

        break;
    }

    case 2: {
        system("cls");
        return; // 直接返回主循环
    }

    default:
        system("cls");
        cout << "-- Invalid number " << option << " --" << endl;
        forgotPassword();
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 21:45:32