【发布时间】:2015-07-06 17:30:57
【问题描述】:
如标题所述,该程序是供用户输入密码的。该程序检查四个规则以使密码通过 (1)。然后它提示用户再次输入密码以进行验证,然后程序将其与初始密码 (2) 进行比较,并说明这两个密码是否匹配并且可以接受。
(1) - 第一条规则,检查字符串长度(必须至少为 8 个字符长),是循环似乎表明它失败的地方,无论它是否失败。也就是说,如果它确实失败了,程序会让用户输入密码,直到用户输入的密码通过了这个规则。
例子:如果我输入“密码”,它会输出输入错误的提示,但如果你再次输入正确,它将继续程序的其余部分。
(2) 我似乎无法正确比较这两个字符串以检查它们的准确性,并尝试了几种技术来查看它是否会改变。许多方法导致 while 循环完全取消。
这是代码:(我很感激任何和所有的帮助,我已经在这个程序上工作了很长时间,但无济于事)
/*This program determines if the entered password is valid and if the second password entry matches the first.
*/
#include <iostream>
#include <cstring>
#include <string.h>
#include <string>
using namespace std;
void UserInput(string password);
short PasswordAlphaNumCheck(string password);
void PasswordValidationOutput(string password);
void PasswordMatch(string password, string second_password);
int main()
{
string password;
string second_password;
UserInput(password);
PasswordValidationOutput(password);
PasswordMatch(password, second_password);
return 0;
}
void UserInput(string password)
{
cout << "The Password that you are entering must: " << endl;
cout << " " << endl;
cout << "1) Be at least 8 characters long" << endl;
cout << "2) Contain at least one number" << endl;
cout << "3) Contain at least one upper case letter" << endl;
cout << "4) Contain at least one lower case letter" << endl;
cout << " " << endl;
cout << "Please enter a password now: " << endl;
cin >> password;
}
short PasswordAlphaNumCheck(string password)
{
short flag[3] = {0};
for(int i = 0; password[i] != '\0'; i++)
{
if(isdigit(password[i]))
flag[0] = 1;
if(isupper(password[i]))
flag[1] = 2;
if(islower(password[i]))
flag[2] = 4;
}
return (flag[0] + flag[1] + flag[2]);
}
void PasswordValidationOutput(string password)
{
short test = PasswordAlphaNumCheck(password);
while ((password.length() < 8) && (test < 7))
{
while (password.length() < 8)
{
cout << " " << endl;
cout << "Your password is not the correct size." << endl;
cout << "Please re-enter your password." << endl;
cin >> password;
}
cout << " " << endl;
test = PasswordAlphaNumCheck(password);
cout << password
<< " is "
<< (test == 0 ? "not supposed to cause this error. Report to inept computer engineer." :
test == 1 ? "not valid because you are missing an upper and lower case letter." :
test == 2 ? "not valid because you are missing an lower case letter and a number." :
test == 3 ? "not valid because you are missing a lower case letter." :
test == 4 ? "not valid because you are missing an upper case letter and a number" :
test == 5 ? "not valid because you are missing an upper case letter." :
test == 6 ? "not valid because you are missing a number." : "valid") << endl;
while (test < 7)
{
cout << " " << endl;
cout << "Please re-enter your password." << endl;
cin >> password;
}
}
cout << " " << endl;
}
void PasswordMatch(string password, string second_password)
{
cout << "Please enter your password again, for verification" << endl;
cin >> second_password;
while (!password.compare(second_password))
{
cout << "Your passwords do not match." << endl;
cout << " " << endl;
cout << "Please re-enter your password." << endl;
cin >> second_password;
}
cout << "Your password has been approved!" << endl;
}
【问题讨论】: