用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 09:05:46 浏览: 43
为了保证用户输入的用户号合法,我们可以先让用户输入一个数字,然后判断其是否在有效范围内(比如1~7)。如果用户号不合法,程序应该提示用户重新输入。
代码如下:
void main() {
char *password[] = {"Cat", "Mouse", "Dog", "Sugar", "Water", "Meat", "Steel"};
int retry_count = 0;
int user_no;
char user_pwd[100];
while(1) {
printf("please input No: ");
scanf("%d", &user_no);
if(user_no < 1 || user_no > 7) {
printf("Invalid user number!\n");
continue;
}
printf("please input password: ");
scanf("%s", user_pwd);
if(strcmp(user_pwd, password[user_no-1]) == 0) {
printf("It's right! Welcome...\n");
break;
} else {
printf("Wrong password!\n");
retry_count++;
if(retry_count >= 3) {
printf("Error!\n");
break;
}
}
}
}
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)