Kaze79

Input_function

Dec 8th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.30 KB | None | 0 0
  1. int input( int array[500][500], int *x0, int *y0 ) {
  2.     int x = 0;
  3.     int y = 0;
  4.     int err = 0;
  5.     char c;
  6.     printf("Size:\n");
  7.     if ( scanf("%d %d%c", x0, y0, &c)!=3 || *x0<=0 || *y0<=0 || *x0>500 || *y0>500 || c!='\n' ) {
  8.         printf("Invalid input.\n");
  9.         return 0;
  10.     }
  11.     while ( y < *y0 ) {
  12.         x = 0;
  13.         while ( x < *x0 ) {
  14.             c = fgetc(stdin);
  15.             if ( (x== *x0 - 1) && (c!='\n') ) { //after enough inputs, newline must follow
  16.                 err = 1;
  17.                 break;
  18.             }
  19.             switch(c) { // only these three chars are accepted
  20.             case 'o':
  21.                 array[y][x] = 1;
  22.                 break;
  23.             case '.':
  24.                 array[y][x] = 0;
  25.                 break;
  26.             case '!':
  27.                 array[y][x] = 2;
  28.                 break;
  29.             case '\n': //if we have entered less chars than size
  30.                 if (x < *x0 - 1)
  31.                     err = 1;
  32.                 break;
  33.             default:
  34.                 err = 1;
  35.                 break;
  36.             }
  37.             x++;
  38.         }
  39.         if (x > *x0)
  40.             err=1;
  41.         y++;
  42.     }
  43.     if (y!=*y0)
  44.         err=1;
  45.     if (err==1) {
  46.         printf("Invalid input.\n");
  47.         return 0;
  48.     }
  49.  
  50.     return 1;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment