here current code:
int num = 0; char c = '#'; scanf("%d",&num); do{ (int i=0;i<num;i++){ printf("%c",c); } printf("\n"); } while (scanf("%d", &num) == 1);
how have if user doesn't enter anything, program won't spit out newline?
any appreciated, thank you!
this code should work want :
#include <stdio.h> int main() { int num = 0; char c = '#'; char readline[50]; while ((fgets(readline, sizeof readline, stdin) != null) && sscanf(readline, "%d", &num) == 1) { (int i=0;i<num;i++){ printf("%c",c); } printf("\n"); fflush(stdout); } return 0; }
the behaviour of code following : fgets read enter in standard stream (stdin), , put in readline array. program try read number in readline variable , put in num variable sscanf function. if number read, program execute behaviour did present in question (writing # character "num" times), , go beginning of loop. if else number has been read, loop stopped.
Comments
Post a Comment