help with structures

Hello, my issue is when i go to input the data such as, address and phone number, when i use a space in between the house number and street name it skips to the next question. ive tried getline(), i.e getline(cin, info[i].street_address), however this does not work. How can i use spaces in my inputs for my program?

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
40
41
42
43
44
45
46
47
48
49
50
51
52



#include <iostream>
#include <string>

using namespace std;

// structure declaration

struct addressBook
{

	string name;
    string street_address;
	string phone;


};

int main()
{

	addressBook info[2];

	for (int i = 0; i < 2; i++)
	{

		cout << "Please enter a name: " << "\n";
		cin >> info[i].name;
		cout << "Please enter the address for " << info[i].name << "\n";
		cin >> info[i].street_address;
		cout << "Please enter the phone number for " << info[i].name << "\n";
		cin >> info[i].phone;


	}

	for (int i = 0; i < 2; ++i)
	{

		cout << info[i].name << "\nAddress: " << info[i].street_address <<  "\nPhone#: " << info[i].phone << "\n\n";

	}




	cout << "\n\n";
	system("PAUSE");
	return 0;
}
This is not about struct, it's about the cin. By default the cin use space as delimiter, so you can only get 1 word for a >> call.

You can change the delimiter, but I don't think it could solve your case.

I don't know how did you use getline, but it should work. Try this:

1
2
3
getline(cin, info[i].name);
getline(cin, info[i].street_address);
getline(cin, info[i].phone);


Don't mix >> with getline, if you have to, you'll need to flush the stream (there're posts about this in this site) before use getline.
sir/mam, thank you for your reply. You are correct. When i tried using the getline() i was still running into the same problem, upon further investigation, however, it was the compiler giving me the issues. For some reason it was not recognizing the fixed code and compiling the old code using the cin >>. Thanks again.
Topic archived. No new replies allowed.