用c语言,把7个同学的口令存入在一个指针数组中,要求用户输入用户号和口令后,程序能判断其口令输入是否正确。如果口令正确,如果口令不对,应允许再重新输入,如果连续三次口令输入均为错误,显示“error!”后退出。 请完成以下程序,实现所要求的功能。 void main() { char *password[]={"Cat","Mouse","Dog","Sugar","Water","Meat","Steel"}; }
时间: 2023-06-06 07:05:39 浏览: 134
为了实现口令验证的功能,需要在程序中进行循环判断,直到输入正确或者错误次数达到三次为止。完整程序如下:
```c
#include <stdio.h>
#include <string.h>
void main()
{
char *password[] = {"Cat", "Mouse", "Dog", "Sugar", "Water", "Meat", "Steel"};
char input_password[20];
int user_id = 0;
int error_count = 0;
int i;
// 循环读入用户号和口令
while (1) {
printf("请输入用户号:");
scanf("%d", &user_id);
printf("请输入口令:");
scanf("%s", input_password);
// 判断口令是否正确
if (strcmp(input_password, password[user_id-1]) == 0) {
printf("口令正确!\n");
break;
} else {
error_count ++;
printf("口令错误!\n");
if (error_count >= 3) {
printf("错误次数达到三次,程序退出!\n");
break;
}
}
}
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)