How to read a random amount of integers in C?

Asked

Viewed 967 times

2

I need to create a way to read integers in the same input, and the amount of integers entered is random every time the program is run.

I tried to perform the following algorithm:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    long int numero, i, vetor[100000];

    i = 0;
    while(scanf("%li", &numero) != EOF)
        vetor[i++] = numero;

    while(i > 0)
        printf("%li ", vetor[i--]);

    return 0;
}

However, on one machine, 1º while never ends. And on another machine, I get a Segmentation fault. What may be happening?

2 answers

3


Your problem and the use of scanf()

while(scanf("%li", &numero) != EOF)

The value that the scanf() return usually corresponds to the number of assignments performed (in your case this value is 1 because there is only one variable assignment numero).

Note that a scanf() with more than one assignment can return a lower number without being an error

chk = scanf("%d%d%d", &um, &dois, &tres);
if (chk == 3) /* 3 atribuicoes */;
if (chk == 2) /* 2 atribuicoes: `tres` nao foi atribuido */;
if (chk == 1) /* 1 atribuicao: apenas `um` foi atribuido */;
if (chk == 0) /* nenhuma atribuicao */;
if (chk == EOF) /* erro de leitura */;

In your example above you should not test the value returned by scanf() with EOF, but rather with 1

while(scanf("%li", &numero) == 1)
  • I forgot to explain that CTRL+Z generates EOF... Good answer +1.

1

I made some small changes to your code and tested using CTRL+Z to stop reading the integers, it worked perfectly:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    long int numero, i = 0, vetor[100000];

    while(scanf("%li", &numero) != EOF)
    {
        if (i == 99999)     //caso o vetor chegue ao seu limite a leitura encerra.
        {
            printf("Limite do vetor atingido!\n");
            break;
        } 
        else 
        {
            vetor[i++] = numero;
        }
    }

    while(i > 0)
        printf("%li ", vetor[--i]);

    return 0;
}

Do the test using this code, if you give any problem let me know in the comments that I will seek to resolve it. I hope to have helped.

  • Ok, it worked perfectly using Ctrl+Z. However, I wanted to understand why the first while doesn’t stop when it reaches the end of the input.

  • I don’t understand what you mean by end of input... Would it be at the end of 100,000 vector slots? For when you use Ctrl+Z it to...

  • 1

    In fact it has a potential problem there: supposing that 100000 readings are made, vetor[i++] will access beyond the limits of the allocated vector. Ideally you should also control that the value of i is less than the maximum in the loop.

  • From what I understand, the end of keyboard input is a #EOF, That’s why I thought the while would be closed when you press enter after typing the numbers in the console.

  • 2

    Um... I believe that if you type ENTER during reading, it will result in an empty string whose formatting with "%li" must return 0. It is different from CTRL+Z, which generates the EOF.

  • @Deyel It might be nice for you to edit your answer to include checking the vector boundaries in the first loop and to detail the CTRL+Z question. I know the answer already helped OP, but it doesn’t cost to improve for anyone else who needs it in the future. :)

  • Or simply stop the loop if the string is empty.

Show 2 more comments

Browser other questions tagged

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