cin.get();cin.clear();cin.sync()
2024-10-19 06:26:41
先看代码:
#include<iostream>
using namespace std;
int main(){
int c,x;
cout<<"输入大小"<<endl;
cin>>c;
x=c+;
cin.sync();//神器;用来清空输入缓冲区剩余的值的
char a[x];
cout<<"输入内容"<<endl;//因为输入大小后直接输入了回车,回车在缓冲区,没有被丢弃,所以a[]直接取的缓冲区的值;直接结束;
cin.get(a,x);//cin.get的参数应该大1;多出一个填充回车;
for(int i=;i<c;i++){
cout<<a[i]<<endl;
}
cout<<sizeof(a)<<endl;
return ;
}
当不用cin.sync()时,cin一个字符,用“回车”结束,但是cin不读入“回车”这个字符,所以回车会留在缓冲区,当cin.get()时,它首先读取缓冲区的内容;然后发现没有才会让你在屏幕输入,但是有内容它就会先读取,所以马上就结束了。
cin.get() this function can read "space" to the char array; Like this:cin.get(char*a,int b),"a" is the name of array,"b"is the number of read characters;
tips: because cin.get() also can accept "Enter" ,so we must made the "b" to be "+1" then the number you want.
cin.clear();的作用是清空错误的标志;并不是清空缓冲区的;详情请见:http://www.cnblogs.com/tonglingliangyong/p/3908463.html
最新文章
- iOS开源项目周报1215
- java中Set,Map,Stack一些简单用法
- thinkPHP访问不同表前缀
- DEV控件,PopupContainerEdit,PopupContainerControl,TreeList,弹出控制问题
- zw&#183;准专利&#183;高保真二值图细部切分算法
- CRB and String
- Java构建工具:如何用Maven,Gradle和Ant+Ivy进行依赖管理
- c++普通高精加
- readonly 关键字的用法
- MVC4
- SessionStateMode之Redis共享session
- bzoj 4446: [Scoi2015]小凸玩密室
- JavaScript es2015经验基础总结
- 上传到 App Store 时出错。
- Event filter with query ";SELECT * FROM __InstanceModi
- golang 类型转换
- Coprime Sequence (HDU 6025)前缀和与后缀和的应用
- Unable to instantiate Action, xxxAction, defined for 'xxxAction' in namespace '/'xxx
- 05-使用jQuery操作input的value值
- shell下变量比较最佳实践