C 无限循环和我不';我不知道为什么
我想用两个字符串的比较作为循环的条件, 但是循环进入无限循环,我不知道为什么C 无限循环和我不';我不知道为什么,c,C,我想用两个字符串的比较作为循环的条件, 但是循环进入无限循环,我不知道为什么 #include <stdio.h> #include <string.h> #include <stdlib.h> void input(int a[], char *comando, char *aux) { fgets(aux, 35, stdin); sscanf(aux, "%s %d %d", comando, &a[0], &a[1]
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void input(int a[], char *comando, char *aux) {
fgets(aux, 35, stdin);
sscanf(aux, "%s %d %d", comando, &a[0], &a[1]);
}
int table_size=10;
int ar_temp[2];
char comando[2];
char aux[35];
int main() {
while((strcmp (comando, "X"))!=0 || (strcmp (comando, "x")!=0)) {
input(ar_temp, comando, aux);
}
return 0;
}
#包括
#包括
#包括
无效输入(int a[],char*comando,char*aux){
fgets(辅助,35,标准);
sscanf(辅助、%s%d%d)、comando和a[0]、&a[1]);
}
int table_size=10;
内部温度[2];
char-comando[2];
char-aux[35];
int main(){
而((strcmp(comando,“X”)!=0(strcmp(comando,“X”)!=0)){
输入(ar_温度、comando、aux);
}
返回0;
}
请帮助我条件错误,while语句的计算结果始终为true
while((strcmp (comando, "X"))!=0 || (strcmp (comando, "x")!=0))
应该是:
while((strcmp (comando, "X"))!=0 && (strcmp (comando, "x")!=0))
虽然(我不是美洲驼)或(我不是狗)
即使是美洲驼和狗也会对其中一个子条款产生真实结果,因此,由于真或任何东西都是真的,所以该陈述的结果总是真的
相反,您需要的是(返回到您的实际代码,而不是我的稍微可笑的示例):
您会注意到,我还“修复”了您的第一个子句,因此括号的使用是一致的,将)
移动到0
之后
当然,如果您在其他任何地方检查了comando
,那么您可能需要先将其设置为单个大小写,然后您只需要检查小写变量。请小心——如果您的声明不是全局的,您将在第一次调用strcmp(comando,“X”)时调用未定义的行为
尝试从未初始化的值读取数据(幸运、意外或上述避免的清晰理解…)
while ((strcmp (comando, "X") != 0) && (strcmp (comando, "x") != 0)) {
// ^^
// and rather than or