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

 

posted @ 2016-09-22 20:02  ZYVV  阅读(7976)  评论(1编辑  收藏  举报