在C中比较密码的字符串
我编辑了代码部分,据我所知,当输入密码A23bc5时,它应该正常运行。但是,它总是返回错误的密码。有什么想法吗?在C中比较密码的字符串,c,string-comparison,strcmp,C,String Comparison,Strcmp,我编辑了代码部分,据我所知,当输入密码A23bc5时,它应该正常运行。但是,它总是返回错误的密码。有什么想法吗?strcmp如果字符串相等,函数将返回0。您应该编辑条件块 编辑: 实际上你也有一些非常基本的语法问题 void main() { Password(); } int Password() { // Declare local variables// char cPassCode[] = "String"; int iFlag, iCompariso
strcmp
如果字符串相等,函数将返回0。您应该编辑条件块
编辑:
实际上你也有一些非常基本的语法问题
void main()
{
Password();
}
int Password()
{
// Declare local variables//
char cPassCode[] = "String";
int iFlag, iComparison = 0;
// Run the code to check the password//
while (iFlag = 0)
{
printf("Please enter the password: ");
scanf("%s", cPassCode);
iComparison = strcmp(cPassCode, "A23bc5");
if (iComparison = 0)
{
ArrayPrinter(Array);
iFlag = 1;
}
else
{
printf("Wrong password");
iFlag = 0;
}
return(iFlag);
}
}
这将起作用,如果字符串相等,则strcmp函数返回0。您应该编辑条件块
int Password()
{
//declare local variables//
char cPassCode[] = "String";
int iFlag=0, iComparison = 0;
//Run the code to check the password//
while (iFlag == 0)
{
printf("Please enter the password: ");
scanf("%s", cPassCode);
iComparison = strcmp(cPassCode,"A23bc5");
if (iComparison == 0)
{
printf("\n Accepted");
iFlag = 1;
}
else
{
printf("Wrong password");
iFlag = 0;
}
return(iFlag);
}
}
int main()
{
Password();
return 0;
}
编辑:
实际上你也有一些非常基本的语法问题
void main()
{
Password();
}
int Password()
{
// Declare local variables//
char cPassCode[] = "String";
int iFlag, iComparison = 0;
// Run the code to check the password//
while (iFlag = 0)
{
printf("Please enter the password: ");
scanf("%s", cPassCode);
iComparison = strcmp(cPassCode, "A23bc5");
if (iComparison = 0)
{
ArrayPrinter(Array);
iFlag = 1;
}
else
{
printf("Wrong password");
iFlag = 0;
}
return(iFlag);
}
}
这会奏效的
int Password()
{
//declare local variables//
char cPassCode[] = "String";
int iFlag=0, iComparison = 0;
//Run the code to check the password//
while (iFlag == 0)
{
printf("Please enter the password: ");
scanf("%s", cPassCode);
iComparison = strcmp(cPassCode,"A23bc5");
if (iComparison == 0)
{
printf("\n Accepted");
iFlag = 1;
}
else
{
printf("Wrong password");
iFlag = 0;
}
return(iFlag);
}
}
int main()
{
Password();
return 0;
}
正在将0分配给变量,然后对其进行测试,0的计算结果为false
if (iComparison = 0)
正在检查变量是否为0,这可能就是您的意思
正在将0分配给变量,然后对其进行测试,0的计算结果为false
if (iComparison = 0)
正在检查变量是否为0,这可能是您的意思打印出cPassCode的每个字符的值。它正在打印正确的密码,但仍然表示密码不正确检查密码时切勿使用strcmp(),因为这会使您的应用程序易受定时攻击!为了防止出现这种情况,您可以创建输入密码和真实密码的md5/sha哈希并进行比较。注意:对于密码,最好在之后将缓冲区归零。然而,对于嵌入了
“A23bc5”
的代码,这种安全性改进是没有意义的,也是一个好主意。打印出cPassCode的每个字符的值。它打印正确的密码,但仍然说密码不正确。在检查密码时千万不要使用strcmp(),因为这会使您的应用程序容易受到定时攻击!为了防止出现这种情况,您可以创建输入密码和真实密码的md5/sha哈希并进行比较。注意:对于密码,最好在之后将缓冲区归零。然而,对于嵌入了“A23bc5”
的代码,这种安全性改进不仅是一个好主意,而且是毫无意义的。这不可能是简单的-P肯定先尝试了一个无效的密码,当然??这不可能是简单的-P肯定先尝试了一个无效的密码,当然??没有人再打开警告了吗?:(@MartinJames)编程新手通常不知道如何正确使用IDE,这是有道理的。他们已经很难使用IDE语言了:P@MartinJames:根据我的经验,问题更多的是忽略警告是的。谢谢!没有人再打开警告了吗((@MartinJames)编程新手通常不知道如何正确使用IDE,这是有道理的。他们已经很难使用IDE语言了:P@MartinJames当前位置根据我的经验,问题往往是忽略警告是的。谢谢!