C++ 输入函数 cin>>、cin.getline()和cin.get()区别

来源:互联网 发布:4399js金沙 编辑:程序博客网 时间:2024/04/28 02:43

在字符串输入中,常用到cin,cin.getline()和cin.get()函数。

cin>>通常只能读取一个单词。cin.getline()和cin.get()可以读取固定长度的字符串,含空格等符号。

一、使用cin函数
由于cin通过空格、制表符、换行符来界定字符串的。故cin在获取字符时只读取一个单词长度,对于有空格的字符串其空格后面字符读不了。
例如:读取姓名
#include <iostream>
using namespace std;


int main()
{
const int size=20;
char name[size];
char add[size];
cout<<"enter name:"<<endl;
cin>>name;
cout<<"enter address:"<<endl;
cin>>add;
cout<<"your name is "<<name<<" and your address is "<<add<<endl;
return 0;
}


运行结果:
enter name:
HSING HSU
enter address:
your name is HSING and your address is HSU


该运行结果不是用户所需的结果。故需要用下面的表示方法。


二、使用cin.getline()函数。
(1)使用cin.getline()函数。cin.getline(name,size)函数,第一个参数表示数组名,传递的是字符串首地址,第二个参数是字符长度,包括最后一个空字符的长度,因此只能读取size-1个字符。
使用该函数读取字符串代码如下:
#include <iostream>
using namespace std;


int main()
{
const int size=20;
char name[size];
char add[size];
cout<<"enter name:"<<endl;
cin.getline(name,size);
cout<<"enter address:"<<endl;
cin.getline(add,size);
cout<<"your name is "<<name<<" and your address is "<<add<<endl;
return 0;
}


运行结果:
enter name:
HSING HSU
enter address:
WU HAN
your name is HSING HSU and your address is WU HAN


运行结果正确


(2)
由于cin.getline(name,size)返回的是一个cin对象,因此可以将两个成员函数拼接起来。使用cin.getline(name,size).getline(add,size);
代码如下:
#include <iostream>
using namespace std;


int main()
{
const int size=20;
char name[size];
char add[size];
cout<<"enter name and address:"<<endl;
cin.getline(name,size).getline(add,size);
cout<<"your name is "<<name<<" and your address is "<<add<<endl;
return 0;
}
运行结果:
enter name and address:
HISNG HSU
WU HAN
your name is HISNG HSU and your address is WU HAN
Press any key to continue


运行也正确。


三、使用cin.get()函数
(1)cin.get()函数与cin.getline()函数类似。但cin.get(name,size);读取到行尾后丢弃换行符,因此读取一次后换行符任留在输入队列中。
例:
#include <iostream>
using namespace std;


int main()
{
const int size=20;
char name[size];
char add[size];
cout<<"enter name:"<<endl;
cin.get(name,size);
cout<<"enter address:"<<endl;
cin.get(add,size);
cout<<"your name is "<<name<<" and your address is "<<add<<endl;
return 0;
}


运行结果:
enter name:
HSING HSU
enter address:
your name is HSING HSU and your address is


运行结果不是用户所需要的结果。
注:第二次直接读取的是换行符


(2)
cin.get()修改:在cin.get(name,size);后面加一条语句:
cin.get();该函数可以读取一个字符。将换行符读入。


#include <iostream>
using namespace std;


int main()
{
const int size=20;
char name[size];
char add[size];
cout<<"enter name:"<<endl;
cin.get(name,size);
cin.get();
cout<<"enter address:"<<endl;
cin.get(add,size);
cout<<"your name is "<<name<<" and your address is "<<add<<endl;
return 0;
}


运行结果:
enter name:
HSING HSU
enter address:
WU HAN
your name is HSING HSU and your address is WU HAN


运行正确。


(3)也可以将两个成员函数拼接起来cin.get(name,size).get()
使用.get()接受后面留下的换行符。


#include <iostream>
using namespace std;


int main()
{
const int size=20;
char name[size];
char add[size];
cout<<"enter name"<<endl;
cin.get(name,size).get();
cout<<"enter address:"<<endl;
cin.get(add,size);
cout<<"your name is "<<name<<" and your address is "<<add<<endl;
return 0;
}


运行结果:
enter name:
HSING HSU
enter address:
WU HAN
your name is HSING HSU and your address is WU HAN

0 0
原创粉丝点击