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

C++使用do while循环实现带星号输入的密码验证问题求助

修正密码验证的Do-While循环实现问题

你的原代码主要有两个核心问题:一是密码验证逻辑错误,二是循环嵌套导致的变量混乱和内存越界,这就是为什么输入错误后再输正确密码也无法触发成功提示的原因。咱们一步步来修正:

问题分析

  1. 错误的验证逻辑:原代码在逐个字符比较时,只要第一个字符匹配就立刻输出"密码正确",这显然不对——应该是所有字符都和正确密码匹配才算验证通过。
  2. 循环变量冲突:内层do-while循环复用了外层的i变量,当外层for循环结束时i已经等于5,此时unos[i] != s[i]会访问数组越界的内存,导致逻辑完全混乱。
  3. 错误计数逻辑失效:原代码的n变量计数位置不对,无法正确统计错误尝试次数。

修正后的Do-While循环实现

我重构了代码,把输入逻辑封装成函数,优化了验证逻辑,确保错误次数统计准确:

#include <iostream>
#include <conio.h>
#include <cstring>
using namespace std;

// 封装密码输入逻辑:获取输入并显示星号
void inputPassword(char password[], int length) {
    for (int i = 0; i < length; i++) {
        password[i] = _getch();
        _putch('*');
    }
    password[length] = '\0'; // 确保字符串以结束符结尾,方便后续比较
    cout << endl;
}

int main() {
    const char correctPassword[] = "samed"; // 正确密码
    const int maxAttempts = 3;              // 最大错误尝试次数
    int failedAttempts = 0;                 // 记录错误次数
    char userInput[10];                     // 存储用户输入的密码

    do {
        cout << "Enter password: ";
        inputPassword(userInput, 5);

        // 验证密码:所有字符匹配才算正确
        bool isPasswordCorrect = true;
        for (int i = 0; i < 5; i++) {
            if (userInput[i] != correctPassword[i]) {
                isPasswordCorrect = false;
                break;
            }
        }

        if (isPasswordCorrect) {
            cout << "Your password is correct" << endl;
            return 0; // 验证成功,直接退出程序
        } else {
            failedAttempts++;
            if (failedAttempts < maxAttempts) {
                cout << "Your password is incorrect. You have " << (maxAttempts - failedAttempts) << " attempts left." << endl;
            } else {
                cout << "Too many incorrect attempts. Program will exit." << endl;
            }
        }
    } while (failedAttempts < maxAttempts);

    return 0;
}

关键修正点:

  • 把输入密码的逻辑封装成inputPassword函数,避免代码重复,让主逻辑更清晰。
  • 密码验证改为全字符匹配,只有所有字符都和正确密码一致才判定成功。
  • 每个循环使用独立的局部变量i,彻底避免变量冲突和内存越界问题。
  • 错误次数failedAttempts的计数逻辑准确,达到3次错误后自动退出。

递归实现版本

如果你想用递归方式实现,这里也提供一个简洁的版本:

#include <iostream>
#include <conio.h>
#include <cstring>
using namespace std;

void inputPassword(char password[], int length) {
    for (int i = 0; i < length; i++) {
        password[i] = _getch();
        _putch('*');
    }
    password[length] = '\0';
    cout << endl;
}

// 递归验证函数:传入正确密码和剩余尝试次数
void validatePassword(const char correctPassword[], int attemptsLeft) {
    if (attemptsLeft <= 0) {
        cout << "Too many incorrect attempts. Program will exit." << endl;
        return;
    }

    char userInput[10];
    cout << "Enter password: ";
    inputPassword(userInput, 5);

    if (strcmp(userInput, correctPassword) == 0) {
        cout << "Your password is correct" << endl;
        return;
    } else {
        cout << "Your password is incorrect. You have " << (attemptsLeft - 1) << " attempts left." << endl;
        validatePassword(correctPassword, attemptsLeft - 1);
    }
}

int main() {
    const char correctPassword[] = "samed";
    validatePassword(correctPassword, 3); // 初始尝试次数为3
    return 0;
}

递归思路:

每次递归调用时传入剩余的尝试次数,当次数耗尽时退出;如果验证成功则直接返回,否则递归调用自身并将尝试次数减一,逻辑非常直观。

内容的提问来源于stack exchange,提问作者Samed Škulj

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 22:17:32