如何保持循环,直到密码满足C++条件
How to keep looping until password meets condition in C++?
我正在尝试继续循环以要求用户输入新密码,直到满足条件。将此新密码与传递到此函数的旧密码进行比较。条件是,新密码和旧密码的前半部分不能相同,新密码和旧密码的后半部分不能相同。如果满足此条件,旧密码将更改为新密码。到目前为止,我有这个,但它只工作一次,并且在满足条件之前不会一直循环要求新密码。有没有办法让它继续循环,直到新密码通过条件?
bool changePasswordintonewPW(string& oldpassword)
{
string newPW;
int sizeN;
int half;
int lengtholdPW;
int lengthnewPW;
do
{
cout << "Enter a new password that must meet condition: ";
getline(cin, newPW);
lengthnewPW = newPW.length();
lengtholdPW = oldpassword.length();
if (lengthnewPW < lengtholdPW)
{
sizeN = lengthnewPW;
}
else if (lengtholdPW < lengthnewPW)
{
sizeN = lengtholdPW;
}
else
sizeN = lengtholdPW;
half = sizeN / 2;
if( ( oldpassword.size() <= 2 ) ||
( ( oldpassword.substr( 0, half ) != newPW.substr( 0, half ) ) &&
( oldpassword.substr( oldpassword.size() - half ) != newPW.substr( newPW.size() - half ) ) ) )
return true;
{
cout << "The new password cannot start or end with " << half
<< " or more characters that are the same as the old password" << endl << endl;
}
} while (( oldpassword.size() <= 2 ) ||
( ( oldpassword.substr( 0, half ) != newPW.substr( 0, half ) ) &&
( oldpassword.substr( oldpassword.size() - half ) != newPW.substr( newPW.size() - half ) ) ) );
oldpassword = newPW;
return true;
}
您的 while 条件与密码为真的条件相同。 您必须在密码不为真时设置它。加!到同时条件。
相关文章:
- 即使没有满足他们的条件,我也无法通过一些 do-while 循环
- c++中的oop(密码生成)
- 密码登录程序将永远循环并显示不正确的结果
- OpenSSL没有共享密码
- Cryptopp:获取密码输入的填充字符串
- 密码长度验证 (c++)
- 如果仍然不满足要求,如何使 if 语句重复?
- C++ win32 如何使密码字段可选并启用复制和粘贴?
- 仿射密码解密,输出大小写不同
- 替换密码:哪一个?
- 无法使用 openssl 服务器,因为密码已过时...什么的...ERR_SSL_VERSION_OR_CIPHER_MISMATCH
- 如何反复调用函数直到满足模拟?
- 如何有效地操作满足给定谓词的向量中的所有项目?
- 如何使用 c++ 在 sqlite3 中打开受密码保护的数据库?
- 根据用户名和密码 Qt C++重新访问数据库值
- 具有不满足严格弱排序的值的 C++ 顺序范围
- C++密码问题
- C++ 即使不满足条件,循环也会终止
- 无法在 Cocos2dx 中使用受密码保护的 zip 文件
- 如何保持循环,直到密码满足C++条件