模拟三次密码输入的场景

这道题的重点是strcmp函数的使用,用它来实现判断密码正确性的过程,逻辑方面并无难度,用循环实现即可
具体格式为:strcmp(password,"123456")

代码如下:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int i = 0;
char password[] = { 0 };
while (i < 3)
{
printf("请输入密码:");
scanf("%s", &password);
if (strcmp(password,"123456") == 0)
{
printf("登录成功");
break;
}
else
{
printf("密码错误请重新输入\n");
}
i++;

}
if (i == 3)
{
printf("退出程序");
}
system("pause");
return 0;
}

猜你喜欢

转载自blog.51cto.com/14239789/2377690