2

I have a question i want to ask about the gets function.

I am currently writing a program of 2 matrix addition and I'm confused about the gets function behavior. From what i read from the description from tutorialspoint about the gets() function in c, it is said that " the gets function will returns str on success, and NULL on error or when end of file occurs while no characters have been read. "

for (matrix_number = 0; matrix_number < 2; matrix_number++)
{
    if (matrix_number == 0) { printf("MATRIX A\n"); }
    else { printf("MATRIX B\n"); }

    // dim = the dimension of the matrix that the user inputted
    // Loop counter is for cycling through the matrix row, if dim is 3 then cycle 3x
    // The user will type in the data in a format like this 
    //        Type in the data for row 1 : 1 2 3
    //        1 is the value for the first row and first column for the matrix
    //        2 is the value for the first row and second column for the matrix and so on

    for (loop_counter = 1; loop_counter <= dim; loop_counter++)
    {
        char row_value[20], space[2] = " ", *value_token;

        printf("Type in the data for row %d :",loop_counter);
        gets(row_value);

        value_token = strtok (row_value,space);

        while (value_token!=NULL)
        {
            insert( atoi(value_token) , matrix_number); 
            // convert value_token from string to int type
            value_token = strtok (NULL,space);
        }
    }
}

It compiles, but when i run the program, i got something like this

Welcome to the Matrix Addition Calculator Program!
Type in the dimension of the matrix : 3
MATRIX A
Type in the data for row 1 :Type in the data for row 2 :6 5 4
Type in the data for row 3 :3 2 1
MATRIX B
Type in the data for row 1 :1 2 3
Type in the data for row 2 :4 5 6
Type in the data for row 3 :7 8 9

I'm not sure what happen, for the Matrix A, the first row input is skipped, but for the Matrix B the first row input are not skipped.

I'm currently using gedit on elemtary luna to write the code and gcc to compile it. The elementary luna is running on virtual machine.

Thanks for everybody who have read my question, if the information is not enough, please let me know, hope someone could help me and Thanks a lot! :)

Chamoileon
  • 23
  • 5

1 Answers1

4

The scanf() you use to read the 3 in:

Type in the dimension of the matrix : 3

leaves the newline in the input buffer. The following call to gets() reads that newline. This is a standard problem.

Also, forget that gets() exists. It is no longer a part of standard C. It is lethal and cannot be used safely in a hostile environment. Assume that using it will crash your program — given the wrong input, that's what will happen, and there's nothing you can do to protect your code except avoid using gets(). That's why it is no longer a part of standard C.

Use fgets() or getline() instead. It won't matter here, but remember that these include the newline in the string that is read, unlike gets() which removes it.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Okay sir, i will try it again, my C programming is still very basic, s I don't know much about stdin and stdout. I will try and do some reading and see if the problem will then be solved. Thanks sir ! – Chamoileon Oct 03 '14 at 08:54
  • @Chamoileon; One thing you should note about `fgets` that it reads `\n` and place it in the destination array (forget what I said in my wrong answer). So, there is no need to consume `\n` when you are using `fgets` repeatedly until the length of the input string becomes equal or exceeds the destination array length. – haccks Oct 03 '14 at 09:10