Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my C program asking for one more input than it should? And the extra input doesn't even do anything

Tags:

c

matrix

The the code for that portion, if you want to compile it is below.

Basically I have to take the following data and input it into the program:

4 5
12 5 7 0 -3
9 11 2 5 4
0 -5 9 6 1
2 12 93 -15 0
5 3
7 1 31
0 0 5
-5 -3 2
9 41 11
0 13 31

The first 4 5 & 5 3 represent the dimensions of the first and second matrix, while the data after it is the data for the matrix.

The problem is, when I copy and paste that in, it asks for one more input afterward, and when I input anything (say 84) it works flawlessly (output wise), and the 84 appears to do nothing. Why's it asking for this extra one?

#include <stdio.h>

int main(int argc, char *argv[]) {
    int rows1 = 1, columns1 = 1, rows2 = 1, columns2 = 1;    // variables for number of rows and columns in each matrix 
        int i, j, k;                                             // loop variables 

        // These will affect the loop's length
        scanf("%d %d", &rows1, &columns1);
        int matrix1[rows1][columns1];
        for (i = 0; i < rows1; i++) {
            for (j = 0; j < columns1; j++) {
                scanf("%d ", &matrix1[i][j]);
            }
        }


        scanf("%d %d", &rows2, &columns2);
        int matrix2[rows2][columns2];
        for (i = 0; i < rows2; i++) {
            for (j = 0; j < columns2; j++) {
                scanf("%d ", &matrix2[i][j]);
            }
        }
}
like image 227
Doug Smith Avatar asked Dec 30 '25 20:12

Doug Smith


2 Answers

Your last scanf requires 2 things

                scanf("%d ", &matrix[i][j]);
                //     112

Thing 1 is an integer
Thing 2 is zero or more of whitespace

If you provide "31" (or "31 " or "31\n") to the scanf it will be expecting more whitespace. Once you provide "84", it know the whitespace has ended and carries on with the program.


Suggestion: remove all whitespace from the scanf conversion specifiers: scanf("%d%d") is good! The "%d" conversion specifier already removes whitespace before the number.

Better yet, read data with fgets() and parse with sscanf()

like image 67
pmg Avatar answered Jan 01 '26 10:01

pmg


The behaviour is due to the spaces in:

scanf("%d ", &matrix1[i][j]);
scanf("%d ", &matrix2[i][j]);

Change the format specifiers to:

scanf("%d", &matrix1[i][j]);
scanf("%d", &matrix2[i][j]);

(Well, it's only the second one that matters, but you might as well keep them identical.)

A space in the format specifier matches one or more whitespaces in the input. When your code is run, the final scanf() sits there reading from stdin until it encounters a non-whitespace character (the 8 in 84), at which point it returns and your program ends.

like image 25
NPE Avatar answered Jan 01 '26 10:01

NPE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!