【问题标题】:C++ - Password validation with do while (entering asterisks signs)C++ - 使用 do while 验证密码(输入星号)
【发布时间】:2020-09-28 13:39:14
【问题描述】:

从标题中可以看出,我想检查密码,但使用 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。 如果您现在即使使用递归也如何完成此任务,它将对我有很大帮助。 在此先感谢:)

【问题讨论】:

  • &lt;conio.h&gt; 仅适用于 Windows,添加 [windows] 标签会很好。

标签: c++ arrays validation passwords


【解决方案1】:

浏览互联网,我读到了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;
}

【讨论】:

    【解决方案2】:

    以下循环永远不会结束:

                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 &lt; 3 是在这个循环之外完成的,所以不考虑次数

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

    此代码中还有其他问题: 如果提到的循环结束,它将退出到for (i = 0; i &lt; 5; i++),它也没有检查n。 我认为主要问题是您只检查输入密码的第一个字母,因此输入“sqwer”会给您“您的密码正确”。

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

    #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);
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-02
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-25
      • 2015-05-24
      相关资源
      最近更新 更多