1

I am making a program to display matrices having 2 rows and 2 columns. The program works if I input 4 numbers, separated by space for both the matrices. Now if I input 5 numbers in each of the matrices, then I'm getting some weird output. For the first matrix the display is alright, but for the second matrix, whatever numbers I input for the first matrix, leaving the first four numbers, other numbers are getting inserted to second matrix.

#include <stdio.h>

int main(int argc, char *argv[])
{
    int matr1[2][2], matr2[2][2], i = 0, j = 0;

    printf("\nMATRIX-1: ");
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++)
            scanf("%d", &matr1[i][j]);
    }

    printf("MATRIX-2: ");
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++)
            scanf("%d", &matr2[i][j]);
    }

    printf("\nMATRIX-1");
    for (i = 0; i < 2; i++) {
        printf("\n");
        for (j = 0; j < 2; j++)
            printf("%d ", matr1[i][j]);
    }

    printf("\nMATRIX-2");
    for (i = 0; i < 2; i++) {
        printf("\n");
        for (j = 0; j < 2; j++)
            printf("%d ", matr2[i][j]);
    }
    printf("\n");

    return 0;
}

OUTPUT:

MATRIX-1: 1 2 3 4 5 
MATRIX-2: 1 2 3 4 5

MATRIX-1
1 2
3 4
MATRIX-2
5 1
2 3

Another thing I noticed is that if I press 'Enter' during the input, the cursor is going in the next line but the label 'MATRIX-1' remains where it is.

So is their any simple way to accept only certain number of items using scanf(), so that even if the user inputs more than that it won't going to insert that into next array, and display the label in the next line if the user inputs 'Enter'. (I know this can be done using do-while loops but I don't know how to use that within two for loops.)

P.S.- I am a beginner in C and may be these questions seem stupid but it would be very grateful for me if I get some help. Thanks in advance.

Tsubasa
  • 1,389
  • 11
  • 21
  • 4
    Read *lines* instead? Or once you read all input for the first matrix, read and *discard* all characters until newline? – Some programmer dude Feb 09 '18 at 09:32
  • Sorry but I don't know how to do that using scanf(). Moreover I think i am going to take characters instead of numbers. So then from there I've to convert it to integers. Actually this is just a formatted code. There is a matrix addition after that. But I haven't included it. I included only that part where I'm having the problem. – Tsubasa Feb 09 '18 at 09:34
  • Try to find a good book. It should tell you how to read lines. Reading and discarding until you get a newline is simple, just read one character at a time, check if it's a newline (or `EOF`) and break the loop then. Otherwise just don't do anything with the characters you read. – Some programmer dude Feb 09 '18 at 09:39
  • Well this seems useful. I think I gotta use getchar() for that but what about my second question regarding labels? – Tsubasa Feb 09 '18 at 09:42
  • That's really nothing you can do about. That's just how terminals works. Unless you want to handle all of the terminal control yourself? (Which I don't recommend for a beginner) – Some programmer dude Feb 09 '18 at 09:46
  • 1
    Read lines and [use `sscanf()` in a loop](https://stackoverflow.com/questions/3975236/how-to-use-sscanf-in-loops). – Jonathan Leffler Feb 09 '18 at 09:57
  • @Someprogrammerdude I wrote one piece of code where the label is being displayed if I input 'Enter'. But the thing is that whenever I input any characters and then press 'Enter' it's giving some weird results. Here's the code: char test[1000]; while (1) { printf("TEST: "); scanf("%c", test); } OUTPUT: TEST: (if i press 'Enter') TEST: ab (I input some chars and then press 'Enter') TEST: TEST: TEST: (I press 'Enter' again) TEST: So can you tell me what's happening? It's so weird, and JonathanLeffler what do you mean by 'in a loop'? Do I have to make a seperate loop? – Tsubasa Feb 09 '18 at 10:08
  • The above comment did not add the newlines and tabs lol. – Tsubasa Feb 09 '18 at 10:08
  • Use `fgets()` to read the _line_. Then uses `sscanf()`, `atol()`, etc to parse the _line_ into `int`s. – chux - Reinstate Monica Feb 09 '18 at 17:44
  • I don't know but can't I clear the stdin buffer before taking the input for matrix-2 ? If yes then that could be a very simple solution. – Tsubasa Feb 10 '18 at 08:37

1 Answers1

0

I think I discovered the way out of myself. The concept was to clear the stdin buffer somehow before reading input for the next array. This can only be the simplest solution I guess.

Here's the code:

#include <stdio.h>
#include <ctype.h>

void clear_stream(FILE *in)
{
    int ch;
    clearerr(in);
    do
        ch = getc(in);
    while (ch != '\n' && ch != EOF);
    clearerr(in);
}

int main(int argc, char *argv[])
{
    int matr1[2][2], matr2[2][2], i = 0, j = 0;

    printf("\nMATRIX-1: ");
    for (i = 0; i < 2; i++) for (j = 0; j < 2; j++) scanf("%d", &matr1[i][j]);
    clear_stream(stdin); // clears stdin

    printf("MATRIX-2: ");
    for (i = 0; i < 2; i++) for (j = 0; j < 2; j++) scanf("%d", &matr2[i][j]);
    clear_stream(stdin);

    printf("\nMATRIX-1");
    for (i = 0; i < 2; i++) {
        printf("\n");
        for (j = 0; j < 2; j++)
            printf("%d ", matr1[i][j]);
    }

    printf("\nMATRIX-2");
    for (i = 0; i < 2; i++) {
        printf("\n");
        for (j = 0; j < 2; j++)
            printf("%d ", matr2[i][j]);
    }
    printf("\n");

    return 0;
}

Output:

MATRIX-1: 1 2 3 4 5 6 7 8 9
MATRIX-2: 1 2 3 4 5 6 7 8 9

MATRIX-1
1 2
3 4
MATRIX-2
1 2
3 4

So my first problem is solved. I'm heading to solve my next problem regarding labels. I'll update the answer as soon as I find any solution.

Pang
  • 9,564
  • 146
  • 81
  • 122
Tsubasa
  • 1,389
  • 11
  • 21