有关getline()的问题

j765471710 2007-04-11 04:12:28
#include<iostream>
using namespace std;

int main()
{
char stringOne[256];
char stringTwo[256];
char stringThree[256];

cout << "Enter string one: ";
cin.getline(stringOne,256);
cout << "stringOne: " << stringOne << endl;

cout << "Enter string two: ";
cin >> stringTwo;
cout << "stringTwo: " << stringTwo << endl;

cout << "Enter sting three: ";
cin.getline(stringThree,256);
cout << "stringTree: " << stringTh bree << endl;
return 0;
}
程序输出为:
Enter string one: one two three
stringOne: one two three
Enter string two: four five six
stingTwo: four //此处为什么只输出了“four” ?
Enter string three: stringThree: five six //


所谓的 getline丢弃换行符是什么意思?
...全文
1785 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
jixingzhong 2007-05-02
  • 打赏
  • 举报
回复
所谓的 getline丢弃换行符是什么意思?

getline 在接收到 回车 或者指定长度后终止,
如果是回车作为输入终止符号,
那么这个最后的 回车 是被丢弃而不在 getline 指定buffer中的,
也就是 “getline丢弃换行符”。

getline
Syntax:

#include <fstream>
istream& getline( char* buffer, streamsize num );
istream& getline( char* buffer, streamsize num, char delim );

The getline() function is used with input streams, and reads characters into buffer until either:

* num - 1 characters have been read,
* a newline is encountered,
* an EOF is encountered,
* or, optionally, until the character delim is read. The delim character is not put into buffer.
toryhector 2007-05-02
  • 打赏
  • 举报
回复
将 '\n ' 换成 ' \0'
egghitler 2007-04-13
  • 打赏
  • 举报
回复

stingTwo: four //此处为什么只输出了“four” ?
******************************************************

cout << "Enter string two: ";
cin >> stringTwo;//不是cin.getline(stringTwo,256);
yuanchuang 2007-04-11
  • 打赏
  • 举报
回复
比较基础的问题,转新手乐园,呵呵
yuyunliuhen 2007-04-11
  • 打赏
  • 举报
回复
Extract strings from the input stream line-by-line.


template<class CharType, class Traits, class Allocator>
basic_istream<CharType, Traits>& getline(
basic_istream<CharType, Traits>& _Istr,
basic_string<CharType, Traits, Allocator>& _Str
);

template<class CharType, class Traits, class Allocator>
basic_istream<CharType, Traits>& getline(
basic_istream<CharType, Traits>& _Istr,
basic_string<CharType, Traits, Allocator>& _Str,
CharType _Delim
);



Parameters
_Istr
The input stream from which a string is to be extracted.

_Str
The string into which are read the characters from the input stream.

_Delim
The line delimiter.

Return Value
The first function returns getline( _Istr, _Str, _Istr.widen( '\n' ) ).

The second function replaces the sequence controlled by _Str with a sequence of elements extracted from the stream _Istr.

Remarks
In order of testing, extraction stops:

At end of file.

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.

After the function extracts str.max_size elements, in which case the function calls setstate(ios_base::failbit).

If the function extracts no elements, it calls setstate(failbit). In any case, it returns _Istr.

Requirements
Header: <string>
roger_77 2007-04-11
  • 打赏
  • 举报
回复
totoorange(千江有水千江月,万里无云万里天)
已经说出原因了
xlbdan 2007-04-11
  • 打赏
  • 举报
回复
楼主笔误了吧,只有第一个cin用的是cin.getline,而其它地方只是cin,那当然不行了.

cin到空格就已经停止了,不再接收.

所以要都改成cin.getline才行
jixingzhong 2007-04-11
  • 打赏
  • 举报
回复
空格被认为是 数据输入的分割符号,
所以后面的部分没有接收。
totoorange 2007-04-11
  • 打赏
  • 举报
回复
cin 输入遇到空格就结束了,后面的都无效。但是已经放进buffer里了。第2次输入结束后,你输入了回车,所以第三次没有提示你输入stringThree,而直接把前一次buffer里面空格('\n')后面的内容当成是第三次输入的显示出来了。
我是这么认为的。
iu_81 2007-04-11
  • 打赏
  • 举报
回复
std::cin不能直接接受空格(空格作为输入的标志)
iu_81 2007-04-11
  • 打赏
  • 举报
回复
cin >> stringTwo;
不能输出带空格的字符串
hellox 2007-04-11
  • 打赏
  • 举报
回复
把你的代码改为如下,就可以了..
#include<iostream>
using namespace std;

int main()
{
char stringOne[256];
char stringTwo[256];
char stringThree[256];

cout << "Enter string one: ";
cin.getline(stringOne,256);
cout << "stringOne: " << stringOne << endl;

cout << "Enter string two: ";
cin.getline(stringTwo,256);//cin >> stringTwo;
cout << "stringTwo: " << stringTwo << endl;

cout << "Enter sting three: ";
cin.getline(stringThree,256);
cout << "stringTree: " << stringTh bree << endl;
return 0;
}
hellox 2007-04-11
  • 打赏
  • 举报
回复
比如:
string s;
cin>>s;//你输入a aaaaaaaaaaaa
不管后面有多少个a,输出都只有1个a!!
hellox 2007-04-11
  • 打赏
  • 举报
回复
stingTwo: four //此处为什么只输出了“four” ?
=====================================================
你的代码是
cout << "Enter string two: ";
cin >> stringTwo;//不是cin.getline(stringTwo,256);
iu_81 2007-04-11
  • 打赏
  • 举报
回复
cin.getline()方法连续地从用户终端接受字符,并将字符存入字符型数组message中,直到输入了(maxchars-1)个字符(第maxchars个字符用来存储字符串结尾的NULL字符'\0')或者接受到了回车为止,这终端键入回车键产生一个换行'\n',它被cin.getline()认为是行输入结尾。cin.getline()获得的字符(除了换行符外)被存储到message数组中。在返回之前,cin.getline()函数在存储的这些字符后面添加一个NULL字符'\0'。

freshui 2007-04-11
  • 打赏
  • 举报
回复
getLine 遇到回车就读取结束了 不清楚 是不是buffer还有上次的回车?
taodm 2007-04-11
  • 打赏
  • 举报
回复
你输入了一个回车符,它没有存入stringOne吧,那不是丢弃掉了?

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧