出现错误后需要帮助重置密码

Need help resetting the password after getting an ERROR

本文关键字:帮助 密码 错误      更新时间:2023-10-16
大家好,我只是想知道在输入无效输入后,我应该在哪里以及如何重置密码。例如,在用户收到错误后输入正确的密码后,它会使用正确的密码进行复制。因此,当用户在密码中输入"空格"时,当错误触发时,它会不断给我一个错误,即即使没有空格,我的输入仍然有空格。enter code here
do 
{
    int i = 0;
    flag = true;
    cin.clear();                // reset the input stream 
    cout << "Enter your passwordn";
    ch = _getch();
    while(ch != 13)
       {//character 13 is enter - carriage return
             pass += ch;
             cout << '*';
             ch = _getch();
       }         
       cout << "nThe password you entered was " << pass << endl;
       cin.clear();             // reset the input stream 
    while (i < pass.length() && flag)
    {
        if (pass.at(i) == ' ')
        {
            cerr << "ERROR: PASSWORD CANNOT CONTAIN ANY SPACES!" << endl;
            flag = false;
        }
               i++;
    }   
        if (pass.length() < 5)
        {
            cerr << "ERROR: PASSWORD IS TOO SHORT!" << endl;
            flag = false;
        }
          cout << endl; 
} 

这将起作用:

string pass;
bool flag;
do
{
  flag = true;
  cout << "Enter your passwordn";
  getline(cin, pass);
  cout << "Pass is " << pass << endl;
  unsigned int i=0;
  while (i < pass.length() && flag)
  {
    if (pass.at(i) == ' ')
    {
      cout << "ERROR: PASSWORD CANNOT CONTAIN ANY SPACES!" << endl;
      flag = false;
    }
    ++i;
  }   
}while(flag == false);