关于getline函数的问题?
#include <iostream>
#include <string> // make string class available
#include <cstring> // C-style string library
int main()
{
using namespace std;
char charr[20];
string str;
cout << "Length of string in charr before input: "
<< strlen(charr) << endl;
cout << "Length of string in str before input: "
<< str.size() << endl;
cout << "Enter a line of text:\n";
cin.getline(charr, 20); // indicate maximum length
cout << "You entered: " << charr << endl;
cout << "Enter another line of text:\n";
getline(cin, str); // cin now an argument; no length specifier
cout << "You entered: " << str << endl;
cout << "Length of string in charr after input: "
<< strlen(charr) << endl;
cout << "Length of string in str after input: "
<< str.size() << endl;
return 0;
}
为什么输出会显示为如下结果,有谁能解释吗?
Length of string in charr before input: 23
Length of string in str before input: 0
Enter a line of text:
hello
You entered: hello
Enter another line of text:
whar's your name
here
You entered: whar's your name
Length of string in charr after input: 5
Length of string in str after input: 16
为什么运行getline(cin, str);的时候,要输入两行并且显示出来的是第一行的内容??有谁能帮我解释一下吗?