现在的位置: 首页 > 综合 > 正文

清出c++程序中的错误的信息即清除缓存

2013年08月08日 ⁄ 综合 ⁄ 共 1171字 ⁄ 字号 评论关闭

int fflush(FILE *stream);

If stream 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.

清空输入缓冲区的方法:

可以自己写代码来清空输入缓冲区。只需要在
scanf 函数后面加上几句简单的代码就可以了。

#include <iostream>
        #include <limits>//
为了使用numeric_limits

     using namespace std;

     using std::numeric_limits;
        using std::streamsize;

     int main()
        {
            int value;
            for ( ; ; )
            {
                cout << "Enter an integer: ";
                cin >> value;
                if ( cin.eof() || cin.bad() )
                {
// 如果用户输入文件结束标志(或文件已被读完),
                  //
或者发生读写错误,则退出循环

break;
                }
               
// 读到非法字符后,输入流将处于出错状态
                //
为了继续获取输入,首先要调用 clear
函数
                //
清除输入流的错误标记,然后才能调用
                // ignore
函数来清除输入流中的数据。
                cin.clear();
               
// numeric_limits<streamsize>::max()
返回输入缓冲的大小。
                // ignore
函数在此将把输入流中的数据清空。

             cin.ignore(sed::numeric_limits<streamsize>::max(),
'\n' );

                cout << value << '\n';
            }

         return 0;
        }

清除程序中的错误信息。就还有很多的方法,但是以上代码段较为简单。若是你学的c没有学c++也可以改编。

抱歉!评论已关闭.