一、描述:
编写代码模拟三次密码输入的场景。
最多能输入三次密码,密码正确,提示“登录成功”,密码错误,
可以重新输入,最多输入三次。三次均错,则提示退出程序。
二、分析:
strcmp函数作用是:两个字符串比较。
设这两个字符串为str1,str2。
若str1==str2,则返回零;
若str1<str2,则返回负数;
若str1>str2,则返回正数。
注意:两个字符串自左向右逐个字符相比(按ASCII值大小相比较),直到出现不同的字符或遇’\0’为止。
比如:“A”<“B”,“a”>“A”,“compture”>“compare”,“DOG”<“cat”。
在使用字符串处理函数时,应该加头文件 #include<string.h>
三、程序:
#include<stdio.h>
#include<windows.h>
#include<string.h>
#pragma warning(disable:4996)
#define USER "Lily"
#define PSD "123456"
int main(){
char name[16];
char password[16];
int count = 3;
while (count > 0){
count--;
printf("请输入你的姓名:");
scanf("%s", name);
printf("请输入你的密码:");
scanf("%s", password);
if (strcmp(USER, name) == 0 && strcmp(PSD, password) == 0){
int i = 5; //加载(缓冲)5s
while (i > 0){
printf("loading...%d\r", i);
Sleep(1000);
i--;
}
printf("登陆成功...\n");
break;
}
else{
printf("您输入有误,请重新输入\n");
}
}
system("pause");
return 0;
}