I want to directly input a 2-D Array in C, separated just by single spaces and newlines. At the same time I also want to verify whether the user is entering a valid single digit integer (either positive or negative). I tried the following.
int A[3][3];
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&A[i][j]);
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d ",A[i][j]);
printf("\n");
}
But I want 2 things in this:-
- Input that is first verified whether its a single digit integer or not.
- Not works merely for positive integers and 0 but also for negative integers.
eg. input such as the following should be accepted and stored in 2-d array
1 2 -3 4
-5 1 2 -6
1 1 2 3
I'm sorry if I wasn't clear. An important component is that the input should be confirmed whether its integer or not i.e. the input should be either as char or string and if it is an integer (which can be ascertained by functions such as isdigit() , then it should be converted to an integer value. This following code chunk works for single positive values
char c = getchar();
if (isdigit(c))
int value = c - '0';
However, I don't know how to enable this functionality for negative integers in a complete 2D array input.