先看代码:

#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

cin.get();cin.clear();cin.sync()的更多相关文章

  1. cin详解(cin.get()、cin.getline()、cin.clear()、cin.sync())

    在C中,输入输出用scanf和printf,在输入数据的同时还需说明数据的类型,如果输入数据较多,那就很麻烦,而C++中也有相似的东西cin和cout,它们来自C++的一个名叫" iostr ...

  2. cin详解(get()、getline()、clear()、sync())

    简述 在C中,输入输出用scanf和printf,在输入数据的同时还需说明数据的类型,如果输入数据较多,那就很麻烦,而C++中也有相似的东西cin和cout,它们来自C++的一个名叫" io ...

  3. (C/C++学习)3.C++中cin的成员函数(cin.get();cin.getine()……)

    说明:流输入运算符,在一定程度上为C++程序的开发提供了很多便利,我们可以避免C语言那种繁琐的输入格式,比如在输入一个数值时,还需指定其格式,而cin以及cout则不需要.但是cin也有一些缺陷,比如 ...

  4. clear()、sync()、ignore()

    #include <iostream> using namespace std; int main() { int a; cin>>a; cout<<cin.rds ...

  5. 注意当cin.getline、和cin 合用的时候

    1. getline有两种,一个是string的(getline(cin,s)),一个是istream的(cin.getline(s,maxsize,delim)). 2. 当用cin输入了数据后,在 ...

  6. C++中cin.clear()的用法

    我们谈谈cin.clear的作用,第一次看到这东西,很多人以为就是清空cin里面的数据流,而实际上却与此相差很远,首先我们看看以下代码: #include <iostream>  usin ...

  7. C++中cin.get(),cin.getline(),cin>>,gets(),cin.clear()使用总结

    1.cin.get()  实质:类istream所定义对象cin的重载成员函数 用于读取单字符  istream& get(char&)    int get(void) 用于读取字符 ...

  8. UVA 1398 Meteor

    传送门 Solution: 记一颗流星在视野内的时间段为(L, R), 为了使所有(L, R)都取整数,首先将坐标放大. 放大倍数可取为 LCM(1, 2, ..., 10)= 2520 接着计算:从 ...

  9. Path of Equal Weight (DFS)

    Path of Equal Weight (DFS)   Given a non-empty tree with root R, and with weight Wi assigned to each ...

随机推荐

  1. 三、原子变量与CAS算法

    原子变量:jdk1.5 后 java.util.concurrent.atomic 包下提供了常用的原子变量: - AtomicBoolean - AtomicInteger - AtomicLong ...

  2. springboot2.0 JPA配置自定义repository,并作为基类BaseRepository使用

    springboot2.0 JPA配置自定义repository,并作为基类BaseRepository使用 原文链接:https://www.cnblogs.com/blog5277/p/10661 ...

  3. python2和python3的range(100)的区别

    python2返回列表,python3返回迭代器,节约内存

  4. linux 内存-文档学习

    ptmalloc http://www.malloc.de/en/ tcmalloc https://github.com/gperftools/gperftools jcmalloc http:// ...

  5. linux blast

    建库 减压后,改名为blast,并在blas目录在建立db文件1,建立数据库makeblastdb -in db.fasta -dbtype nucl(prot) -parse_seqids -has ...

  6. 2018-2019-2 网络对抗技术 20165303 Exp3 免杀原理与实践

    实验内容 一. 正确使用msf编码器(0.5分),msfvenom生成如jar之类的其他文件(0.5分),veil-evasion(0.5分),加壳工具(0.5分),使用shellcode编程(1分) ...

  7. gm图片处理器:The gm/convert binaries can't be found

    今天在使用node处理模块gm的时候,总是无法正常使用,报:The gm/convert binaries can't be found(找不到gm/convert二进制文件) google了一下,发 ...

  8. SpringBoot之AOP

    AOP:面向切面编程,相当于OOP面向对象编程. Spring的AOP的存在目的是为了解耦,AOP可以让一组类共享相同的行为. Spring支持AspectJ的注解切面编程: (1)使用@Aspect ...

  9. Matlab-6:解非线性方程组newton迭代法

    函数文件: function x=newton_Iterative_method(f,n,Initial) x0=Initial; tol=1e-11; x1=x0-Jacobian(f,n,x0)\ ...

  10. maven中央仓库地址(支持db2,informix等)

    maven中央仓库地址(以下设置写在pom.xml文件里): <repositories> <repository> <id>nexus</id> &l ...