Define a structure member that is inside another structure

I am trying to create one structure for DATE and a second one for EVENT. I want to link the DATE structure inside the EVENT structure. I think I have that part figured out but when I try and define the date member using a CIN I cant figure out how. I either did not link DATE inside EVENT properly or I dont know how to assign it using CIN. I continue to get errors in the last line (23) when I try and assign a date but I dont see an error in my structures which leads me to believe I am just not assigning it correctly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  struct Date
	{
		int day;
		int month;
		int year;
	};

struct Event
	{
		char location[20];
		char information[20];
		Date eventDate;
	};

void main()
{
	Event event1;
	cout << "Enter the location of your event (20 char or less)" << endl;
	cin >> event1.location;
	cout << "Enter any event information (20 char or less)" << endl;
	cin >> event1.information;
	cout << "Enter the date of your event using month/date/year (01/01/2015)" << endl;
	cin >> event1.Date.eventDate;


Duh! OK, in looking at my post I seem I made a huge mistake and tried to assign three members (day, month and year) using only Date.eventDate. Let me fix this and try it again before anyone tries to help. I will post back when I am finished. Seeing the code int he forum really helped me to see this.
Last edited on
Your line 23 has syntax error. The event has member eventDate. No "Date".

You should use std::string rather than those character arrays. Even if you tell the user to use 20 characters or less, the user can disobey and then you have memory corruption.

You could override the operator>> for struct Date. Then you could continue to use the cin >> event1.eventData; syntax; the details would hide in the implementation of the operator.

Likewise, you could override operator>> for the struct Event. That would reduce the code in main() to:
1
2
Event event1;
cin >> event1;
OK, I think I fixed everything but I did not link the EVENT member "eventDate" with the structure DATE. Any suggestions?

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
struct Date
	{
		int day;
		char month[3];
		int year;
	};

struct Event
	{
		char location[20];
		char information[20];
		string eventDate;
	};



void main()
{
	Event event1;
	cout << "Enter the location of your event (20 char or less)" << endl;
	cin >> event1.location;
	cout << "Enter any event information (20 char or less)" << endl;
	cin >> event1.information;

	Date date1;
	cout << "Enter the day of your event using two digits " << endl;
	cin >> date1.day;
	cout << "Enter the month of your event using three letters " << endl;
	cin >> date1.month;
	cout << "Enter the year of your event using four digits " << endl;
	cin >> date1.year;

	cout << event1.location << endl;
	cout << event1.information << endl;
	cout << date1.month << '/' << date1.day << "/" << date1.year << endl;

}
Last edited on
Why don't you just store the info into the eventDate like:
cin>>event1.eventDate.day;

Aceix.
because I'm an idiot... Thanks, that worked!
Topic archived. No new replies allowed.