c - scanf reading more than required -
i coding program in c requires user enter 3 inputs. using scanf numbers:
printf("enter first value: "); scanf("%lf", &eps); while(eps < 0){ printf("epsilon must positive. choose new value: "); scanf("%lf", &eps); } printf("enter second value: "); scanf("%lf ", &a); while(a < 0){ printf("a must positive. enter new value: "); scanf("%lf", &a); } if (a != 0){ printf("enter third value: "); scanf("%lf", &w); while(w < 0){ printf("w must positive. enter new value: "); scanf("%lf", &w); } }
the problem is, when second input required, put value , scanf ignore , "asks new number" (i mean, if enter 1, need enter 1 again value read). haven't been using c while, remember had problem before , used getch() or getchar() after scanf problem didn't happen. doing wrong?
thanks in advance!
there unfortunate space character in scanf
format here:
scanf("%lf ", &a);
that cause scanf swallow whitespace following number, including newline character , whitespace in next line (which means insist on reading line.)
remove space , should work ok.
Comments
Post a Comment