先看代码:

#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

最新文章

  1. iOS开源项目周报1215
  2. java中Set,Map,Stack一些简单用法
  3. thinkPHP访问不同表前缀
  4. DEV控件,PopupContainerEdit,PopupContainerControl,TreeList,弹出控制问题
  5. zw&#183;准专利&#183;高保真二值图细部切分算法
  6. CRB and String
  7. Java构建工具:如何用Maven,Gradle和Ant+Ivy进行依赖管理
  8. c++普通高精加
  9. readonly 关键字的用法
  10. MVC4
  11. SessionStateMode之Redis共享session
  12. bzoj 4446: [Scoi2015]小凸玩密室
  13. JavaScript es2015经验基础总结
  14. 上传到 App Store 时出错。
  15. Event filter with query &quot;SELECT * FROM __InstanceModi
  16. golang 类型转换
  17. Coprime Sequence (HDU 6025)前缀和与后缀和的应用
  18. Unable to instantiate Action, xxxAction, defined for 'xxxAction' in namespace '/'xxx
  19. 05-使用jQuery操作input的value值
  20. shell下变量比较最佳实践

热门文章

  1. Axios Token验证拦截器
  2. HDU 3948 The Number of Palindromes(Manacher+后缀数组)
  3. 5、Docker容器网络
  4. kali虚拟机添加共享文件夹
  5. c#数据库事务锁类型
  6. MySQL 存储过程错误处理
  7. spring 事务关键类
  8. warning C4828问题的处理
  9. D - Dice Game (BFS)
  10. git 拉取远程代码