Using a structure inside another structure

I am trying to create a program that uses two structures, one for the date and one to list events. The prototype will only track 5 events. I made the structures and figured out how to embed the DATE structure into the EVENT structure but my issue is with keeping the data separate. When a use enters data, how to I assign it to a specific event (like event1 and event2). I am also getting an error on the last line between the COUT and the DATA>EVENTDATE.


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

struct Event
	{
		char location[20];
		char information[20];
		Date eventDate;
	}event1, event2, event3, event4, event5;

void printEvent (Event events);

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


	cout << data.location << endl;
	cout << data.information << endl;
	cout << data.eventDate << endl;
You are getting an error on the last line because you didn't define how an ostream should output Date using the << operator. You can use

cout << data.eventDate.year << ' ' << data.eventDate.month << ' ' << data.eventDate.day << endl;

Or you could do the same thing in an overloaded operator<< function.
Line 13 and 19 are different ways of doing the same thing

you can write it

} event1, event2, event3, event4, event5, data;
under the class

or
Manually write each one out in main like this
Event data;
Event event1;
....
Event event5

or Event event1, event2, event3, event4, event5, data;

Last edited on
Thanks for the help! It is really appreciated.

So it should really be like this and I could continue adding event3 and event4 and so on right?


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
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 >> data.location;
	cout << "Enter any event information (20 char or less)" << endl;
	cin >> data.information;
        cout << "Enter the date of your event (month, date, year)" << endl;
        cin >> data.eventDate.month >> ' ' >> data.eventDate.day >> ' ' >> data.eventDate.year >> ' ' >>endl;

	Event event2;
	cout << "Enter the location of your event (20 char or less)" << endl;
	cin >> data.location;
	cout << "Enter any event information (20 char or less)" << endl;
	cin >> data.information;
        cout << "Enter the date of your event (month, date, year)" << endl;
        cin >> data.eventDate.month >> ' ' >> data.eventDate.day >> ' ' >> data.eventDate.year >> ' ' >>endl;
	
Why do you need to have them separately as event1, event2, event3, event4, event5, instead of just creating an array?

At any rate, all your input statements are wrong. You don't have variable Event data; anymore so stop using data. Also, remove the space characters on lines 24 and 32.
Thanks FG, I forgot to change that. I am using structures since I have different data types
Topic archived. No new replies allowed.