The purpose of the code below is
- Scan the Location of the bot
- Scan the state of the location, whether it is dirty or clean
- 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>
int main(int argc, char *argv[]) {
char location[1];
char state[4];
int count;
//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;
}
Aucun commentaire:
Enregistrer un commentaire