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?
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;
}
}
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
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 = newint[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.
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.
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;
}
}
}