您的位置:首页 > 编程语言 > C语言/C++

c++中cin对象中getline()方法和get()方法的区分

2014-06-22 10:26 316 查看
getline 和get的区别:

getline 每次读取一行,舍弃最后的换行符,下一个getline来的时候,直接读取下一行!

get每次读取一行,但是将每一行最后的换行符保留在输入队列中!下一个get来的时候,先读取输入队列中的换行符!

#include <iostream>

using namespace std;

int main()
{
const int Size=20;
char s1[Size];
char s2[Size];

cout<<"Enter the s1"<<endl;
cin.get(s1,Size);
cout<<"Enter the s2"<<endl;
cin.get(s2,Size);
cout<<"s1:"<<s1<<endl;
cout<<"s2:"<<s2<<endl;
return 0;
}




#include <iostream>

using namespace std;

int main()
{
const int Size=20;
char s1[Size];
char s2[Size];

cout<<"Enter the s1"<<endl;
cin.get(s1,Size);
cout<<"Enter the s2"<<endl;
cin.get(s2,Size);
cout<<"s1:"<<s1<<endl;
cout<<"s2:"<<s2<<endl;
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  getline get c++
相关文章推荐