首页 > 解决方案 > 无限循环,我不知道为什么

问题描述

我想将两个字符串的比较用作循环的条件,但循环进入无限循环,我不知道为什么。

#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;
}

请帮帮我

标签: c

解决方案


条件错误,while 语句总是被评估为真。

while((strcmp (comando, "X"))!=0 || (strcmp (comando, "x")!=0)) 

它应该是:

while((strcmp (comando, "X"))!=0 && (strcmp (comando, "x")!=0)) 

推荐阅读