0

I have an array of 15 strings (which don't all have to necessarily be used), and despite reading everywhere that gets should never be used, for some reason I believe it is most convenient for me for this program. After prompting the user to specify how many rows and columns he wants, to create a matrix, I ask him to enter the matrix values, one row per line at a time. I do this using gets. Simultaneously, I want to scan through the string for the amount of spaces entered to ensure that the user is entering the appropriate amount of numbers that correspond to the amount of columns specified.

At the end I want to print out the second row that I entered.

You can assume rowone and colone are already defined, I just didn't copy that part of the code to save space.

int i=0, rowone, colone, sbar=0, inputs=0;
char matrixone[15][10000];
 ......

printf("input your matrix\n");

for (i=0;i<rowone;i++){
    gets(matrixone[i]);

    while(matrixone[i][inputs]!='\n'){
        if (mplier[i][inputs] == ' '){
        sbar++;
        inputs++;
        }
        else
        inputs++;

    }
    if (sbar>=colone||sbar<colone-1){
            printf("Too many/too few inputs per line\n");
            main();
        }

  sbar = 0;
  inputs = 0;

  }
  puts(matrixone[2])

I get warnings when compiling and ultimately not even the chance to input the matrix as "Too many/too few inputs" always pops up.

Any help would be greatly appreciated, thank you!

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
stellarhawk 34
  • 133
  • 1
  • 3
  • 11

1 Answers1

0

Infinite loop.

Or more properly, eventually matrixone[i][inputs] accesses way beyond what it was read and maybe beyond the array itself. This leads to undefined behavior (UB).

gets() consumes, but does not save '\n'.

gets(matrixone[i]);
while(matrixone[i][inputs]!='\n'){  // why should this quit?
  ...
  inputs++;
}

Instead, drop gets() as it is dropped from the language for 5 years now, use fgets(), check return value and look for the end of the string as well

if (fgets(matrixone[i], sizeof matrixone[i], stdin) {
  while(matrixone[i][inputs] != '\0'){
    ...
  }
}

Suggestion for OP (guessing OP's goal)

char *matrixone[15] = { 0 };
printf("input your matrix\n");

for (i=0;i<min(rowone,15);i++){
  buf[1000];
  if (fgets( buf, sizeof buf, stdin) == NULL) break;
  buf[strcspn(buf, "\n")] = 0;

  // qualify buf concerning ' ' , sbar, etc
  ...

  matrixone[i] = strdup(buf);
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • I want it to stop when it encounters a newline, so that one line of input is read into 1 row. Then the for loop will ensure the process starts again for the second row of the array. I am trying to create a matrix simply where each line of input corresponds to one row. Is this the false approach to it? – stellarhawk 34 Feb 27 '16 at 21:14
  • To lop off a potential `\n` from `fgets()` input, see http://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/27729970#27729970 – chux - Reinstate Monica Feb 27 '16 at 21:16
  • @stellarhawk 34 "I want it to stop when it encounters a newline" --> What is "it", the entire program, the `for()` loop, the `while` loop, something else? – chux - Reinstate Monica Feb 27 '16 at 21:18
  • I mean that once a newline is encounterd, I should stop reading input into the 1st string of the array, and move on to the second. And i want a maximum of 15 strings/rows. – stellarhawk 34 Feb 27 '16 at 21:25
  • The code you posted in your suggestion, can i ask what it does exactly, because im not very familiar with the syntax used. – stellarhawk 34 Feb 27 '16 at 22:08
  • Actually, with my method i still havent understood my mistake. Why is it an infinite loop? Because as soon as the user hits enter for a new line, wont it move to matrixone[2][inputs] where inputs starts off from 0 again? – stellarhawk 34 Feb 27 '16 at 23:43
  • @stellarhawk 34 " i still havent understood my mistake." --> In your code, what stops the `while(matrixone[i][inputs]!='\n')` loop? (Answer: the occurrence of a `'\n'`). Does `gets(matrixone[i])` _ever_ put a `'\n'` in `matrixone[i]`? (Answer: no). – chux - Reinstate Monica Feb 28 '16 at 06:57
  • So fgets can allow me to scan for that \n and then move onto reading input to my second string in the array? – stellarhawk 34 Feb 28 '16 at 12:27
  • @stellarhawk 34 "So fgets can allow me to scan for that \n ..." --> Yes. – chux - Reinstate Monica Feb 28 '16 at 16:58