7

Why fflush(..) doesn't work to c2 and c0?
If I use the declaration c0 = 0 and c2 = 0 it works, but fflush(stdin) doesn't work, I tried to put in different places but it did not work, im using code blocks in ubuntu 13.04;

int main(void)
{
    int cod ,passou = 0, c0, c1, c2, c3, ct;
    float p1, p2, p3;
    char o;

    do {
    puts  ("Informe codigo: ");
    scanf ("%i", &cod);
    fflush (stdin);
        switch (cod)
        {
            case 0:
                c0 = c0 + 1;
                break;

            case 1:
                c1 = c1 + 1;
                ct = ct + 1;
                break;

            case 2:
                c2 = c2 + 1;
                ct = ct + 1;
                break;

            case 3:
                c3 = c3 + 1;
                ct = ct + 1;
                break;

            default:
                puts ("Valor invalido");

        }
        getchar();
        puts ("Deseja informar mais um voto?");
        fflush (stdin);
        scanf("%c",&o);
        if (o == 'S' || o == 's' ) {
        passou = 0;
        } else if (o == 'N' || o == 'n' ) {
        passou = 1;
        } else {
        puts ("Opcao invalida");
        }
        } while ( passou != 1 );


        p1=(c1/ct)*100;
        p2=(c2/ct)*100;
        p3=(c3/ct)*100;
        if (c1 > c2 && c1 > c3 && c1 > c0 ) {
        puts ("Candidato numero 1 eh o vencedor");
        } else if (c2 > c1 && c2 > c3 && c3 > c0) {
        puts ("Candidato numero 2 eh o vencedor");
        } else if (c3 > c1 && c3 > c2 && c3 > c0) {
        puts ("Candidato numero 3 eh o vencedor");
        } else {
        puts ("Numero de votos em branco eh maior do que todos os outros candidatos");
        }
        printf ("\nTotal de votos do candidato 1: %d", c1);
        printf ("\nTotal de votos do candidato 2: %d", c2);
        printf ("\nTotal de votos do candidato 3: %d", c3);
        printf ("\nTotal de votos em branco: %d", c0);
        printf ("\nPercentual de votos do candidato 1: %.2f", p1);
        printf ("\nPercentual de votos do candidato 2: %.2f", p2);
        printf ("\nPercentual de votos do candidato 3: %.2f", p3);

        return 1;
    }
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • 3
    `fflush` is for flushing _output_. `fflush(stdin)` is meaningless. – Barmar May 25 '13 at 18:39
  • What do you mean by "fflush doesn't work to c2 and c0"? That makes no sense. – Barmar May 25 '13 at 18:42
  • 1
    Imagine the stream (*stdin*) is like the flow of water from a tap. What does `fflush(stdin);` mean? Something like "open the tap and let the water flow out until there is no more water"? – pmg May 25 '13 at 18:42
  • Thanks! i put c0=0 and c2=0, works.. –  May 25 '13 at 18:43

2 Answers2

5

On your system ubuntu 13.04 (Unix or Linux) calling fflush (stdin); is undefined behavior!

int fflush(FILE *ostream);

ostream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined

To learn a trick to flush the input buffer correctly, you can use some of the following code snippets that actually read and discard unwanted chars from input buffer. You can use this as fflush before reading actual data. read this FAQ entry.

for C:

 while ((ch = getchar()) != '\n' && ch != EOF);  

for C++:

 while ((ch = cin.get()) != '\n' && ch != EOF);

However, if you call these when there is no data in the input stream, the program will wait until there is, which gives you undesirable results.

Read: @Keith Thompson's answer: "Alternative to C library-function fflush(stdin)"

Edit:
There are platforms where fflush(stdin) is fully defined (as a non-standard extension on that platform). The primary example is a well-known family of systems known collectively as Windows. Microsoft's specification:

Flushes a stream

The int fflush(FILE *stream ) function flushes a stream. If the file associated with stream is open for output, fflush writes to that file the contents of the buffer associated with the stream. If the stream is open for input, fflush clears the contents of the buffer. fflush negates the effect of any prior call to ungetc against stream. Also, fflush(NULL) flushes all streams opened for output. The stream remains open after the call. fflush has no effect on an unbuffered stream.

Community
  • 1
  • 1
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • @lagoOchoa read this [**How do I... (Level 2) > Flush the input buffer**](http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044873249&id=1043284392) To learn how to flush the input buffer correctly, ... awesome link :) – Grijesh Chauhan May 25 '13 at 18:45
  • "I'd be grateful for any feedback or criticism that I can learn from or use to help me improve my answer." – Grijesh Chauhan May 29 '13 at 06:18
  • @lagoOchoa Read updated answer I added some more information and few links regarding fflush. – Grijesh Chauhan Oct 10 '13 at 14:44
2

fflush(stdin) has undefined behavior.Use this henceforth to deal with the newline that remains in the stdin buffer while using scanf(),especially in cases when you need to read a character but the newline remaining in the buffer is automatically taken up as the character :

 while((c = getchar()) != '\n' && c != EOF);

Here's what the cplusplusreference says about fflush() (You can verify the same from other sources as well,because too many veterans here on SO frown upon cplusplusreference though they fall short of condemning it altogether)

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

http://www.cplusplus.com/reference/cstdio/fflush/

Thokchom
  • 1,602
  • 3
  • 17
  • 32