C++中有两个getline函数, cin.getline()与getline() 
这两个函数相似,但是 这两个函数分别定义在不同的头文件中。
 
cin.getline()属于istream流,而getline()属于string流,是不一样的两个函数

1.getline()是定义在<string>中的一个行数,用于输入一行string,以enter结束。

getline()的原型是istream& getline ( istream &is , string &str , char delim );
其中 istream &is 表示一个输入流,譬如cin;string&str表示把从输入流读入的字符串存放在这个字符串中(可以自己随便命名,str什么的都可以);char delim表示遇到这个字符停止读入,在不设置的情况下系统默认该字符为'\n',也就是回车换行符(遇到回车停止读入)。

函数原型:istream& getline ( istream &is , string &str , char delim );
is:istream类的输入流对象,譬如cin;
str:待输入的string对象,表示把从输入流读入的字符串存放在这个字符串中(可以自己随便命名,str什么的都可以);
delim:表示遇到这个字符停止读入,在不设置的情况下系统默认该字符为'\n',也就是回车换行符(遇到回车停止读入)。

example 1:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
string fstr;
string lstr; int main(int argc, char* argv[])
{
cout<<"first str: \n";
getline(cin,fstr);
cout<<"last str: \n";
getline(cin,lstr); cout<<"first str:"<<fstr<<","<<"last str:"<<lstr<<endl;
system("pause");
return(0);
}

  

example 2:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std; int main(int argc, char* argv[])
{
string line;
cout<<"please cin a line:";
getline(cin,line,'#');
cout<<endl<<"The line you give is:"<<line<<endl;
return(0);
}

  

2.cin.getline(char ch[],size)是cin 的一个成员函数,定义在<iostream>中,用于输入行指定size的字符串,以enter结束。若输入长度超出size,则不再接受后续的输入。

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
char fstr[6] ;
char lstr[7]; int main(int argc, char* argv[])
{
cout<<"first str: \n";
cin.getline(fstr,6); cout<<"last str: \n"; cin.getline(lstr,7); cout<<"first str:"<<fstr<<","<<"last str:"<<lstr<<endl;
system("pause");
return(0); }

  

这里注意,如果在输入”first str“的时候,输入的字符数多余5个的话,会感觉到    cin.getline(lstr,7);  没有执行

如下图:

为什么呢,因为你输入进去的”wertyuioop”过长,cin函数出错了~

cin有一些函数进行错误处理。这里需要在“cin.getline(fstr,6);”后面加以下两行代码:

cin.clear();//清除错误标志
cin.ignore(1024,'\n');//清除缓存区数据

然后运行就会有了

关于cin出错以及解决方法还在整理中~整理好后放一个连接到这里来~

cin.getline()与getline()的更多相关文章

  1. C++中cin、cin.get()、cin.getline()、getline()、gets()等函数的用法----细节决定成败 (sort用法)

    C++中cin.cin.get().cin.getline().getline().gets()等函数的用法 学C++的时候,这几个输入函数弄的有点迷糊:这里做个小结,为了自己复习,也希望对后来者能有 ...

  2. c++中获取字符cin,getchar,get,getline的区别

    http://www.imeee.cn/News/GouWu/20090801/221298.html cin.get()与getchar()函数有什么区别? 详细点..C++中几个输入函数的用法和区 ...

  3. 日常笔记4关于cin、cin.get()、cin.getline()、getline()使用区别

    1.关于PAT中段错误 使用字符数组出现错误: char str[256]; 报错段错误,然后改用C++中的string 改成: string str; 同char数组一样,也可以使用下标来取单个字符 ...

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

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

  5. C++中关于cin、cin.get()、cin.getline()、getline()、gets()等函数的用法

    1.cin>> 用法1:最基本,也是最常用的用法,输入一个数字: 注意:>> 是会过滤掉不可见的字符(如 空格 回车,TAB 等) cin>>noskipws> ...

  6. C++中cin、cin.get()、cin.getline()、getline()、gets()等函数的用法

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

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

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

  8. [转载]cin、cin.get()、cin.getline()、getline()、gets()函数的用法

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

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

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

随机推荐

  1. c++第三次作业

    GitHub地址 https://github.com/ronghuijun/3Elevators-scheduling 实现过程 一开始打算分成三个类来写的 因为想到电梯的功能不太一样 一个只能上1 ...

  2. prototype.js中Function.prototype.bind方法浅解

    prototype.js中的Function.prototype.bind方法: Function.prototype.bind = function() { var __method = this; ...

  3. Linux学习笔记3

    touch filename 创建一个不存在的文件,或者修改文件的时间戳. touch log.txt whereis name 定位一个文件. whereis php.ini whereis.loc ...

  4. Hibernate 中一级缓存和快照区的理解

    刚刚开始的时候觉得这个快照区很难理解,在网上看了很多博客之后,开始明白了.我是结合 ADO.NET 理解的,在ADO.NET 中有一个类, 叫 SqlCommandBuilder,在我看来,他就是 A ...

  5. 洛谷 P3391 【模板】文艺平衡树(Splay)

    题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...

  6. MSF下ms17_010_psexec模块使用技巧

    0x01 前言 MS17-010 的psexec是针对Microsoft Windows的两款最受欢迎的漏洞进行攻击. CVE-2017-0146(EternalChampion / EternalS ...

  7. HDU.1846 Brave Game (博弈论 巴什博弈)

    HDU.1846 Brave Game (博弈论 巴什博弈) 题意分析 巴什博奕裸题 博弈论快速入门 代码总览 include <bits/stdc++.h> using namespac ...

  8. 函数式编程(1)-高阶变成(1)-map/reduce

    map/reduce Python内建了map()和reduce()函数. 如果你读过Google的那篇大名鼎鼎的论文“MapReduce: Simplified Data Processing on ...

  9. [CodeVs1515]跳(lucas定理+费马小定理)

    嘿嘿嘿好久没写数学题了,偶尔看到一道写一写... 题目大意:一个(n+1)*(m+1)[0<=n, m<=10^12,n*m<=10^12]的矩阵,C(0,0)=1,C(x,y)=C ...

  10. Codeforces 934.C A Twisty Movement

    C. A Twisty Movement time limit per test 1 second memory limit per test 256 megabytes input standard ...