问tadom大妈关于缓冲区的问题

jieao111 2008-04-10 02:35:11
1)
cin.clear();
cin.sync();//一直对sync()不是特别清楚,昨晚查了msdn,一大段英文,迷迷糊糊。。

2)
cin.clear();
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
ignore是读取一个然后忽略到,是从哪读取啊,是从缓冲中吗,为什么下面那个还可以输出i
void main(){
int i;
cin>>i;
cin.ignore();
cout<<"Please enter a double"<<i;
}


3)这个里的cin.get()也是从缓冲里读字符的把,读的不是'\n'就continue;
while (!(cin>>i))
{
cin.clear(); //重置输入
while (cin.get() != '\n')
continue; //解除错误的输入
cout<<"Please enter a double"<<"("<<(i+1)<<")"<<": ";
}
...全文
190 6 打赏 收藏 举报
写回复
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
grellen 2008-05-16
  • 打赏
  • 举报
回复
mark
visame 2008-04-10
  • 打赏
  • 举报
回复

首先告诉你:学好英文很重要哦!
istream::sync public member function

int sync ( );

Synchronize input buffer with source of characters

Synchronizes the buffer associated with the stream to its controlled input sequence. This effectively means that the unread characters in the buffer are discarded.
这里的sync就是英文单词Synchronize的缩写,意思是“v. 使同时,同时发生”。
就是把输入缓冲区内没有被读取的全部都扔掉,这样才能“同步”。
Example:
// read a file into memory
#include <iostream>
using namespace std;

int main () {
char first, second;

cout << "Please, enter a word: ";
first=cin.get();
cin.sync();

cout << "Please, enter another word: ";
second=cin.get();

cout << "The first word began by " << first << endl;
cout << "The second word began by " << second << endl;

return 0;
jinwei1984 2008-04-10
  • 打赏
  • 举报
回复
向taodm学习!
taodm 2008-04-10
  • 打赏
  • 举报
回复
sync就是清空现有输入。
ignore是忽略输入,试下还没输入的时候是啥反应就知道了。
get、getline当然可以实现忽略输入功能,不过,语意不正确了。另外,VC6对这2个接口实现上有bug。
hityct1 2008-04-10
  • 打赏
  • 举报
回复
baihacker 2008-04-10
  • 打赏
  • 举报
回复
和stdin同步
相关推荐
发帖
C++ 语言

6.3w+

社区成员

C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
帖子事件
创建了帖子
2008-04-10 02:35
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下