比较两个密码以在C++中进行验证
Comparing two passwords for validation in C++
我正在尝试比较两个C++字符串。此函数传递要与新密码进行比较的旧密码。此新密码的条件是新密码和旧密码的前半部分不能相同,新密码和旧密码的后半部分不能相同。例如,如果旧密码是 ABCDEFGH,新密码是 ABCDyzyz,则不接受新密码,因为这些密码的前半部分是相同的。到目前为止,我已经想出了这个,它正在运行,但显示没有显示输出语句。我是否正确比较它们?
bool changePassword(string& password)
{
string newPW;
int lengthnewPW;
int lengtholdPW;
float sizeN;
float half;
do
{
cout << "Enter new password: ";
getline(cin, newPW);
lengthnewPW = newPW.length();
lengtholdPW = password.length();
if (lengthnewPW < lengtholdPW)
{
sizeN = lengthnewPW;
}
else if (lengtholdPW < lengthnewPW)
{
sizeN = lengtholdPW;
}
else
sizeN = lengtholdPW;
half = sizeN / 2;
if (newPW.compare(0, half - 1, password) == 0 || newPW.compare(half, (sizeN - half) - 1, password) == 0)
{
cout << "The new password cannot start or end with " << half << " or more characters that are the same as the old password" << endl << endl;
}
} while (newPW.compare(0, half - 1, password) == 0 || newPW.compare(half, (sizeN - half) - 1, password) == 0 );
return true;
}
尝试:
bool changePassword(const string& password)
{
for( ; ; )
{
string newPW;
cout << "Enter new password: ";
getline( cin, newPW );
size_t half = ( newPW.length() < password.length() ? newPW.length() : password.length() ) >> 1;
// May want to to add: if( !newPW.empty() )
if( ( password.size() <= 2 ) ||
( ( password.substr( 0, half ) != newPW.substr( 0, half ) ) &&
( password.substr( password.size() - half ) != newPW.substr( newPW.size() - half ) ) ) )
return true; // <-- Will you ever return non-true? Max retries?
cout << "The new password cannot start or end with " << half << " or more characters that are the same as the old password" << endl << endl;
}
return true;
}
相关文章:
- 正在尝试了解输入验证循环
- 如何在C++中检查2D数组中负值的输入验证
- LibGit2 SSH身份验证失败
- Vulkan验证层不断在VkQueuePresentKHR()上抛出图像布局错误
- constexpr上下文中std::initializer_list的验证
- 正在验证c++中用户的整数输入
- 加密++验证大文件签名
- C++卡验证问题
- 验证指针链
- 此代码验证公式是什么意思?
- cin 的十进制输入验证?
- 简单的 HTML 验证器
- 密码长度验证 (c++)
- 输入验证以筛选出字符、字符串和一系列整数
- 如何使用 CredUIPromptForWindowsCredentials验证凭据
- 指针验证的最佳实践?
- RSA_PKCS1签名验证
- 验证openssl c++中的签名,这是由JAVA DSA签名的?
- 使用 C++ 验证 NMEA 句子
- 为什么 WinInet 在通过 FQDN 连接时无法通过协商自动进行身份验证,但如果通过 IP 连接则成功?