How to identify a line break character in C?

Asked

Viewed 4,332 times

4

Problem: Sometimes I get an entry like

inserir a descrição da imagem aqui

(and the entrance continues)

other times as:

inserir a descrição da imagem aqui

(and the entrance continues)

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

My problem: I cannot define when to stop receiving integers. I tried to identify the ' n' character, 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 cycle?

  • The input can be 2 3 4 or just 2 if you enter, that’s it?

  • Yes, I can type 2 3enter, or 2enter.

  • Your error is almost the same: https://answall.com/q/262976/132 - Only yours is in C instead of Java.

2 answers

3


Your problem is almost the same as the one 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 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 from a line and process them as we read, it gets more complicated, but we can use the %n which tells 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 input:

1 2 3 4 5 6 7 8

That’s the way out:

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, the %d will put the read value in the address of y. This if any value can be read. If no value can be read, EOF is returned and the break; interrupts the while.

If any value can be read, the %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 share the not yet read part of the line.

  • the problem is that he wants an input from 2 43 5 such as, for example, the sscanf I was just gonna take out a number

  • @Fábiomorais Editado.

  • You don’t understand, he doesn’t know the amount he’ll put in, it may be 1,2,3,4,5...etc is undefined

  • @Fábiomoral Resolvido.

  • Yes, now you’ve settled the matter, good solution!

  • Can you explain the line int ret = sscanf(&linha[pos], "%d%n", &y, &p); ? I found this line quite interesting, but I’m not getting it.

  • I would also like you to explain how the line the friend above quoted works.

  • 1

    @Fábiomoral Edited with the explanation.

  • @Williamphilippe Edited with the explanation.

Show 4 more comments

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 that way because that little bit of code I think will be easy to see what you’re doing.

Code in the 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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.