功能概述: 模拟密码的三次输入,成功提示"登陆成功",失败提示"密码错误",超过三次密码错误提示"账户已被冻结"。
int strcmp(const char * str1,const char * str2):
包含在头文件 string.h 中
1、不匹配的第一个字符在ptr1中的值低于在ptr2中的值:返回值 <0
2、不匹配的第一个字符在ptr1中的值高于在ptr2中的值:返回值 >0
3、两个字符串完全相同:返回值 =0
因此: 利用该函数来比较两个字符串是否相同
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
// 模拟密码的三次输入,成功提示"登陆成功",失败提示"密码错误",超过三次密码错误提示"账户已被冻结"
int main() {
char password[] = "123456";
char user_password[] = { 0 };
int n = 0;
while (1) {
scanf("%s", user_password);
if (strcmp(user_password, password) == 0) {
printf("登陆成功!\n");
break;
} else {
printf("密码错误,请重新输入!\n");
n++;
}
if (n == 3) {
printf("密码错误次数太多,您的账户已经被冻结!\n");
break;
}
}
system("pause");
return 0;
}