I am trying to save some ints into an array that are read from the user input . I don't know the number of ints on each line, only the number of lines which will also be the maximum number of ints on that line. For example if this is 5 then the user should input 5 lines with ints and each line a maximum number of 5 elements on it. The values will be positive. What am I doing wrong?
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int n;
scanf("%d",&n);
int array_row[n];
int i=0;
int noEnter=n;
//My idea is when in getchar() there is a enter it means that the user wants to go to the next line so decrement noEnter with 1 and also store a value -1 which tells me that that was the end of the line
while(noEnter!=0){
int c;
if(scanf("%d",&c)==1){
array_row[i]=c;
i++;
continue;
}
char d=getchar();
if(d=='\n'){
array_row[i]=-1;
i++;
noEnter--;
continue;
}
}
for(int i=0;i<n*n;i++){
printf("%d ",array_row[i]);
}
return 0;
}
Example of input:
5
4
4 35 65
4 32
2 222 4 5 6
4
Output:
4 -1 4 35 65 -1 4 32 -1 2 222 4 5 6 -1 4 -1