I am writing my first program using strings. In the program, I need to read in the date in a specific format and then do a whole bunch of stuff with that information.
However, I am stuck very early on. I need to write a function that will takes the 1 string containing the date and, split it into 3 separate strings: one for the month, one for the day, one for the year.
For example, if I was to enter 5/10/13, it would set month to 5, day to 10, and year to 13
I decided the easiest way to do this was to create 3 substrings out of it, but that wasn't yielding proper results. So, instead, I decided to try getting the first substring, then erasing that part of the string, getting another, erasing, etc.
Full program:
http://pastebin.com/ezHwieuq
The function in question:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
void breakoriginaldate (string origdate, string &month, string &day, string &year)
{
int slash;
slash=origdate.find("/",0);
month=origdate.substr(0,slash);
cout<<month<<" is the month"<<endl;
origdate.erase(0, slash);
slash=origdate.find("/",0);
day=origdate.substr(0,slash);
cout<<day<<" is the day"<<endl;
origdate.erase(0, slash);
year=origdate;
cout<<year<<" is the year"<<endl;
return;
}
|
Getting the month out of it is simple, but I cannot figure out the proper way to get the day and year.