How to limit the number of integers inputted by the user ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • linda7
    New Member
    • Apr 2014
    • 16

    How to limit the number of integers inputted by the user ?

    I'm writing a pretty simple program, but I faced a problem in limiting user input data. For example, if I have a 2D-array of a certain size and I asked the user to input the array column by column but the problem is when I run the program it mix the numbers together.
    what I actually need is to delete the rest of numbers which are not part of this column so if the size=3 and the user input 1 2 3 4 5 I need the program to skip 4 & 5 and ask the user again to input the next column with different numbers so 4 & 5 will not be printed in the next column (they should be skipped). I have an idea of splitting the loops I mean Inputting the first column in a loop, then another loop for the second column, and so on, but imagine that the size is 100 it will be really long.

    This is the program I'm getting a strange output !.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define size 3
    int main(){
    	
    	
    int a[size][size];
    
    int i,j=0;
    	
    
    	//printf(" input  column  #%d \n",j+1);
           printf(" input  the first column \n"); 
         for(i=0;i<size;i++){
            	scanf("%3d", &a[i][0]);
       if(i==size)
      scanf ("%*[^\n]"); (void) getchar ();
            }
           j++;
             printf(" input  the next column \n"); 
    
               for(i=0;i<size;i++){
            	scanf("%3d", &a[i][1]);
       if(i==size)
     scanf ("%*[^\n]"); (void) getchar ();
            }
         
         	printf("Matrix A is\n");
        for(i=0;i<size;i++)
        {
           for(j=0;j<size;j++) 
            printf("%d\t",a[i][j]);
            printf("\n");
        }
    
         return 0;
    }
Working...