| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2219 人关注过本帖
标题:[求助]getline函数问题
只看楼主 加入收藏
kisscjy
Rank: 1
等 级:新手上路
帖 子:217
专家分:0
注 册:2007-4-9
收藏
 问题点数:0 回复次数:17 
[求助]getline函数问题

有段代码~~~

#include<iostream>
#include<string>
using namespace std;

void main()
{
int n;
cout<<"输入数字!"<<endl;
cin>>n;

string *fruit=new string [n];

for(int i=0; i<n; i++)
{
getline(cin,fruit[i]);
}
cout<<endl;

for(i=0; i<n; i++)
{
cout<<fruit[i]<<endl;
}

}

当我输入3次ASD的时候
程序只是输出两次ASD,
那还有一次去哪呢??

请高手指教~~

搜索更多相关主题的帖子: getline 函数 fruit string cout 
2007-06-02 00:43
yushui
Rank: 3Rank: 3
等 级:论坛游民
威 望:7
帖 子:1355
专家分:22
注 册:2006-7-19
收藏
得分:0 

数组越界吧 字符串类型的最后一位都要存放NULL的阿


fighting!from now on!
2007-06-02 07:08
herbert_1987
Rank: 5Rank: 5
等 级:贵宾
威 望:15
帖 子:1314
专家分:0
注 册:2007-5-13
收藏
得分:0 

人生重要的不是所站的位置,而是所朝的方向
2007-06-02 08:39
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
以下是引用yushui在2007-6-2 7:08:07的发言:

数组越界吧 字符串类型的最后一位都要存放NULL的阿


Fight  to win  or  die...
2007-06-02 09:43
kisscjy
Rank: 1
等 级:新手上路
帖 子:217
专家分:0
注 册:2007-4-9
收藏
得分:0 

哦,谢谢了~~~


每当我一晚写下70,80个程序时,你还真以为,这都是我一个人干的.....不过说真的,其实都是抄书的~~ ^@^
2007-06-02 10:21
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 

还是不懂,fruit[i]是一个string对象,为什么数组越界?
string str;
getline(cin,str);有问题吗?
然道不行?
谢谢说明白点。


[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2007-06-02 11:49
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 

说错了!

开始还以为是char数组来的。

跟踪了下,发现在读取fruit[0]的时候,会自动读取'\n',应该是跟输入流cin缓存区有关。

我也很想知道为什么,等待解答!


Fight  to win  or  die...
2007-06-02 12:08
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 

被截断了吧,如果是其他的编译器的话,就不会了是这样的结果了,比如说GCC++。这里也同样是这个问题,偶查了点资料,还是无头绪。http://bbs.bc-cn.net/dispbbs.asp?boardid=56&replyid=135595&id=137935&page=1&skin=0&star=2





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-06-02 14:26
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 

对于C风格字符串,有如下输入方式:
char info[20];
cin>>info; //read a word;
cin.getline(info,20); // read a line ,discard \n
cin.get(info,20); //read aline ,leave \n in queue


对于string 对象,有两种输入方式:
string stuff;
cin>>stuff; //read a word
getline(cin,stubuf); // read a line discard\n


有两个版本的getline()都有一个参数,
用于指定哪个字符来确定输入的边界:
cin.getine(info,20,':') //read up to :,discard :
getline(stuff,':') ////read up to :,discard :

string 版本的getline()将自动调整string 对象的大小,使之刚好能存储输入的字符
char fname[10];
string lname;
cin>>fname; //could be a problem if input siae >9 characters

cin>>lname; //can read a very very long word
cin.getline(fname,10); //may truncate input
getline(cin,fname) ; //no truncate

string 的版本getline()的自动调节节大小的功能不需要指定输入的大小
string 版本的getline()从输入中读取字符,并将其存储在目标string 中,但有三种情况例外:
1:到达文件尾,这样,输入流的edfbit被设置,以为着方法file(),eof()都将返回true;
2:遇到分界字符,默认为‘\n’,这种情况下,将把分界字符从输入流中删除,而且不存储她;
3:读取的字符到达最大的允许值,它将设置输入流的failbit,这意味着fail()方法返回true; /////Attention !!!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我想下面的这段文字也可以解释LZ的问题。
(from MSDN)
string::getline :
Remarks

Note
The class/parameter names in the prototype do not match the version in the header file. Some have been modified to improve readability.

The getline function creates a string containing all of the characters from the input stream until one of the following situations occurs: - End of file. - The delimiter is encountered. - is.max_str elements have been extracted.

Example
Copy Code
// string_getline_sample.cpp
// compile with: /EHsc
// Illustrates how to use the getline function to read a
// line of text from the keyboard.
//
// Functions:
//
// getline Returns a string from the input stream.
//////////////////////////////////////////////////////////////////////

#pragma warning(disable:4786)
#include <string>
#include <iostream>

using namespace std ;

int main()
{
string s1;
cout << "Enter a sentence (use <space> as the delimiter): ";
getline(cin,s1, ' ');
cout << "You entered: " << s1 << endl;;
}

Input

test this

Sample Output

Enter a sentence (use <space> as the delimiter): test this
You entered: test


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The key as follows:
The getline function creates a string containing all of the characters from the input stream until one of the following situations occurs: - End of file. - The delimiter is encountered. - is.max_str elements have been extracted.



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-06-02 17:03
kisscjy
Rank: 1
等 级:新手上路
帖 子:217
专家分:0
注 册:2007-4-9
收藏
得分:0 

确实是输入流cin 的缓存区有关~~
清空缓存区就可以了~~

但是为什么会自动读取 \n 呢?~~


每当我一晚写下70,80个程序时,你还真以为,这都是我一个人干的.....不过说真的,其实都是抄书的~~ ^@^
2007-06-02 18:37
快速回复:[求助]getline函数问题
数据加载中...
 
   



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

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