I am trying to input some integers into a 2 dimensional array. I am using a for loop in a for loop for iterate through the array. One problem i am facing is that lets say the 2d array is v[3][3], when the user enters 4 digits in one line for when y = 0, i want to give out an error. I've tried using counters to see if the user has inputted more than the desired amount, but instead i just goes to the second line of array and inputs the fourth integer into v[0][1]. Thank you.
This is what i've tried, the logic of "if(x==i)" is wrong, and i'm not sure how to fix this.
int main(void) {
// TODO
int x = -1;
int y = -1;
scanf("%d %d", &x, &y);
if (x <= 0 || y <= 0) {
printf("Cannot decode\n");
return 1;
}
int v[x][y];
for (int t=0; t < y; t++) {
for(int i=0; i < x; i++) {
// printf("i = %d\nt = %d\n", i, t);
if (x == i) {
printf("Cannot decode\n");
return 1;
}
scanf("%d", &v[i][t]);
// printf("%d-%d\n", i, t);
}
}
}
When i try to use fgets, i keep getting the errors:
nonogram.c:39:11: error: incompatible integer to pointer conversion
passing 'char' to parameter of type 'char *'; take the address with &
[-Werror,-Wint-conversion]
fgets(temp, x, stdin);
^~~~
&
/usr/include/stdio.h:564:38: note: passing argument to parameter '__s'
here
extern char *fgets (char *__restrict __s, int __n, FILE
*__restrict__stream)
^
1 error generated.
This is what i did
for (int t=0; t < y; t++) {
for(int i=0; i < x; i++) {
fgets(temp, x, stdin);
v[i][t] = atoi(temp);
}
}