1

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");
    }
}
Mitisme
  • 33
  • 5
  • 4
    It's because `scanf("%c", &c);` leaves a newline (`\n`) in the input stream and that lets the while loop run once more. Changing it to `scanf(" %c", &c);` (notice the space in the format string) and that'll consume the leftover newline. – P.P Feb 10 '22 at 05:37

0 Answers0