How to identify a line break character in C?

Problem: sometimes I get an input like

insert the description of the image here

(and the input continues)

Other times like:

insert the description of the image here

(and the input continues)

That is, I can receive an integer, or two, or three, and then then receive a string to do anything.

My problem: I can't define when to stop receiving integers. I tried identifying the character '\n', but it is not working.

            while(lixo != '\n'){
                scanf("%d", &y);
                scanf("%c", &lixo);
                for(i = 0; i < m; i++){
                    if(!i && matriz[y][0]) break;
                    matriz[y][i] = 1;
                    inseticida += 2;
                }
            }

How can I identify this line break to stop receiving integers and exit the while loop?

Author: lineOut, 2018-08-24

2 answers

Your problem is almost the same as what was outlined in this other question. The only difference is that yours is in C, Not Java.

The solution approach is the same. You are reading lines containing numbers. This is different from simply reading numbers. Although they are similar things, there is some difference exactly because of the line breaks. So, to read a line containing a number, do this:

char linha[11];
fgets(linha, 10, stdin); // Lê a linha inteira.
sscanf(linha, "%d", &y); // Retira o número da linha lida.

To remove three numbers from the line lida:

sscanf(linha, "%d %d %d", &a, &b, &c); // Retira três números da linha lida.

To read several numbers of a line and process them as we read, it gets more complicated, but we can use %n that tells how many characters have been read. For example:

#include <stdio.h>

int main(void) {
    char linha[10001];
    fgets(linha, 10000, stdin); // Lê a linha inteira.
    int pos = 0;
    while (1) {
        int y;
        int p;
        int ret = sscanf(&linha[pos], "%d%n", &y, &p); // Retira um número da linha lida.
        pos += p;
        if (ret == EOF) break;
        printf("%d ", y);
    }
    printf("fim");
}

With this entry:

1 2 3 4 5 6 7 8

The output is this:

1 2 3 4 5 6 7 8 fim

See here working on ideone.

These four lines are the heart of the business:

        int p;
        int ret = sscanf(&linha[pos], "%d%n", &y, &p); // Retira um número da linha lida.
        pos += p;
        if (ret == EOF) break;

First, %d will put the read value at the address of y. This if some value can be read. If no value can be read, EOF is returned and break; stops while.

If any value can be read, %n specifies that the amount of characters read is placed at the address of p. This is then added to the value of pos. In this way, &linha[pos] always matches the unread part of the line.

 3
Author: Victor Stafusa, 2018-08-24 22:24:37
int number;
char nl = 0;

while ( scanf("%d%c", &number, &nl) != EOF)
{
    printf("%d \n", number);//debug

    if (nl == '\n')
        break;
}

Is an example that I think will work, I put a print to give a "debug" and see what happened, I tested here and it worked, give me some feedback.

I put it that way because this snippet of code I think will be easy to see what you're doing.

Code No Ideone

char lixo = 0;

while(scanf("%d%c", &y, &lixo) != EOF)
{

    for(i = 0; i < m; i++)
    {
        if(!i && matriz[y][0]) break;
        matriz[y][i] = 1;
        inseticida += 2;
    }
    if (lixo == '\n')
        break;
}
 1
Author: Fábio Morais, 2018-08-24 21:49:10