I want write a simple program that gets a date from the user in the form
dd/mm/yy and flush the forward slashs so i can output the date as
e.g. February 23 2010.
I've been looking around for simple ways to do this but can find anything concise and still simple. I know there is a very simple way to do it however.
Just read the raw date from cin into a string. Use strptime() to parse the time and then strftime() to reformat it. strptime() is part of the POSIX standard, but not a standard part of <ctime>. It's probably there nonetheless. strftime() is part of the C++ standard library.
Thanks, a lot. What I was finding was more complicated than that. I knew there were a couple functions that dealt specifically with this kind of content, but I couldn't remember what they were. Thanks again.
struct my_date_t
{
unsigned day, month, year;
};
istream& operator >> ( istream& ins, my_date_t& date )
{
ins >> date.day;
ins.get(); // skip the '/'
ins >> date.month;
ins.get(); // skip the '/'
ins >> date.year;
if (date.year < 100) date.year += 2000; // adjust two-digit years like '10 to 2010
return ins;
}
ostream& operator << ( ostream& outs, const my_date_t& date )
{
outs << ...; // write the date as you want it to look here...
return outs;
}
These are, of course, very simplistic. Reading and writing date/time information can be a very complicated subject.