他while
告诉我比较不存在
#include <stdio.h>
int main() {
int n, e, op, edad;
char sexo[1], res[2];
n = 0;
edad = 0;
do {
n = n + 1;
printf("Edad del conductor\n");
scanf("%d", &e);
printf("Sexo de conductor F= femenino, M= masculino\n");
scanf("%s", sexo);
printf("1 si es capital y 2 otra parte del estado\n");
scanf("%d", &op);
if (edad < 30) {
edad = edad + e;
}
printf("desea ingresar otro usuario? Si o No?\n");
scanf("%s", res);
} while (res != "No");
edad = edad / n;
printf("%d", edad);
return 0;
}
问题是在 C 中你不能使用操作符比较字符串
==
,它们也!=
不会只比较两者的指针。你应该使用一个函数来比较像
strcmp
0
当它们相等时函数返回。根据您的评论,如果您需要操作单个字符,那么您不需要对字符串使用函数,您可以将程序更改为如下所示:
请注意,它
res
现在被声明为char
without[]
。scanf
现在使用格式%c
读取单个字符并'N'
使用单引号'
不"
表示它不是字符串。为此使用函数很方便,例如:
但是如果我们想看到这样的数组,一般来说数组......
数组作为一个整体是逐个值、逐个索引进行比较的。
在 C 中,字符串以字符 '\0' 结尾,这表明在其位置(以及从其位置开始)字符串中不再有字符。
如果我们想在 C 中实现一个函数来比较字符数组中的字符串,我建议使用此代码..
注意:顺便说一下,在键盘输入的时候,记住scanf不支持空格,如果我们想支持空格,我们必须使用
gets(arrayDeCaracteres);