C: When using this series of if, else if, and else statements, why does the else always perform

Viewed 27

I'm currently working on a ASCII game and to move maps I want to use the n s e w keys, but whenever I move to another map, the else statement always trigger in the console. maybe im blind but some help would be appreciated.

bool running = true; 
while(running) {
    char c;
    printf("Use n s e w to move in those directions");
    scanf("%c", &c);

    printf("\n");

    if(c == 'n' && y < 399) {
      y++;
       struct zone z = mapGen();
       struct zone *zone1 = &z;
       map[y][x] = zone1;
       printMap(map[y][x]);
    }
    
    else if(c == 's' && y > 0) {
      y--;
       struct zone z = mapGen();
       struct zone *zone1 = &z;
       map[y][x] = zone1;
       printMap(map[y][x]);
    }
    
    else if(c == 'e' && x < 399) {
      x++;
       struct zone z = mapGen();
       struct zone *zone1 = &z;
       map[y][x] = zone1;
       printMap(map[y][x]);
    }
    
    else if(c == 'w') {
      x--;
       struct zone z = mapGen();
       struct zone *zone1 = &z;
       map[y][x] = zone1;
       printMap(map[y][x]);
    }
    else if(c == 'x') {
      break;
    }
    else {
      printf("end of map or key input error, Use n s e w to move, X to stop THIS LINE \n");
    }
}
0 Answers
Related