Direct data to different instances of a structure

I am almost done with this code, I just have one problem holding me back. I want to be able to enter several events into the structure EVENT. At this point I could just continue creating event1, event2 event3 and make it very long and nasty but before I do that, is there a way I can make a loop that will place each new set of data into the next event? I have the loop all created and it works to the point it continues to ask for new events and then stops when the use is done but the only way I can get it to work is by placing the event number in the first line of the loop (line 36).

What I want to know, is there a way of writing this so that each time the loop runs, it gives the data the next number of event so it can keep the events separate?

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

struct Event
	{
		string location;
		string information;
		Date eventDate;
	};


void main()
{
Event event1;
cout << "Enter the location of your event for the first event" << endl;
cin >> event1.location;
cout << "Enter any event information (20 char or less)" << endl;
cin >> event1.information;
cout << "Enter the day of your event using two digits " << endl;
cin >> event1.eventDate.day;
cout << "Enter the month of your event using three letters " << endl;
cin >> event1.eventDate.month;
cout << "Enter the year of your event using four digits " << endl;
cin >> event1.eventDate.year;

char answer;
cout << "Would you like to enter another event? (y or n)" << endl;
cin >> answer;

while (answer == 'y' || answer == 'Y')
	{
	Event event2;
	cout << "Enter the location of your next event" << endl;
	cin >> event2.location;
	cout << "Enter any event information (20 char or less)" << endl;
	cin >> event2.information;
	cout << "Enter the day of your event using two digits " << endl;
	cin >> event2.eventDate.day;
	cout << "Enter the month of your event using three letters " << endl;
	cin >> event2.eventDate.month;
	cout << "Enter the year of your event using four digits " << endl;
	cin >> event2.eventDate.year;

	cout << "Would you like to enter another event? (y or n)" << endl;
	cin >> answer;
	}
}
Last edited on
You can create an array of Event. For example

Event event[5]; Just like any other array, now this holds 5 events between the indexes 0 and 4.

And you can use it just as you use event1,event2. Fore xample

1
2
3
4
5
6
cout << "Enter the day of your event using two digits " << endl;
cin >> event[0].eventDate.day;
cout << "Enter the month of your event using three letters " << endl;
cin >> event[0].eventDate.month;
cout << "Enter the year of your event using four digits " << endl;
cin >> event[0].eventDate.year;


Thats the first event out of 5 that you created. If you wanna fill the next one you just use event[1] etc
Last edited on
That would still require me to have the 5 different set of questions, one for each array number though correct? Or would this allow the next event written to be placed in the next array number?

could I just do the questions like this:

cin << event[].eventDate.day;

and then each time a use put something in that member it would use the next number?
What you're asking for is a dynamic array that you can resize. You cannot do that with arrays. Thats why you use vectors instead of arrays nowadays. But im pretty sure you cant use a vector, so the only little work around is that you start of by just asking the user how many eventdates he wants, and then you create an array based off of that. For example.

1
2
3
int x;
cout << "Hello there, how many numbers would you like to store in you array?" << endl;
cin >> x;


(lets say he entered 7, now you'd have to dynamically allocate memory, like this)

int* arr = new int[x];

And then you can use this array just like in your code, just as normal. But since you dynamically allocated memory for it, you'd have to delete it at the end.

delete[] arr;

If you're not allowed to do this for some reason, then all you can really do is just create a big one. An Event array of say 30. But that would require the user to never exceed 30 events. You can still use it as you want though.
Last edited on
I just threw in the towel and finished it up. I know this can be more efficient and use less code but at this point that solution eludes me. At least it works. I'll just add some stuff at the end to print out the events and its a wrap. Thanks for the help all.

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
struct Date
	{
		int day;
		char month[3];
		int year;
	};

struct Event
	{
		string location;
		string information;
		Date eventDate;
	};


void main()
{
Event event1;
cout << "Enter the location of your event for the first event" << endl;
cin >> event1.location;
cout << "Enter the event information" << endl;
cin >> event1.information;
cout << "Enter the day of your event using two digits " << endl;
cin >> event1.eventDate.day;
cout << "Enter the month of your event using three letters " << endl;
cin >> event1.eventDate.month;
cout << "Enter the year of your event using four digits " << endl;
cin >> event1.eventDate.year;

char answer;
cout << "Would you like to enter another event? (y or n)" << endl;
cin >> answer;

while (answer == 'y' || answer == 'Y')
	{
	Event event2;
	cout << "Enter the location of your second event" << endl;
	cin >> event2.location;
	cout << "Enter any event information" << endl;
	cin >> event2.information;
	cout << "Enter the day of your event using two digits " << endl;
	cin >> event2.eventDate.day;
	cout << "Enter the month of your event using three letters " << endl;
	cin >> event2.eventDate.month;
	cout << "Enter the year of your event using four digits " << endl;
	cin >> event2.eventDate.year;

	cout << "Would you like to enter another event? (y or n)" << endl;
	cin >> answer;

	if (answer == 'y' || answer == 'Y')
		{
		Event event3;
		cout << "Enter the location of your third event" << endl;
		cin >> event3.location;
		cout << "Enter any event information" << endl;
		cin >> event3.information;
		cout << "Enter the day of your event using two digits " << endl;
		cin >> event3.eventDate.day;
		cout << "Enter the month of your event using three letters " << endl;
		cin >> event3.eventDate.month;
		cout << "Enter the year of your event using four digits " << endl;
		cin >> event3.eventDate.year;

		cout << "Would you like to enter another event? (y or n)" << endl;
		cin >> answer;
		}

	if (answer == 'y' || answer == 'Y')
		{
		Event event4;
		cout << "Enter the location of your fourth event" << endl;
		cin >> event4.location;
		cout << "Enter any event information" << endl;
		cin >> event4.information;
		cout << "Enter the day of your event using two digits " << endl;
		cin >> event4.eventDate.day;
		cout << "Enter the month of your event using three letters " << endl;
		cin >> event4.eventDate.month;
		cout << "Enter the year of your event using four digits " << endl;
		cin >> event4.eventDate.year;

		cout << "Would you like to enter another event? (y or n)" << endl;
		cin >> answer;
		}

	if (answer == 'y' || answer == 'Y')
		{
		Event event5;
		cout << "Enter the location of your fifth event" << endl;
		cin >> event5.location;
		cout << "Enter any event information" << endl;
		cin >> event5.information;
		cout << "Enter the day of your event using two digits " << endl;
		cin >> event5.eventDate.day;
		cout << "Enter the month of your event using three letters " << endl;
		cin >> event5.eventDate.month;
		cout << "Enter the year of your event using four digits " << endl;
		cin >> event5.eventDate.year;

		cout << "Would you like to enter another event? (y or n)" << endl;
		cin >> answer;
		}

	}
}
Just a tip, it should beint main() not void main(), its not supported by all compilers so you should probably change that.
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
53
54
55
56
57
58
59
struct Date
	{
		int day;
		char month[3];
		int year;
	};

struct Event
	{
		string location;
		string information;
		Date eventDate;
	};


void main()
{
     std::vector<Event*> events;
     Event* targetedevent;
     bool moreevents = true;
     char answer;


     while(moreevents == true )
     {
          events.push_back(new Event);
          eventint = events.size() - 1;
          targetedevent = events[eventint];



          cout << "Enter the location of event #" << eventint + 1 << ": ";
          cin >> targetedevent->location;

          cout << "Enter the event information: " << endl;
          cin >> targetedevent->information;

          cout << "Enter the day of your event using two digits " << endl;
          cin >> targetedevent->day;

          cout << "Enter the month of your event using three letters " << endl;
          cin >> targetedevent->eventDate.month;

          cout << "Enter the year of your event using four digits " << endl;
          cin >> targetedevent->eventDate.year;
     
          cout << "Would you like to enter another event? (y or n)" << endl;
          cin >> answer;

          if( answer == 'n' || answer == 'N' )
          {
               moreevents = false;
          }


     }

     return 0;
}
Last edited on
Topic archived. No new replies allowed.