大家好我想知道在输入无效输入后我应该在何处以及如何重置密码。例如,用户在收到错误后输入正确的密码后,会使用正确的密码进行复制。因此,当用户放置"空间"在密码中,当错误触发时,它一直给我一个错误,即我的输入仍有空格,即使没有。enter code here
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;
}
答案 0 :(得分:0)
这会起作用:
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);