蓝森林首页 | 返回主页 | 本站地图 | 站内搜索 | 联系信箱 |
 您目前的位置:首页 > 自由软件 > 技术交流 > 应用编程


    

蓝森林 http://www.lslnet.com 2006年6月6日 10:18


c++初学,关于getline()的问题

#include <iostream>;

using namespace std;
int main()
{
        cout<<"please enter the host's ip:";
        string s;      
        cin>;>;s;
        cout<<"please enter the port:";
        int port;
        cin>;>;port;
        cout<<"your host is:"<< s <<endl;
        cout<<"your port is :" << port<<endl;
        cout<< "input all line : ";
        string ss;
        getline(cin,ss);
        cout << ss <<endl;

}


不知道为什么getline()直接就过去了,没有等待我输入,前边的几句都正常。
如果我们把前边的注释掉:
留下
cout<< "input all line : ";
        string ss;
        getline(cin,ss);
        cout << ss <<endl;
程序正常,等待我输入

不知道什么原因? :em05:

c++初学,关于getline()的问题

getline前加上一句:
cin.ignore();
因为你在回车的时候cin并未读取这最后一个回车字符.还放在缓冲区中.
而当你getline时它默认是遇到'\n'就结束读取的.除非你把getline改成:
getline(cin,ss,'#');不过你得注意在最后加上一#字符,放心,这个#不会放在你的ss中的.当然这样做之后输出就是空出一行,还是因为前面的回车.

c++初学,关于getline()的问题

真是小问题,大文章。。。。
多谢指点 :em09:



Copyright © 1999-2000 LSLNET.COM. All rights reserved. 蓝森林网站 版权所有。 E-mail : webmaster@lslnet.com