gpt4 book ai didi

c++ - 如何保持循环直到密码满足 C++ 中的条件?

转载 作者:行者123 更新时间:2023-11-28 05:52:58 26 4
gpt4 key购买 nike

我正在尝试继续循环以要求用户输入新密码,直到满足条件为止。将此新密码与传递给此函数的旧密码进行比较。条件是,新旧密码的前半部分不能相同,新旧密码的后半部分不能相同。如果满足此条件,则旧密码将更改为新密码。到目前为止我有这个,但它只工作一次,并且在满足条件之前不会一直循环要求输入新密码。有没有办法让它一直循环直到新密码通过条件?

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 条件与密码为真的条件相同。您必须在密码为 true 时设置它。添加 !到 while 条件。

关于c++ - 如何保持循环直到密码满足 C++ 中的条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34832850/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com