C++ - 使用 do while 进行密码验证(输入星号)

C++ - Password validation with do while (entering asterisks signs)

正如您从标题中看到的那样,我想检查密码但是使用 do while 循环。我想要做的是,要求用户输入密码,如果密码输入错误 3 次,程序应该退出。

这是代码,希望你明白我想做什么

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

int main()
{
    int n = 0;
    char s[10] = {'s','a','m','e','d'};
    char unos[10];
    int i;
    do
    {
        for (i = 0; i < 5;i++) {
        unos[i] = _getch(); 
        _putch('*');

    }
    cout << endl;
    for (i = 0; i < 5; i++)
      {
    if (unos[i] == s[i])
        {
            cout << "Your password is correct" << endl;
            break;
        }
        else if (unos[i] != s[i])
        {
            do
            {
            cout << "Your password is incorrect" << endl;
            cout << "Enter again: "; 
             for (i = 0; i < 5;i++) {
             unos[i] = _getch(); 
            _putch('*');
           }
            n++; // how many times user entered the password        
            }while(unos[i] != s[i]);
        }   
      } 
    }while(unos[i] != s[i] && n < 3);

    return 0;
}

控制台输出是正确的,或者如果我第一次输入正确的密码,它会执行我想要的操作,但如果我输入错误,之后它不会执行任何操作,或者实际上它会再次要求我输入密码但它没有,显示消息 Your password is correct。 如果您现在知道如何使用递归完成此任务,那将对我有很大帮助。 提前致谢:)

以下循环永远不会结束:

            do
        {
        cout << "Your password is incorrect" << endl;
        cout << "Enter again: "; 
         for (i = 0; i < 5;i++) {
         unos[i] = _getch(); 
        _putch('*');
       }
        n++; // how many times user entered the password        
        }while(unos[i] != s[i]);

1) n < 3是在这个循环之外完成的,所以不考虑次数

2) 在 }while(unos[i] != s[i]); i = 5 的时刻(你之前声明了 i),所以你在比较 unos[5]s[5]。这些值未初始化。

此代码中还有其他问题: 如果上述循环结束,它将退出到 for (i = 0; i < 5; i++),它也不会检查 n。 我认为主要问题是您只检查输入密码的第一个字母,因此输入 "sqwer" 会给您 "Your password is correct".

这段代码应该完全重新编写。 工作示例(如果继续您的想法)将如下所示:

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

int main() {
    int n = 0;
    char s[10] = { 's', 'a', 'm', 'e', 'd' };
    char unos[10];
    bool is_pwd_correct = false;

    do {
        for (int i = 0; i < 5; i++) {
            unos[i] = _getch();
            _putch('*');
        }

        for (int i = 0; i < 5; i++) {
            if (unos[i] != s[i]) {
                n++;
                cout << endl << "Your password is incorrect" << endl;
                break;
            } else if (i == 4) {
                is_pwd_correct = true;
                cout << endl << "Your password is correct" << endl;
            }
        }
    } while (!is_pwd_correct && n < 3);
}

但是我建议使用字符串而不是字符数组,并且不要使用 conio.h,它只是 Windows。 另外,添加对密码大小的处理也是个好主意。

浏览互联网,我阅读http://www.cplusplus.com/articles/E6vU7k9E/。 为什么不使用那里描述的 getch 和 getpass 函数,主要建议,更容易阅读?

int main()
{
  string s = {'s','a','m','e','d'};
  string unos;
  int ntry (0);
  bool ok (false);
  while (!ok && (ntry < 3)) {
    unos = getpass("Please enter the password: ",true);
    if(unos==s) {
      cout <<"Correct password"<<endl;
      ok = true;
    }
    else {
      if (ntry < 2) {
        cout <<"Incorrect password. Try again"<<endl;
      }
      else {
        cout << "access denied" << endl;
      }
      ++ntry;
    }
  }
  return 0;
}