Problem with function GETLINE

Write your question here.
I am practicing inputing string from the keyboard. I use function getline to get them. However my code seems not working properly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #include <iostream>
  #include <string>
  using namespace std;
// struct type person
struct person {
	string name;
	int age;
	string address;
};
int main() {
	person ps;
	string address;
	cout << "The name: "; getline(cin, ps.name);
	cout << "The age: "; cin>> ps.age;
	cout << "The address: "; getline(cin, ps.address);
	cout << "The name is: " << ps.name << endl;
	cout << "The age is: " << ps.age << endl;
	cout << "The address is: " << ps.address << endl;
}

The second getline doesn't work. I can't input a string here.
The following is a screenshot of the code when it runs:
https://drive.google.com/open?id=0BxDjLP_mwwuWVGc4YU94MFEzeEU
Last edited on
cin>> leaves a \n in the buffer, so getline immediately reads that \n, as if you had simply pressed enter.

http://www.cplusplus.com/forum/general/51433/

Thank you very much!
Topic archived. No new replies allowed.