c - Using while(getchar()!='\n') I empty the stdin, but I have to push on Enter key -
i'm using while(getchar()!='\n') empty stdin, if want go on, need push on enter key , computation continue... why? i'll post part of code:
while(1){ if(fgets(buffer,max_dimension,stdin)==null){ perror("error"); exit(1);} }else{ printf("not correct term\n"); while(getchar()!='\n'); sleep(1); } }
thanks!
@iharob if set max_dimension=1240 , send input: string > 1024 remains on stdin, have use while(getchar()!='\n')
2 issues:
1: code has }
.
// here if(fgets(buffer,max_dimension,stdin)==null){ perror("error"); exit(1);} }else{ // or @ beginning
2: assuming above not issue, @unxnut answered, typically fgets()
contains '\n'
, there no need empty stdin
.
yet when line excessively long , '\n'
not read fgets()
, makes sense read until end found.
if (fgets(buffer, max_dimension, stdin) == null) { perror("error"); exit(1); } if (strchr(buffer, '\n') == null) { int ch; printf("not correct term\n"); while (((ch = getchar()) != '\n') && (ch != eof)) ; // while(getchar()!='\n'); sleep(1); }
Comments
Post a Comment