假设一个密码,允许用户输三次,三次错误后程序关闭
#include <stdio.h>
#include <string.h>
int main()
{
int i = 0;
char password[20] = { 0 };
for (i = 0; i < 3; i++)
{
printf("请输入密码:>");
scanf("%s", password);
if (strcmp(password, "123456") == 0)
//strcmp函数用来比较字符串是否相同,按ASCII码顺序比较两个数组字符串,需要引用头文件string.h
//格式:strcmp(字符数组名1,字符数组名2);
//字符串1=字符串2 返回0
//字符串1>字符串2 返回1
//字符串1<字符串2 返回-1
{
printf("登录成功\n");
break;
}
else
{
printf("密码错误\n");
}
}
if (i == 3)
{
printf("三次密码均错误,退出程序\n");
}
return 0;
}