I am new trying to learn C programming, I code a little program that read only the integers from an input with both integers and characters but I want to go a little further: I need to add a \n char only the first time a character is read from the input (in this case the standard input but it could be a file). This is the code I can get:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define M 1000
int main(void)
{
    char a;
    char enteros[M];
    int output[M];
    int count = 0;
    int i;
    int x = 0;
    int index = 0;
    int num = 0;
    char *str;
    while (fscanf(stdin, "%c", &a) != EOF)
    {
        enteros[count] = a;
        count++;
    }
    enteros[count] = '\0';
    str = enteros;
    while (*str)
    {
        if (sscanf(str, "%d%n", &num, &x) == 1)
        {
            output[index] = num;
            index++;
        }
        str += x;
        for (; *str; str++)
        {
            if (*str >= '0' && *str <= '9') /* positive value */
                break;
            if (*str == '-' && *(str + 1) >= '0' && *(str + 1) <= '9') /* negative */
                break;
        }
    }
    for (i = 0; i < index; i++)
    {
        printf(" %d", output[i]);
    }
    return 0;
}
When the input is:
1 8 4000 2 -23 end 51 87 end –4
–3 2 end
The output should be:
1 8 4000 2 -23 
51 87 
-4 -3 2
But instead I get:
1 8 4000 2 -23 51 87 4 3 2
Thanks for the help!
 
    