stop array from accepting more than one value at once

I'm having trouble with this struct array.. The first time it asks for an input, if the person were to type "1 2 3 4" it would have entered the input for each part of the array and skip to line 29 and display the final output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <sstream>
#include <string>
using namespace std;


int main() 
{
struct Person
{
string name;
string color;
};
Person person[2];
int i = 0;
	while (i < 2)
	{

		cout << "name: ";
		cin >> person[i].name;

		cout << "color:";
		cin >> person[i].color;
		i = i + 1;
	}

		cout << endl;
			i =0; 
			while (i <=1)
			{
					cout << person[i].name << " " << person[i].color <<  endl;
					i = i + 1;
			
		}
	return 0;
}


does this work?
Very new to structs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
int main()
{
    struct Person
    {
        string name;
        string color;
    };
    Person person[2];
    int i = 0;
	while (i < 2)
	{

		cout << "name: ";
        getline(cin, person[i].name, '\n');

		cout << "color:";
        getline(cin, person[i].color, '\n');
		i = i + 1;
	}

    cout << endl;
    i =0;
    while (i <=1)
    {
        cout << person[i].name << " " << person[i].color <<  endl;
        i = i + 1;
        
    }
	return 0;
}
Excellent ! Thankyou so much!
Topic archived. No new replies allowed.