How to identify a line break character in C?

4

Problem: Sometimes I get an entry like

(andtheinputcontinues)

othertimesas:

(andtheinputcontinues)

Thatis,Icanreceiveaninteger,ortwo,orthree,andthenlaterreceiveastringtodoanything.

Myproblem:Icannotsetwhentostopreceivingtheintegers.Itriedtoidentifythecharacter'\n',butitisnotworking.

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 the integers and exit the while loop?

    
asked by anonymous 24.08.2018 / 23:12

2 answers

3

Your issue is almost identical to the one 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 so:

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 read line:

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

To read multiple numbers in a row and process them as we read, it gets trickier, but we can use %n that says how many characters were 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 y address. That if some value can be read. If no value can be read, EOF is returned and break; interrupts while .

If any value can be read, %n specifies that the amount of characters read is placed at the p address. That adds up to the value of pos . This way, &linha[pos] always corresponds to the unread part of the line.

    
24.08.2018 / 23:43
1
int number;
char nl = 0;

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

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

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

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

No Ideone Code

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;
}
    
24.08.2018 / 23:39