C++ 輸入函數 cin>>、cin.getline()和cin.get()區別

在字符串輸入中,常用到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


運行正確。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章