I need to read a matrix of variable order from stdin. The first line of the input is the order (N) of the matrix and the following N lines all should have N space separated integers. However, lines are allowed to have comments after all needed input for that line was sent.
As an example, this is 100% valid input:
3 Matrix Order
1 2 3 Line 0
4 5 6 Line 1
7 8 9 Line 2
/empty line
I've managed to do that using the following code:
scanf("%d", &matrixOrder);
scanf("%*[^\n]");
for (i = 0; i < matrixOrder; i++) {
for (j = 0; j < matrixOrder; j++) {
scanf("%d", &matrix[i][j]);
}
scanf("%*[^\n]");
}
However, I must also be able to know when the line does not provide all the needed input and output a warning message. For instance, the following example should output a warning:
3
1 1 1
1 1 1
1 1
/empty line
My current code simply ignores the missing entry.
The only way I could think of possibly doing this would be to read a single character at a time and, once reaching a new line character, checking if I had already received all the input I needed for that line. That, however would require me to read the integers, which may have multiple digits and even negative values, as characters and do the conversion myself. That seems to be far from a ideal solution to me.
Is there any better, intelligent way of doing this? I ask mainly because I don't come from a C background and am not really used to it yet.