/* * program to illustrate a problemj with using scanf * and how to fix it * set BAD to get the version with the problem * otherwise you get the fixes version * * Matt Bishop, ECS 36A * April 9, 2024 original version */ #include int main(void) { int i; /* variable to hold input */ /* initialzize i -- needed as if the user enters */ /* bad input, we want to come back and ask again */ i = 1; /* * loop until the user types a 0 or negative number */ do { #ifdef BAD /* may cause an infinite loop */ scanf("%d", &i); #else /* if given input that (in BAD) causes an */ /* infinite loop, give error and quit */ if (scanf("%d", &i) != 1){ fprintf(stderr, "You blew it -- integers only\n"); return(1); } #endif /* print what the uer entered */ printf("i = %d\n", i); /* and loop until they enter a non-positive number */ } while(i > 0); /* th-th-that's all, folks! */ return(0); }