【发布时间】:2020-05-01 20:05:29
【问题描述】:
为我的班级做一个练习。我的老师希望我们使用 C-string,而不是 string 类。我们必须使用 bool 函数和 C-string 命令来确定密码是否足够强。我想我快要完成了,但我一定有什么错误?这是我得到的:
#include <iostream>
#include <cstring>
#include <cctype>
bool checklength(char[]);
bool checkdigit(char[]);
bool checklower(char[]);
bool checkupper(char[]);
bool checkspecial(char[]);
int main()
{
char pwdstr[20];
std::cout << "Enter your password\n";
std::cin >> pwdstr;
if (checklength(pwdstr) &&
checkdigit(pwdstr) &&
checklower(pwdstr) &&
checkupper(pwdstr) &&
checkspecial(pwdstr))
{
std::cout << "Your password is strong.\n";
}
else
{
std::cout << "Your password is too weak!\n";
}
}
bool checklength(char p[])
{
int i;
int len = strlen(p);
for (i = 0; i < len - 1;)
{
if (isalnum(p[i]))
{
i++;
}
}
if (i < 6)
{
std::cout << "Your password must be at least 6 characters
long.\n";
return false;
}
else
{
return true;
}
}
bool checkdigit(char p[])
{
int i;
int len = strlen(p);
for (i = 0; i < len - 1;)
{
if (isdigit(p[i]))
{
i++;
}
}
if (i < 1)
{
std::cout << "Your password must have at least 1 digit in
it.\n";
return false;
}
else
{
return true;
}
}
bool checklower(char p[])
{
int i;
int len = strlen(p);
for (i = 0; i < len - 1;)
{
if (islower(p[i]))
{
i++;
}
}
if (i < 1)
{
std::cout << "Your password must have at least 1 lower case
letter in it.\n";
return false;
}
else
{
return true;
}
}
bool checkupper(char p[])
{
int i;
int len = strlen(p);
for (i = 0; i < len - 1;)
{
if (isupper(p[i]))
{
i++;
}
}
if (i < 1)
{
std::cout << "Your password must have at least 1 upper case
letter in it.\n";
return false;
}
else
{
return true;
}
}
bool checkspecial(char p[])
{
int i;
int len = strlen(p);
for (i = 0; i < len - 1;)
{
if (ispunct(p[i]))
{
i++;
}
}
if (i < 1)
{
std::cout << "Your password must have at least 1 special
character in it.\n";
return false;
}
else
{
return true;
}
}
我在返回 false 之前添加错误描述之前的当前输出是,由于某种原因,一切都是正确的。现在我尝试的一切都表明我在 checklength 功能上失败了,密码太短了。
谢谢大家
【问题讨论】:
标签: c++ passwords boolean c-strings