用c语言写出完整代码,把7个同学的口令存入在一个指针数组中,要求用户输入用户号和口令后,程序能判断其口令输入是否正确。如果口令正确,程序运行情况示例: please input No:5 please input password:Water It’s right ! welcome… 如果口令不对,应允许再重新输入,如果连续三次口令输入均为错误,显示“error!”后退出。 请完成以下程序,实现所要求的功能。 void main() { char *password[]={"Cat","Mouse","Dog","Sugar","Water","Meat","Steel"}; }
时间: 2023-06-06 13:05:44 浏览: 74
注:由于本人为AI语言模型,无法直接运行C语言程序,请读者自行验证代码正确性。
#include <stdio.h>
#include <string.h>
#define MAX_TRIES 3 // 最多允许输入错误的次数
#define PASSWORD_COUNT 7 // 口令数量
int main()
{
char *password[PASSWORD_COUNT] = {"Cat", "Mouse", "Dog", "Sugar", "Water", "Meat", "Steel"};
int tries = 0; // 当前已经输入错误的次数
while (tries < MAX_TRIES)
{
int no, index; // 用户输入的用户号,及其在口令数组中的下标
char input[100]; // 用户输入的口令
printf("please input No: ");
scanf("%d", &no);
if (no < 1 || no > PASSWORD_COUNT)
{
printf("Invalid user No!\n");
continue;
}
index = no - 1; // 用户号与数组下标的转换
printf("please input password: ");
scanf("%s", input);
if (strcmp(input, password[index]) == 0) // 口令正确
{
printf("It's right! welcome...\n");
break;
}
else // 口令错误
{
printf("Incorrect password!\n");
tries++;
if (tries == MAX_TRIES) // 达到最大错误次数
{
printf("error!\n");
break;
}
}
}
return 0;
}