String in a Struct wtf

Why is it when I put a space when entering Person1's name the program screws up?

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

int main()
{
	struct Person{
		string name;
		int age;
		double weight;
	} Person1;

	cout << "Person1, what is your name? ";
	cin >> Person1.name;
	cout << "What is your age? ";
	cin >> Person1.age;
	cout << "What is your weight? ";
	cin >> Person1.weight;
	cout << endl;

	cout << "Person1 is " << Person1.name << " who is " << Person1.age << " years of age, and weighs " << Person1.weight << " pounds." << endl;

        return 0;
}
Last edited on
Probably because when reading a string, the extraction operator will read just one word.
To read the entire line, use:

std::getline (std::cin,Person1.name);
Topic archived. No new replies allowed.