c语言flush刷新界面,flush(stdin)刷新标准输入缓冲区

// fflush_stdin.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include

using namespace std;

int main(int argc, char* argv[])

{

char i='a';

while( i>'A')

{

printf("Last i=%c,Input new i:\n",i);

scanf("%c",&i);

fflush(stdin);//getchar();

}

printf("hello,world!\n");

cout<

return 0;

}

/*

Last i=a,Input new i:

a

Last i=a,Input new i:

A

hello,world!

hello,world! out!

Press any key to continue

*/C语言中“fflush(stdin)”的作用是什么?

清除标准输入设备(一般是键盘)的缓存。

scanf()函数接收输入数据时,遇以下情况结束一个数据的输入:(不是结束该scanf函数,scanf函数仅在每一个数据域均有数据,并按回车后结束)。

①   遇空格、“回车”、“跳格”键。

②   遇宽度结束。

③   遇非法输入。

键盘缓冲区就可能有残余信息问题。

scanf()函数应该只是扫描stdin流,这个残存信息是在stdin中

解决就要在scanf()函数之后加个fflush(stdin)。

flush(stdin)刷新标准输入缓冲区,把输入缓冲区里的东西丢弃

fflush(stdout)刷新标准输出缓冲区,把输出缓冲区里的东西打印到标准输出设备上。

其他回答 共3条

清除标准输入设备(一般是键盘)的缓存。往往适用于截获输入特殊值,例如每次读取一个输入的字符,但是如果你输完一个字符后敲了回车,回车是一个特殊的键,会产生2个字符,因此程序会认为你输入了3个字符,此时可以用读取一个字符后用这个函数清除键盘缓存,从而清除回车的影响。