flush cin line

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.

http://www.opengroup.org/onlinepubs/009695399/functions/strptime.html
http://www.cplusplus.com/reference/clibrary/ctime/strftime/

Otherwise, use the Boost DateTime library to parse and format dates.

http://www.boost.org/doc/libs/1_43_0/doc/html/date_time.html
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.
All you need are the standard I/O functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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.

Hope this helps.
Topic archived. No new replies allowed.