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! :)