c - Why this getchar() doesn't work properly? -


this question has answer here:

can tell me why line o = getchar(); works first time , once works, once it's not?

#include <stdio.h>  int main(void) {     char o;      (int = 1; > 0; i++)     {         printf("%d\n", i);          if (i % 10 == 0)         {             printf("do want continue? (y/n): ");             o = getchar();             if (o == 'n')                 break;         }     }      return 0; } 

first of all, getchar() returns int, may not fit char. change

char o; 

to

int o = 0; 

then, come point of skipping, well, not skip. when press key , press enter, newline press of enter key serves feed getchar() in every second iteration of loop.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -