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;
你的文字几乎无法理解,但听起来好像你忘记重置标志了。对不起,我的英语不是很好。但是,是的,我如何重置标志?