Simplify condition in do-while
[eleutheria.git] / unix / homework / scanfprob.c
blob3e831a78d6bd08e91c49e11a8c9edd7d04014da9
1 /*
2 * Compile with:
3 * gcc scanfprob.c -o scanfprob -Wall -W -Wextra -ansi -pedantic
4 */
6 #include <stdio.h>
7 #include <stdlib.h>
9 int main(void)
11 int a, rv;
14 * When scanf is attempting to convert numbers, any non-numeric characters
15 * it encounters terminate the conversion and are left in the input stream.
16 * Therefore, unexpected non-numeric input ``jams'' scanf again and again.
17 * E.g.
19 * do {
20 * printf("Input: ");
21 * scanf("%d", &a);
22 * } while (a != -1);
24 * See also: http://c-faq.com/stdio/scanfjam.html
27 /* The correct way to do it */
28 do {
29 printf("Input: ");
30 rv = scanf("%d", &a);
31 if (rv == 0)
32 scanf("%[^\n]");
33 } while (a != -1);
35 return EXIT_SUCCESS;