要求:1.由电脑随机生成两位数的密码,需要用到rand函数。
2.可输入五次密码,并提示密码错误。如果超过五,密码错误
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int main()
{
int count = 0;
char password[3], input[10];
srand(time(NULL));
password[0] = rand() % 10 + '0';
password[1] = rand() % 10 + '0';
password[2] = '\0';
while (1)
{
printf("请输入密码: ");
scanf("%s", input);
if (strcmp(input, password) == 0)
{
printf("密码正确\n");
break;
}
else
{
printf("密码错误\n");
if (++count >= 5)
break;
}
}
return 0;
}