I'm working on a project where I have to read a date to make sure that it's a valid date. For example, February 29th is only a valid date on leap years, or June 31st is not a valid date, so the computer would output that information based on the input. My issue is that I can't figure out how to parse the string so that the user can enter "05/11/1996" as a date (for example) and then take that and put it into seperate integers. I was thinking about trying to do something with a while loop and string stream, but I'm a little stuck. If someone could help me with this, I would really appreciate it.
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Correctly parse PDF paragraphs with Python
- Why does const allow implicit conversion of refere
- how to split a list into a given number of sub-lis
相关文章
- Angular Material Stepper causes mat-formfield to v
- JSP String formatting Truncate
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- Why doesn't Django enforce my unique_together
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
Another option is to use
std::get_time
from the<iomanip>
header (available since C++11). A good example of its use can be found here.A possible solution might be also based on
But sincestrptime
, however note that this function only validates whether the day is from the interval<1;31>
and month from<1;12>
, i.e."30/02/2013"
is valid still:strptime
is not always available and additional validation would be nice, here's what you could do:struct tm
i.e.:
used as:
which in this case would output
date is invalid
since normalization would detect that29/02/2013
has been normalized to01/03/2013
.I'd prefer to use Boost DateTime:
See it Live on Coliru
Outputs:
If the format is like in your example, you could take out the integer like this:
where of course in buffer you have the date ("05/11/1996" )