Advertisement
cd62131

SafeScanf

Feb 8th, 2014
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define N 3
  4. int main(void) {
  5.   int test[N], i;
  6.   char buf[BUFSIZ];
  7.   for (i = 0; i < N; i++) {
  8.     puts("テストの点数を入力せよ");
  9.     for (; scanf("%d", &test[i]) == 0;) {
  10.       fgets(buf, BUFSIZ - 1, stdin);
  11.       puts("error.");
  12.       puts("テストの点数を入力せよ");
  13.     }
  14.     if (test[i] < 0 || test[i] > 100) {
  15.       puts("0から100までの整数を入力してください。");
  16.       i--;
  17.       continue;
  18.     }
  19.   }
  20.   for (i = 0; i < N; i++)
  21.     printf("%d人目の点数は %d です。\n", i + 1, test[i]);
  22.   return 0;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement