#include <stdio.h>
#include <string.h>
int main()
{
int i = 0;
char password[20] = { 0 };
for (i = 0; i < 3; i++)
{
printf("请输入密码:>");
scanf_s("%s", password, sizeof(password));
if (strcmp(password, "abcdef") == 0)
{
printf("密码输入正确!");
break;
}
}
if (i == 3)
{
printf("三次密码输入错误,退出程序!");
}
return 0;
}
判断字符串相等要用到库函数strcmp(),不能再用==
if (strcmp(password, "abcdef") == 0);这句表达式的含义是若返回值为0,则连个字符串相等。
今天写这个代码牵扯出来scanf_s的新格式,是scanf_s("格式",变量,sizeof(变量)),此写法编译器才能通过。