输入函数getline(cin,str) 与cin.getline(str,int)的区别[C/C++]

cin.getline()函数是处理数组字符串的,其原型为cin.getline(char * , int),第一个参数为一个char指针,第二个参数为数组字符串长度。

getline(cin,str)函数是处理string类的函数。第二个参数为string类型的变量。

#include <iostream>
#include <string>
using namespace std;
const int SIZE=20;
 
int main()
{
    string str;
    cout<<"please input string:"<<endl;
    getline(cin,str);
    cout<<"the string is:"<<endl;
    cout<<str<<endl;
 
    cin.get();//接受最后一个结束符
 
    char chs[SIZE];
    cout<<"please input chs[]:"<<endl;
    cin.getline(chs,20);
    cout<<"the chs is:"<<endl;
    cout<<chs<<endl;
 
    return 0;
}
/*注:getline(cin,str); 处理后还留有结束符在输入流中,故需要使用cin.get();
接受最后一个结束符,才能接受后面得输入值。两个函数返回值都会丢弃换行符。*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值