只允许输入三次密码,如果密码正确则提示登录成,如果三次均输入错误,则退出程序。
cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h> //strcmp用到的库函数
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函数串比较函数,比较是否相等
{
printf("登陆成功\n");
break;
}
else
{
printf("密码错误\n");
}
}
if(i == 3)
printf("三次均输入错误,退出程序\n");
return 0;
}
函数 strcmp 的用法:
cpp
函数名: strcmp
功 能: 串比较
用 法: int strcmp(char *str1, char *str2);
程序例:
#include <string.h>
#include <stdio.h>
int main(void)
{
char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";
int ptr;
ptr = strcmp(buf2, buf1);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1\n");
else
printf("buffer 2 is less than buffer 1\n");
ptr = strcmp(buf2, buf3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 3\n");
else
printf("buffer 2 is less than buffer 3\n");
return 0;
}