c语言做输入一个用户登陆验证,密码最多输入三次
时间: 2023-05-21 14:06:17 浏览: 566
我们可以使用 while 循环来实现密码验证的功能,具体实现代码如下所示:
```c
#include <stdio.h>
#include <string.h>
int main() {
char username[10] = "admin";
char password[10] = "123456";
char input_name[10], input_pwd[10];
int count = 0;
printf("请输入用户名: ");
scanf("%s", input_name);
while (strcmp(input_name, username) != 0) {
printf("用户名输入错误,请重新输入: ");
scanf("%s", input_name);
}
printf("请输入密码: ");
scanf("%s", input_pwd);
while (strcmp(input_pwd, password) != 0) {
count++;
if (count == 3) {
printf("密码输入错误超过3次,程序退出\n");
return 0;
}
printf("密码输入错误,请重新输入: ");
scanf("%s", input_pwd);
}
printf("登陆成功\n");
return 0;
}
```
以上代码实现了一个简单的用户登陆验证功能,用户输入用户名和密码,程序会判断输入是否正确,如果不正确则给出提示,最多允许输入密码错误3次,如果超过3次则程序退出。