用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 20:05:52 浏览: 43
void main() {
char *password[] = {"Cat", "Mouse", "Dog", "Sugar", "Water", "Meat", "Steel"};
int count = 0;
int No, i;
char input[20];
while (count < 3) { // 最多允许输入三次
printf("please input No: ");
scanf("%d", &No);
printf("please input password: ");
scanf("%s", input);
if (strcmp(password[No - 1], input) == 0) {
printf("It's right ! welcome...\n");
break;
}
count++;
printf("password is wrong! You have %d chance(s) left.\n", 3 - count);
}
if (count == 3) {
printf("error!\n");
}
}
相关问题
用c语言,把7个同学的口令存入在一个指针数组中,要求用户输入用户号和口令后,程序能判断其口令输入是否正确。如果口令正确,如果口令不对,应允许再重新输入,如果连续三次口令输入均为错误,显示“error!”后退出。 请完成以下程序,实现所要求的功能。 void main() { char *password[]={"Cat","Mouse","Dog","Sugar","Water","Meat","Steel"}; }
为了实现口令验证的功能,需要在程序中进行循环判断,直到输入正确或者错误次数达到三次为止。完整程序如下:
```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;
}
}
}
}
```
用c语言写完善代码,把7个同学的口令存入在一个指针数组中,要求用户输入分别用户号和口令后,程序能判断其口令输入是否正确。如果口令正确,屏幕输出欢迎进入,如果口令不对,应允许再重新输入,如果连续三次口令输入均为错误,显示“error!”后退出。 请完成以下程序,实现所要求的功能。 void main() { char *password[]={"Cat","Mouse","Dog","Sugar","Water","Meat","Steel"}; }
void main() {
char *password[]={"Cat","Mouse","Dog","Sugar","Water","Meat","Steel"};
int userId; // 用户号
char inputPwd[10]; // 存储用户输入的口令
int count = 0; // 记录输入错误次数
while(count < 3) {
printf("请输入用户号和口令(格式:用户号 口令):");
scanf("%d %s", &userId, inputPwd);
if(strcmp(inputPwd, password[userId-1]) == 0) {
printf("欢迎进入!\n");
break;
} else {
printf("口令错误,请重新输入!\n");
count++;
}
}
if(count == 3) {
printf("error!\n");
}
}
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)