Tuhin A.
Tuhin A.

Reputation: 616

counter value is incrementing just once in if else condition inside while loop

The purpose of the code below is

  1. Scan the Location of the bot
  2. Scan the state of the location, whether it is dirty or clean
  3. Do this continuously until the bot get 3 times "no" state continuously

The issue is: counter value increment just once, counter = 1, and then never increment. That's why the loop never ends.

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

Upvotes: 0

Views: 1151

Answers (2)

Carlo Longhi
Carlo Longhi

Reputation: 54

You forgot to include the string.h header. Try add:

#include <string.h>

at the top of the code

Upvotes: 0

user2736738
user2736738

Reputation: 30926

You have couple of problems:-

  • count is uninitialized. So when you use it in the while loop - what value it has to which you are comparing the other variable. You don't know - it is unspecified. You should initialize it with 0 outside the loop.

  • char location[1] Maybe you thought you need a single character input and a char* to pass to the strcmp? Well clear few things - strcmp expects a null terminated char array. Here suppose you get y or some input in input but is there any \0 here? No. You didn't leave any space for that. Solution is to make input array larger. char input[4] maybe.

  • &input and input is quite different. For array type first has type char(*)[] (pointer pointing to an array) and second is a pointer to char. scanf expects a char* when you use %s format specifier. You didn't provide one.

  • Add error check when using those scanf-s. Check the return value of scanf and if it fails print it and exit.

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

Upvotes: 1

Related Questions