Problem with strings!

I have this code and whenever I enter a name that is a single word, everything works correctly. However! Whenever I enter a first and last name,
e.g. "Joe Bloggs"

The console stores the second word "Bloggs" as the second piece of data and skips the next cout and cin.

Why is this?! Here is my code!

(Yes I know that age should be an int, but it's easier to see what I mean if it's a string.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
using namespace std;

int main(){
	string name; //Defines the string name
	

	cout<< "Hello! What's your name? "; //Prints message to screen
	cin>>name; //Stores user inputted data into variable name

	string age;

	cout<<endl<<"Hi "<<name<<" how old are you? "; //Prints message and variable value to screen
	cin>>age;
	cout<<endl<<"So, "<<name<<" you are "<<age<<" years old."<<endl;

	system("PAUSE"); //Press any key to continue...
	return(0);
}
cin reads as far as the first blank space and leaves the rest in the buffer for the next cin. Use getline if you want blank spaces in your input.
Ah thanks!
Topic archived. No new replies allowed.