OsmaK
OsmaK

Reputation: 37

Attempting to remove repeated 0 on c program

I have a C program that you introduce a sequency of numbers until you type -1, It works fine with debian compiling with gcc ("$gcc act.c -o act") but now I am on windows 10 with devc++ and TDM-GCC 4.9.2 and the output fails

if you introduce "1 5 6 7 8 0 0 0 0 5 5 6 6 6 6 8 4 1 0 0 0 2 5 0 5 6 8" the output must be "1 5 6 7 8 0 5 5 6 6 6 6 8 4 1 0 2 5 0 5 6 8"

As I said on linux works, but on windows the output is only the first number in this case 1.

Whats wrong in my code?

code:

#include <stdio.h>
int main(){
    int secuencia_numeros;
        int aux_secuencia_numeros = -1; //Utilizamos el -1 como numero auxiliar
        printf("\n\n\t Quitar ceros consecutivos.\n\n");
        printf("Introduce una secuencia de numeros [fin = -1]: ");
        scanf("%d",&secuencia_numeros);
        while(secuencia_numeros != -1) {
                fflush( stdin );
                if((secuencia_numeros ==0)&&(aux_secuencia_numeros == 0)) {
                }else{
                        printf("%d ",secuencia_numeros);
                }
                aux_secuencia_numeros = secuencia_numeros;
                scanf("%d",&secuencia_numeros);
        }
        printf("\n");
        return 0;
}

Upvotes: 0

Views: 43

Answers (1)

user2736738
user2736738

Reputation: 30926

fflush(stdin) has undefined behavior. That's why avoid using it.

In some implementations, flushing a stream open for reading causes its input buffer to be cleared (but this is not portable expected behavior). 1

1. Link

Upvotes: 2

Related Questions