本帖最后由 一点点儿 于 2022-8-10 14:58 编辑
while(M != "abc" ); 这句有问题,比较两个字符串不能用 != 运算符比较,两个字符串不能用C语言的运算符进行比较,
比较两个字符串可以用strcmp()函数
示例:strcmp(str1, str2)
如果两个字符串完全一致,strcmp()函数返回0
while(M != "abc" ) 应改为while(strcmp(M,"abc") != 0)
还有,要使用strcmp()函数,程序开头应加上 #include <string.h>
另外
声明数组需要指明数组元素的数量,数组初始化式完全为空也不行
char M[]={} 可以改为 char M[255]={'\0'}
把数组初始化为全\0
修改后的代码如下
- #include <stdio.h>
- #include <string.h>
- int main() {
- char M[255] = {'\0'};
- do {
- printf("请输入密码:");
- scanf("%s", M);
- } while (strcmp(M, "abc") != 0);
- printf("输入正确\n");
- return 0;
- }
复制代码