fflush doesn't work

a 夏天 提交于 2019-12-01 03:01:21

问题


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;
    }

回答1:


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/




回答2:


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.



来源:https://stackoverflow.com/questions/16752759/fflush-doesnt-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!