gpt4 book ai didi

c - fflush 不起作用

转载 作者:太空狗 更新时间:2023-10-29 14:59:10 26 4
gpt4 key购买 nike

为什么 fflush(..)c2c0 不起作用?
如果我使用声明 c0 = 0c2 = 0 它有效,但 fflush(stdin) 无效,我试着把在不同的地方但它没有用,我在 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;
}

最佳答案

在您的系统 ubuntu 13.04(Unix 或 Linux)上调用 fflush (stdin); 是未定义的行为!

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

要学习正确刷新输入缓冲区的技巧,您可以使用以下一些实际从输入缓冲区读取和丢弃不需要的字符的代码片段。在读取实际数据之前,您可以将其用作 fflush。 read this FAQ entry.

对于 C:

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

对于 C++:

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

但是,如果您在输入流中没有数据时调用它们,程序将等待直到有数据,这会给您带来不良结果。

阅读:@ Keith Thompson的回答:"Alternative to C library-function fflush(stdin)"

编辑:
在某些平台上,fflush(stdin) 已完全定义(作为该平台上的非标准扩展)。主要示例是一个众所周知的系统系列,统称为 Windows。微软的规范:

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.

关于c - fflush 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16752759/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com