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

在字符數組輸入中,常用到cin,cin.getline()和cin.get()函數。(不能用在string類型中)

 

cin>>通常只能讀取一個單詞。cin.getline()和cin.get()可以讀取固定長度的字符串,含空格等符號。

1、使用cin函數

由於cin通過空格、製表符、換行符來界定字符串的。故cin在獲取字符時只讀取一個單詞長度,對於有空格的字符串其空格後面字符讀不了。


例如:讀取姓名

#include <bits/stdc++.h> 
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;
}

輸入:HSING   HSU

運行結果:

enter name:
HSING HSU
enter address:
WU HAN
your name is HSING and your address is HSU

該運行結果不是用戶所需的結果。故需要用下面的表示方法。


2、使用cin.getline()函數。

(2.1)使用cin.getline()函數。使用該函數讀取字符串代碼如下:

#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;
}

運行結果:

enter name:
HSING HSU
enter address:
WU HAN
your name is HSING HSU and your address is WU HAN

運行結果正確

cin.getline(name,size)函數,第一個參數表示數組名,傳遞的是字符串首地址,第二個參數是字符長度,包括最後一個空字符的長度,因此只能讀取size-1個字符。由於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;
}

運行結果:

enter name:
HSING HSU
enter address:
your name is HSING HSU and your address is

運行結果不是用戶所需要的結果。


注:第二次直接讀取的是換行符

2、程序修改:

在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.getline()和cin.get()區別

 

分爲三種情況來看:
1、輸入的字符串不超過 size 限定大小
get(str,size):讀取所有字符,遇到'\n'時止,並且將'\n'留在輸入緩衝區中,其將被下一個讀取輸入的操作捕獲,影響下面的輸入輸入處理;
getline(str,Size):讀取所有字符,遇到'\n'時止,並且將'\n'直接從輸入緩衝區中刪除掉,不會影響下面的輸入處理。

2、輸入的字符數超出  size  限定的大小
get(str,Size):讀取Size-1個字符,並將str[Size-1]置爲'\0',然後將剩餘字符(包括'\n')留在輸入緩衝區中,這些字符將被下一個讀取輸入的操作捕獲,影響下面的輸入處理;
getline(str,Size):讀取Size-1個字符,並將str[Size-1]置爲'\0',剩餘字符(包括'\n')留在輸入緩衝區中,隨即設置cin實效位(即if(!cin)的判斷爲真),關閉輸入。其後的所有輸入都無法得到任何東西,當然也無法得到輸入緩衝區中剩餘的字符串。但如果象本例一樣用clear()重置cin,其後的輸入便可用並會得到遺留在輸入緩衝區中的字符。


3、輸入一個空行(即直接回車)
get(str,Size):str將得到'\0',並設置cin實效位,關閉輸入,但回車依然留在輸入緩衝區中,因此如果我們用clear()重置cin,其下一個讀取輸入的操作將捕獲'\n';
getline(str,Size):str將得到'\0',並將'\n'刪除掉,不置實效位,不關閉輸入。所以對於cin.getline來說空行是合法的輸入,且不會影響下面的輸入處理。

至於使用那個更好,可能因人習慣不同而不同,仁者見仁智者見智。對於我們編程來說,總希望能有更好的容錯性,即便用戶輸入了不合理的輸入,程序也應該能夠 提示並能夠重新輸入或繼續正常處理,而因爲用戶的輸入問題而導致程序錯誤或其後的所有輸入都不可用顯然不是我們希望的。使用get(str,Size)和 getline(str,Size),都可能碰到設置失效位,關閉輸入的情況,故都是需要考慮到相應的防錯處理的。

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