cin.get()和cin.getline()之间的区别
生活随笔
收集整理的這篇文章主要介紹了
cin.get()和cin.getline()之间的区别
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
cin.getline()和cin.get()都是對輸入的面向行的讀取,即一次讀取整行而不是單個數字或字符,但是二者有一定的區別。
cin.get()每次讀取一整行并把由Enter鍵生成的換行符留在輸入隊列中,然而cin.getline()每次讀取一整行并把由Enter鍵生成的換行符拋棄,比如:
1 #include <iostream> 2 using std::cin; 3 using std::cout; 4 const int SIZE = 15; 5 int main( ){ 6 cout << "Enter your name:"; 7 char name[SIZE]; 8 cin.getline(name,SIZE); 9 cout << "name:" << name; 10 cout << "\nEnter your address:"; 11 char address[SIZE]; 12 cin.get(address,SIZE); 13 cout << "address:" << address; 14 }輸出:
Enter your name:zy
name:zy
Enter your address:njfu
address:njfu
?
轉載于:https://www.cnblogs.com/wlzy/p/5897742.html
總結
以上是生活随笔為你收集整理的cin.get()和cin.getline()之间的区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CSS3自定义滚动条
- 下一篇: 前端发给后台的参数中含有中文