Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 收到错误后需要帮助重置密码吗_C++_String_Vector - Fatal编程技术网

C++ 收到错误后需要帮助重置密码吗

C++ 收到错误后需要帮助重置密码吗,c++,string,vector,C++,String,Vector,嗨,伙计们,我只是想知道在输入无效输入后,我应该在哪里以及如何重置密码。例如,用户在收到错误后输入正确的密码后,会使用正确的密码复制该密码。因此,当用户在密码中输入一个“空格”并触发错误时,它会不断给我一个错误,即即使没有空格,我的输入仍然有空格。在此处输入code do { int i = 0; flag = true; cin.clear(); // reset the input stream cout <<

嗨,伙计们,我只是想知道在输入无效输入后,我应该在哪里以及如何重置密码。例如,用户在收到错误后输入正确的密码后,会使用正确的密码复制该密码。因此,当用户在密码中输入一个“空格”并触发错误时,它会不断给我一个错误,即即使没有空格,我的输入仍然有空格。
在此处输入code

do 
{
    int i = 0;
    flag = true;

    cin.clear();                // reset the input stream 
    cout << "Enter your password\n";
    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; 

} 
do
{
int i=0;
flag=true;
cin.clear();//重置输入流

这会起作用的:

string pass;
bool flag;
do
{
  flag = true;

  cout << "Enter your password\n";
  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);
字符串传递;
布尔旗;
做
{
flag=true;

你的文字几乎无法理解,但听起来好像你忘记重置
标志了。对不起,我的英语不是很好。但是,是的,我如何重置标志?