fflush不起作用 [英] fflush doesn't work

查看:275
本文介绍了fflush不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么fflush(..)c2c0不起作用?
如果我使用声明c0 = 0c2 = 0起作用,但是fflush(stdin)不起作用,我试图放在不同的地方,但是它不起作用,我在ubuntu 13.04中使用了代码块;

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

推荐答案

fflush(stdin)具有未定义的行为.此后使用此字符处理使用scanf()时保留在stdin缓冲区中的换行符,尤其是在以下情况下您需要读取一个字符,但是缓冲区中剩余的换行符会自动被当作字符:

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

这是cplusplusreference关于fflush()的说法(您也可以从其他来源进行验证,因为这里有太多退伍军人皱眉cplusplusreference,尽管他们没有完全谴责它)

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/

这篇关于fflush不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆