整理过后,重发帖:清空输入流

citylove 2012-02-21 02:08:13
#include<iostream>
#include<vector>
#include<stdexcept>
#include<cstdio>


using namespace std;

void keep_window_open()
{
cin.clear();
cout<<unitbuf; //就是这个地方,我想将输入流彻底清空,不知道用什么方法

cout << "Please enter a character to exit\n";
char ch;
cin >> ch;
cout <<ch <<endl;
return;
}
int main()
try{
vector<int> v;
int x;

while(cin>>x)
{
v.push_back(x);
}
for(int i=0;i<=v.size();i++)
cout<<"v["<<i<<"]=="<<v.at(i)<<endl;

}catch (exception& oor) {
cerr << "Out of Range error: " << oor.what() << endl;
keep_window_open();
return 1;
}catch(...){
cerr<<"exception :something went wrong\n";
return 2;
}


请大家回复前先认真看一下我的程序~
输入city@ubuntu:~/Desktop/temp$ ./hh
11
22
s
结果:v[0]==11
v[1]==22
Out of Range error: vector::_M_range_check
Please enter a character to exit
s
大家可以看到我是用输入一个字符来结束 while(cin >>x)输入的,但是字符s 还是在输入流中,并在keep_window_open()里赋值给了ch。
我的问题就是怎样在 cin >>ch 这步之前清空输入流??
目前根据大家的回复,我尝试了下面几种方法:
1)cout.flush() cout<<flush flush(cout)都不行。貌似这几个是等价的,我查看了一下,发现这几个都是情况输出流的
2)cin.clear() ; cin.sync(); 这个也不行,但我不知道为什么? cin.sync()的用途是: the unread characters in the buffer are discarded. 求大牛解释
3)cin.ignore(cin.gcount()+1);这个只能清空一个字符时的输入流,如果输入流里是一个字符串,那么这个方法还是会保留下第一个字符。同样不知道为什么???求解释
4)rewind(stdin);这个是赵中老师的方法,他说可以,但是放在我的程序里就是不行,而且不想用scanf。求赵老师根据我的程序给改改吧~~~
5)cout<<unitbuf; 这个也木有用啊~~~unitbuf的解释如下:
Sets the unitbuf "format" flag for the str stream.
When the unitbuf flag is set, the associated buffer is flushed after each insertion operation.

非常感谢提供以上方法的大牛,虽然还木有把我的问题彻底解决~~
希望大家提供更多的有效可行的方法!!!!
...全文
214 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
citylove 2012-02-21
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 forever_feng 的回复:]

我就是在linux上测试的啊,奇怪,你在clear之前调用一下flush()试试看?
[/Quote]
还是不行啊!!!这是怎么回事???难道是因为我用的是虚拟机???我的Ubuntu已经升级到最新版的了啊!
平凡的思想者 2012-02-21
  • 打赏
  • 举报
回复
我就是在linux上测试的啊,奇怪,你在clear之前调用一下flush()试试看?
citylove 2012-02-21
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 forever_feng 的回复:]

先调用cin.clear(),然后调用cin.sync()。
具体原因我上面已经解释过了。

正确代码如下:
void keep_window_open()
{
cin.clear();
cin.sync();

cout << "Please enter a character to exit\n";
char ch;
c……
[/Quote]
为什么把这两句调换一下顺序就不行了呢???求指教!!
citylove 2012-02-21
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 forever_feng 的回复:]

cin.clear()清空标志位,cin.sync()是执行清除动作,但是做清除动作时要看标志位,就这么简单。
[/Quote]
你的方法我试过了,在Windows下可以,但是我在Linux下不行啊!这个难道和操作系统有关系???
IVERS0N 2012-02-21
  • 打赏
  • 举报
回复
cout<<endl;
平凡的思想者 2012-02-21
  • 打赏
  • 举报
回复
cin.clear()清空标志位,cin.sync()是执行清除动作,但是做清除动作时要看标志位,就这么简单。
平凡的思想者 2012-02-21
  • 打赏
  • 举报
回复
先调用cin.clear(),然后调用cin.sync()。
具体原因我上面已经解释过了。

正确代码如下:
void keep_window_open()
{
cin.clear();
cin.sync();


cout << "Please enter a character to exit\n";
char ch;
cin >> ch;
cout <<ch <<endl;

return;
}
citylove 2012-02-21
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 focuslight 的回复:]

cin.sync()的用途是: the unread characters in the buffer are discarded. 求大牛解释
------------ 缓冲里未读的字符全被放弃
[/Quote]
我的意思是为什么这个不行~~~你理解错我的意思了
citylove 2012-02-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 justkk 的回复:]

flush(stdin) 呢?
[/Quote]
好像不行,总是报错

city@ubuntu:~/Desktop/temp$ make
g++ -g -Wall test.cpp -o hh
test.cpp: In function ‘void keep_window_open()’:
test.cpp:14:13: error: no matching function for call to ‘flush(_IO_FILE*&)’
test.cpp:14:13: note: candidate is:
/usr/include/c++/4.6/ostream:564:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::flush(std::basic_ostream<_CharT, _Traits>&)
test.cpp: In function ‘int main()’:
test.cpp:31:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
make: *** [hh] Error 1
citylove 2012-02-21
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zhao4zhong1 的回复:]

用cin似乎也没什么问题。
C/C++ code
#include<iostream>
#include<vector>
#include<stdexcept>
#include<cstdio>
using namespace std;
void keep_window_open()
{
cin.clear();
cin.sync();
cout << "Please……
[/Quote]
赵老师是在Windows下运行成功的么?我在Linux下运行怎么不行呢????
Isnis-fallen 2012-02-21
  • 打赏
  • 举报
回复
cin.sync()的用途是: the unread characters in the buffer are discarded. 求大牛解释
------------ 缓冲里未读的字符全被放弃
平凡的思想者 2012-02-21
  • 打赏
  • 举报
回复
先调用cin.clear(),然后调用cin.sync()。
当程序想要去读一个int型却读到了一个char型输入的时候,cin就会将自己的内部错误标识符设定为ios::failbit(没有错误是ios::goodbit),cin.clear()的作用不是清空输入缓冲区,而是清空这个内部错误标识符,真正清空输入缓冲区的是cin.sync(),但是只清空缓冲区也不行,因为内部错误标识符还保留着呢,下次读取的时候一看上次有错误,这次根本不读了,所以一定要一起调用。
赵4老师 2012-02-21
  • 打赏
  • 举报
回复
用cin似乎也没什么问题。
#include<iostream>
#include<vector>
#include<stdexcept>
#include<cstdio>
using namespace std;
void keep_window_open()
{
cin.clear();
cin.sync();
cout << "Please enter a character to exit\n";
char ch;
cin >> ch;
cout <<ch <<endl;
return;
}
int main() {
try{
vector<int> v;
int x;

while(cin>>x)
{
v.push_back(x);
}
for(int i=0;i<=v.size();i++)
cout<<"v["<<i<<"]=="<<v.at(i)<<endl;
return 0;
} catch (exception) {
cerr << "Invalid input! "<< endl;
keep_window_open();
return 1;
} catch (...) {
cerr<<"exception :something went wrong\n";
return 2;
}
}
//11
//22
//s
//v[0]==11
//v[1]==22
//Invalid input!
//Please enter a character to exit
//a
//a
赵4老师 2012-02-21
  • 打赏
  • 举报
回复
楼主这个例子充分说明C++异常处理能不用最好不用。
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;

void keep_window_open()
{
cout << "Please enter a character to exit\n";
char ch;
rewind(stdin);
ch=getchar();
cout << ch <<endl;
return;
}
int main() {
vector<int> v;
int x,r;

while (1) {
r=scanf("%d",&x);
if (1==r) {//输入一个整数回车
v.push_back(x);
} else if (0==r) {
cerr << "Invalid input!" << endl;
keep_window_open();
return 1;
} else if (EOF==r) {//输入Ctrl+Z回车
break;
} else {
cerr << "exception :something went wrong\n";
return 2;
}
}
for (int i=0;i<=v.size();i++)
cout << "v[" << i << "]==" << v.at(i) << endl;
return 0;
}
//11
//22
//s
//Invalid input!
//Please enter a character to exit
//a
//a

justkk 2012-02-21
  • 打赏
  • 举报
回复
flush(stdin) 呢?

64,662

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧