| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4784 人关注过本帖
标题:一个getline和cin.geline这两个函数的区别
只看楼主 加入收藏
zhen16
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2007-1-4
收藏
 问题点数:0 回复次数:17 
一个getline和cin.geline这两个函数的区别
这两个函数同时是存储一个句子,用getline函数时,当输入一个字符串时,要敲两下回车,这个语句才结束,而用cin.getline则不用
当我运行这个程序时
#include <iostream>
#include <string>
using namespace std;
main ()
{
string name;
getline (cin, name);
cout <<name;
}
要想执行cout这个语句时,要敲两次回车才可以,当我输入one sentence[ENTER]时,它并不运行cout这个语句,而是光标还在编绎窗口上闪动,要再按一下[ENTER]才会运行cout这个语句,
而下面这个用cin.getline函数就不用,
#include <iostream>
#include <string>
using namespace std;
main ()
{
char name[100];
cin.getline (name , 100);
cout <<name;
}
而这个程序当我输入one sentence[ENTER]时就会运行cout这个语句,请高手解答一下
搜索更多相关主题的帖子: cin geline getline 函数 
2007-03-09 18:52
dlcdavid
Rank: 3Rank: 3
来 自:成都
等 级:新手上路
威 望:6
帖 子:193
专家分:0
注 册:2005-12-23
收藏
得分:0 

应该是一样的吧, 都是一个函数

下面那个程序,用string不用char[]试试呢?


为了C++,我放弃了课本
为了高考,我又放弃了C++
现在而今眼目下,我能做什么?www.
2007-03-09 23:43
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 

getline()
Syntax:
#include <string>
istream& getline( istream& is, string& s, char delimiter = '\n' );

The C++ string class defines the global function getline() to read strings from and I/O stream. The getline() function, which is not part of the string class, reads a line from is and stores it into s. If a character delimiter is specified, then getline() will use delimiter to decide when to stop reading data.

For example, the following code reads a line of text from STDIN and displays it to STDOUT:

string s;
getline( cin, s );
cout << "You entered " << s << endl;

getline()是一个流类库的一个成员函数,其书写形式是:

cin.geline(v,n);// getline()和cin.geline()应该是一样 ,只是参数不同,

其中参数v用来指定存放字符串的缓冲区地址,第二个参数n指定缓冲区长度。
使用getline(cin,i,?)函数可以输入带空格的整行字符 ,第三个参数默认为'\n'


Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-03-10 00:36
zhen16
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2007-1-4
收藏
得分:0 

谢谢了,不过还是不太懂,因为我想问的是为什么getline函数要敲两次回车才会执行完该语句,而cin.getline只需一次就行了

2007-03-11 22:31
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 

lz没提出来,我还真没注意这个问题,后来查了下MSDN,想想会不会是如下原因,望大家一起讨论。。。

istream::getline:
Extracts characters from the stream until either the delimiter delim is found, the limit nCount–1 is reached, or end of file is reached. The characters are stored in the specified array followed by a null terminator. If the delimiter is found, it is extracted but not stored.

The second template function replaces the sequence controlled by str with a sequence of elements extracted from the stream is. In order of testing, extraction stops:
1、At end of file.
2、After the function extracts an element that compares equal to delim, in which case the element is neither put back nor appended to the controlled sequence.
3、After the function extracts is.max_size() elements, in which case the function calls setstate(ios_base::failbit).

也就是说,cin.getline执行时,当用户输入一个回车后,cin.getline从istream中吸取出\n(但是不store),然后函数结束.getline则是把第一个\n当作一行语句中的一个字符对待,因此第二个回车才是函数结束的标志(我总觉得和put back这句有关,但是我的解释还有一点问题,就是put back到底做了什么,getline中有这句,cin.getline没有这句,他们是在这有区别吗?)


[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2007-03-12 21:58
csight
Rank: 1
等 级:新手上路
威 望:1
帖 子:293
专家分:0
注 册:2006-6-11
收藏
得分:0 
1.istream& getline( istream& is, string& s, char delimiter = '\n' );
2.cin.getline(string &,LENGTH,char delimiter = '\n' );
是两个不同的函数;
一个是属于string;
一个是属于istream流;

头可断,发型不可乱;血可流,皮鞋不可不擦油;
2007-03-12 22:27
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 

呵呵,ls开玩笑,不过他们都是做和流有关的处理,只是处理流里面的缓冲时,稍微有一点不同。


[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2007-03-13 13:49
litcatyx
Rank: 1
等 级:新手上路
威 望:1
帖 子:151
专家分:0
注 册:2006-10-27
收藏
得分:0 
我这里敲一次回车就可以了,我用的是vs2005

2007-03-13 21:55
litcatyx
Rank: 1
等 级:新手上路
威 望:1
帖 子:151
专家分:0
注 册:2006-10-27
收藏
得分:0 
以下是摘自CSDN上的一段文字


FIX: getline Template Function Reads Extra Character
修正: getline 模板函数读取额外字符


RESOLUTION
解决方案

Modify the getline member function, which can be found in the following system header file "string", as follows:
在系统头文件 string 中,修改 getline 成员函数的内容为以下形式(用记事本查找以下代码段定位):

else if (_Tr::eq((_E)_C, _D))
{_Chg = true;

// _I.rdbuf()->snextc(); // 删除这一行,加上以下一行
_I.rdbuf()->sbumpc();
break; }




STATUS
状态

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.
微软已经证实这是列在本文开始处微软产品中发现的 Bug (这个中文怎么说来着?).

This problem was corrected in Microsoft Visual C++ .NET.
该问题已在 Microsoft Visual C++.NET 中修正.


MORE INFORMATION
更多信息

The following sample program demonstrates the bug:
以下程序演示该 Bug:

//test.cpp

#include <string>
#include <iostream>
int main () {
std::string s,s2;
std::getline(std::cin,s);
std::getline(std::cin,s2);
std::cout << s <<'\t'<< s2 << std::endl;
return 0;
}



//Actual Results:
//实际结果

Hello<Enter Key>
World<Enter Key>
<Enter Key>
Hello World



//Expected Results:
//预期结果

Hello<Enter Key>
World<Enter Key>
Hello World

2007-03-13 22:15
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 
同意楼上

Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-03-13 22:17
快速回复:一个getline和cin.geline这两个函数的区别
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031319 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved