程序的输入都建有一个缓冲区,即输入缓冲区。一次输入过程是这样的,当一次键盘输入结束时会将输入的数据存入输入缓冲区,而cin函数直接从输入缓冲区中取数据。正因为cin函数是直接从缓冲区取数据的,所以有时候当缓冲区中有残留数据时,cin函数会直接取得这些残留数据而不会请求键盘输入

. cin>>

该操作符是根据后面变量的类型读取数据。

输入结束条件 :遇到Enter、Space、Tab键。

对结束符的处理 :将\n类似的结束符落在缓冲区里.

所以会有如下的程序和结果:

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main()
  5. {
  6. string s1,s2;
  7. cin>>s1;
  8. getline(cin,s2);
  9. cout<<s1<<endl;
  10. cout<<s2<<endl;
  11.    return 0;
  12. }

只输入了一行abc, 然后回车后就输出一行abc, 一行空

这是因为cin在缓冲区里丢下的\n 给后面的getline()当成输入结束的标志了

二.getline(cin, str)

输入结束条件:Enter键

对结束符处理:将\n结束符从缓冲区里清除.

所以会有如下的程序

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main()
  5. {
  6. string s1,s2;
  7. getline(cin,s1);
  8. getline(cin,s2);
  9. cout<<s1<<endl;
  10. cout<<s2<<endl;
  11.    return 0;
  12. }

如果在输入完abc后, 按两次回车, 就会有如下结果

三.getchar()

清除缓冲区里的\n , 建议用getchar(), 这种方式最方便.

四. 例子

先输入一个整数到变量n里 ,然后读入n行字符串, 注意用getchar()消除\n

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main()
  5. {
  6. int n;
  7. string s1;
  8. cin>>n;
  9. getchar();
  10. while(n--){
  11. getline(cin, s1);
  12. }
  13. //process these strings
  14.    return 0;
  15. }


缓冲区 cin() getline() getchar()的更多相关文章

  1. c++中 cin、cin.get()、cin.getline()、cin.getchar()的区别

    ①cin>>:无法接收空格.Tap键且以空格.Tap键.回车符为分隔符: ②cin.get( ):可以接收空格.Tap键且以回车符为结束符: 一:可输入单个字符 格式: char ch; ...

  2. [原创]cin、cin.get()、cin.getline()、getline()、gets()、getchar()的区别

    这几个输入函数经常搞不清具体特点和用法,这里稍作总结 一.cin>> 1.最基本用法,输入一个变量值 2.输入字符串,遇“空格”.“TAB”.“回车”结束,比如输入“hello world ...

  3. C++语言中cin cin.getline cin.get getline gets getchar 的用法实例

    #include <iostream> #include <string> using namespace std; //关于cin cin.getline cin.get g ...

  4. C++中几个输入函数的用法和区别(cin、cin.get()、cin.getline()、getline()、gets()、getchar())

    1.cin>> 用法1:最基本,也是最常用的用法,输入一个数字: #include <iostream>using namespace std;main (){int a,b; ...

  5. getchar()、putchar()、gets()、puts()、cin.get()、cin.getline()、getline()

    1.getchar: 原型为int getchar(void). 它从stdin里读取一个字符.返回值为用户输入的ASCⅡ码,出错返回-1. eg:c=getchar(). 2.putchar: 原型 ...

  6. cin.get(),cin.getline(),getline(),gets(),getchar()

    1.cin.get() (1).cin.get()-------提取单个字符,可以提取回车.空格 a=cin.get(); (2) 同(1)---------------提取单个字符,可以提取回车.空 ...

  7. cin,cin.get(),cin.getline(),gets(),getchar()函数的用法

    1.cin>> 用法a:最基本的流输入用法,接受一个数字或字符,自动跳过输入的空格. 用法b:接受一个字符串,但是遇到除开头外的空格则会终止输入. #include<iostream ...

  8. 关于cin, cin.get(), getchar(),getline()的字符问题

    一.getchar()和cin.get() getchar()会将开头的空格或者回车作为输入 1 #include<iostream> 2 using namespace std; 3 i ...

  9. (转)cin、cin.get()、cin.getline()、getline()、gets()等函数的用法

    学C++的时候,这几个输入函数弄的有点迷糊:这里做个小结,为了自己复习,也希望对后来者能有所帮助,如果有差错的地方还请各位多多指教(本文所有程序均通过VC 6.0运行)转载请保留作者信息:1.cin1 ...

随机推荐

  1. Android NDK 学习之在C中抛出异常

    本博客主要是在Ubuntu 下开发,且默认你已经安装了Eclipse,Android SDK, Android NDK, CDT插件. 在Eclipse中添加配置NDK,路径如下Eclipse-> ...

  2. fatfs源码阅读

    使用fatfs文件的第一步,就是调用F_mount函数注册一个工作空间. F_mount函数的原型如下: 第一个参数根据网上大神的答复,是外设类型,如果是sd卡就是0,flash等等其他的外设就是其他 ...

  3. 对比centos7的systemctl和其他service+chkconfig

    syetemctl就是service和chkconfig这两个命令的整合,在CentOS 7就开始被使用了.systemctl 是系统服务管理器命令,它实际上将 service 和 chkconfig ...

  4. springboot系列(七) 项目热加载

    spring为开发者提供了一个名为spring-boot-devtools的模块来使Spring Boot应用支持热部署,提高开发者的开发效率,无需手动重启Spring Boot应用. devtool ...

  5. 【转载】抓包工具tcpdump用法说明

    转载地址:http://www.cnblogs.com/f-ck-need-u/p/7064286.html tcpdump采用命令行方式对接口的数据包进行筛选抓取,其丰富特性表现在灵活的表达式上. ...

  6. springboot整合freemarker模板引擎后在页面获取basePath绝对路径

    在项目中引用静态资源文件或者进行ajax请求时我们有时候会使用 ${basePath} ,其实这就是一种获取绝对路径的方式: 那么在springboot项目中要怎么配置才能使用 basePaht呢? ...

  7. jFinal的小知识点总结

    sql批处理 // 批处理sql List<String> sqlList = new ArrayList<String>(); sqlList.add("delet ...

  8. pymysql的增删改查、索引

    1.pymysql增删改 一定要有commit() import pymysql username = input("请输入用户名:") pwd = input("请输入 ...

  9. tomcat启动之后,Chrome浏览器可以访问,IE不行(IE无法访问8080 端口)

    方法简单粗暴,在windows中关闭IE服务,然后再重新安装服务. 请注意,在输入框输入:  http://localhost:8080/myproject 不要直接输入localhost:8080/ ...

  10. 在使用rem布局的时候,遇到的样式排版混乱问题,

    在使用rem布局的时候,遇到的样式排版混乱问题, 问题1:设置字体为rem单位,但是没有设置line-height为100%, 即    * {             line-height: 1; ...