C++ 输入函数 cin>>、cin.getline()和cin.get()区别
2011-08-05 01:01
2431 查看
在字符串输入中,常用到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
运行正确。
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
运行正确。
相关文章推荐
- C++ 输入函数 cin>>、cin.getline()和cin.get()区别
- C++ 输入函数 cin>>、cin.getline()和cin.get()区别
- C++ 输入函数 cin>>、cin.getline()和cin.get()区别
- C++ 输入函数getline(cin,str) 与cin.getline(str,int)区别
- C++ 输入函数getline(cin,str) 与cin.getline(str,int)区别
- C++中几个输入函数的用法和区别(cin、cin.get()、cin.getline()、getline()、gets()、getchar()))
- C++中cin>>、cin.get()、cin.getline()、getline()、gets()输入函数详解
- C++ 输入函数getline(cin,str) 与cin.getline(str,int)区别
- C++中cin.get(),cin.getline(),cin>>,gets(),cin.clear()使用总结
- C++中cin>> cin.get(char[], int) cin.getline(char[], int) cin.get(char)输入区别
- c++中字符输入函数getline、cin.getline区分
- c++中字符输入函数cin.getline在输入char与string时的不同
- C++中string类的处理字符串构造函数用法, 以及getline cin.getline()输入的区别
- c++中的各种输入操作符 cin,cin.get(),cin.getline()等函数用法总结
- c++中字符输入函数getline、cin.getline区分
- 字符串输入函数cin.getline()、与cin.get()的区别
- 自学研究了一下C++输入字符(串)的cin>>,cin.get,cin.getline
- cin、cin.get()、cin.getline()、getline()、gets()等函数的用法 学C++的时候,这几个输入函数弄的有点迷糊;这里做个小结,为了自己复习,也希望对后来者能有所
- c++中 cin.get();cin.getline();getline();gets();getchar()等输入函数的小结
- C++中cin、cin.get()、cin.getline()、getline()、gets()等函数的用法