0

I am trying to take a numbers from a user and put it into an array without them having to press enter.

i.e the output should look like this

Enter number of rows: 2
Enter number of columns: 3

Enter elements of matrix A:
Enter elements of row1: 2 3 4
Enter elements of row2: 5 2 3

Enter elements of matrix B:
Enter elements of row1: -4 5 3
Enter elements of row2: 5 6 3

Sum of A and B is:
-2 8 7
10 8 6

Currently, my code looks like this,

int main()
{
    char *str[100];
    int rows, cols;
    printf("Enter number of rows: ");
    scanf(" %d", &rows);
    printf("Enter number of columns: ");
    scanf(" %d", &cols);

    for(int i = 0; i<rows; i++)
    {
        printf("Enter elements in row%d: ", i+1);
        gets(str[i]);
    }
}

Any help would be appreciated, I am still quite new to coding. Thanks!

Chillah
  • 1
  • 3
  • You have to allocate and assign a valid buffer to `str[i]` before doing `gets(str[i]);`. Also note that `gets()` shouldn't be used because it has unavoidable risk of buffer overrun. `gets()` is deprecated in C99 and removed from C11. – MikeCAT Apr 25 '21 at 23:14
  • Does this answer your question? [Capture characters from standard input without waiting for enter to be pressed](https://stackoverflow.com/questions/421860/capture-characters-from-standard-input-without-waiting-for-enter-to-be-pressed) – kaylum Apr 25 '21 at 23:15
  • If that doesn't answer your question then please clarify what problem or error you are actually asking about. Because you haven't actually asked any question or explained what difficulty you have. – kaylum Apr 25 '21 at 23:16

0 Answers0