该怎么清缓存呢?fflush clear ignore

1)fflush

编译器规定,fflush用于刷新输出流,对于输入流没有规定,也就是说 fflush(stdin) 的结果是未知的。

这也就是在 VC下写C程序时,fflush(stdin)有效,VC下写C++程序时,fflush(stdin)无效的原因。

 

2)VC下写C++程序时,清空缓存使用 clear 和 ignore

1 //重置输入流的状态(从错误中恢复)
2 std::cin.clear();
3 
4 //清空输入流,直到指定的字符(默认为 EOF)
5 std::cin.ignore(INT_MAX);
6 //std::cin.ignore(INT_MAX, '\n');

 

posted @ 2014-12-30 00:04  luzhiyuan  阅读(282)  评论(0编辑  收藏  举报