C 在while循环内的if-else条件中,计数器值仅递增一次

C 在while循环内的if-else条件中,计数器值仅递增一次,c,if-statement,while-loop,C,If Statement,While Loop,以下代码的目的是 扫描bot的位置 扫描位置的状态,无论它是脏的还是干净的 持续执行此操作,直到机器人连续获得3次“否”状态 问题是: 计数器值只增加一次,计数器=1,然后从不增加。这就是为什么循环永远不会结束 #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) { char location[4]; cha

以下代码的目的是

  • 扫描bot的位置
  • 扫描位置的状态,无论它是脏的还是干净的
  • 持续执行此操作,直到机器人连续获得3次“否”状态
  • 问题是: 计数器值只增加一次,
    计数器=1
    ,然后从不增加。这就是为什么循环永远不会结束

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    
    int main(int argc, char *argv[]) {
        char location[4];
        char state[4];
        int count=0;
    
        //If the bot get state "no" 3 times continuosly, end the loop.
        while(count<3){
    
            //Scan bot location
            printf("Bot Location ?\n");                 
            scanf("%s",location);
    
            if(strcmp(location, "a")==0){
                printf("Is it dirty?\n");
                //Scan state of the location, dirty or clean (yes or no)
                scanf("%s",state);
    
                if(strcmp(state,"yes")==0){
    
                    //Reset the counter value to zero if state is yes
                    count=0;
    
                    //printing the counter value
                    printf("Value of Counter %d \n",count);
                    printf("Clean\n");
                    printf("Go left to B\n");
                }
                else if (strcmp(state,"no")==0) {
                    printf("Go Left to B\n");
    
                    //Counter increment if state is no
                    count++;
                    printf("Value of Counter %d \n",count);
                }
                else {
                    printf("Wrong input\n");
                }
            }
            else if (strcmp(location, "b")==0){
                printf("Is B dirty?\n");
                scanf("%s",state);
    
                if(strcmp(state,"yes")==0){
    
                    //Reset the counter value to zero if state is yes, same as Location A
                    count=0;
                    printf("Clean\n");
                    printf(" Go Right to A\n");
                    printf("Value of Counter %d \n",count); 
                }
                else if (strcmp(state,"no")==0) {
                    printf("Go Right to A\n");
    
                    //Counter increment if state is no, same as Location A
                    count++;
                    printf("Value of Counter %d \n",count);
                }
                else {
                    printf("Wrong input\n");
                }
            }
            else {
                printf("Location not found\n");
            }
        }
        return 0;
    }
    
    #包括
    #包括
    #包括
    int main(int argc,char*argv[]){
    字符位置[4];
    半焦态[4];
    整数计数=0;
    //如果机器人连续3次获得“否”状态,则结束循环。
    
    而(count您忘记了包含字符串.h头。 尝试添加:

    #包括
    

    在代码的顶部,您有两个问题:-

    • count
      未初始化。因此,当您在while循环中使用它时-它的值是多少,您正在与其他变量进行比较。您不知道-它是未指定的。您应该在循环外使用
      0
      对其进行初始化

    • char位置[1]
      也许你认为你需要一个单字符输入和一个
      char*
      来传递到
      strcmp
      ?很清楚一些事情-
      strcmp
      需要一个以null结尾的
      char
      数组。这里假设你得到
      y
      input
      中的一些输入,但是这里有
      \0
      吗?没有。你没有留下一个有足够的空间。 解决方案是将
      input
      数组变大。
      charinput[4]
      可能

    • &input
      input
      是完全不同的。对于数组类型,第一个是type
      char(*)[]
      (指向数组的指针),第二个是指向
      char
      scanf
      的指针。当您使用
      %s
      格式说明符时,scanf
    需要一个
    字符*
    。您没有提供一个

  • 使用这些
    scanf
    -s时添加错误检查。检查
    scanf
    的返回值,如果失败,则打印并退出

     if(scanf("%3s",input)!=1){
       fprintf(stderr,"Error in input\n");
       exit(1);
     }
    

  • count
    未初始化。
    char location[1];
    是无用的(在此上下文中)
    scanf(“%s”,&state);
    -->
    scanf(“%s”,state);
    谢谢您的评论。是的,count未初始化,我认为这没什么大不了的。我正在使用strcmp,所以location[1]这里是必需的。我会在有时间时提供答案。谢谢。非常感谢..您不能将
    “%s”
    与长度为1的数组一起使用。格式的意思是“跳过空白,然后将一个或多个非空白字符读入以null结尾的字符串”,并且长度为1的数组没有足够的空间容纳字符和空字节。它是1(小)问题。这远远不是代码的唯一问题。程序没有增加count变量,因为strcmp不工作,循环永远不会结束。我找不到任何其他问题。这不是问题@carlonghi。我已经做了更改。谢谢。谢谢。我已经做了建议的编辑。但问题仍然存在。@Tuhin答:你得到了什么错误?我能确保你的代码逻辑是防弹的吗?没有错误,如果你运行代码,在得到状态“否”后,你会得到计数器值总是1。计数器值应该在每次得到状态“否”时增加@图希娜:告诉我你给我的输入。它按我的预期工作。你检查正确了吗?输入位置,“a”或“b”输入状态,“是”或“否”3次“否”应该结束循环
     if(scanf("%3s",input)!=1){
       fprintf(stderr,"Error in input\n");
       exit(1);
     }