0

Formatting input for dates in C Program

I am trying to write a program that accepts two different formats for dates Month DD, YYYY and MM/DD/YY but then convert it to Month DD, YYYY where DD and YYYY are ints and can be worked with inside the program. Anyone do this before or have any suggestions? For previous program I used scanf("%s %d, %d, month, day, year) and limited the input to that format. I want to open it up to different formats now but consolidate in early stage to the one I scanned for prior. Thanks for your help or comments.

14th Jan 2020, 10:29 PM
Shay Christensen
Shay Christensen - avatar
5 Respostas
+ 4
Do you have a code along this idea? I'd recommend to share the code link with the Description. Without having a look none of us here knows what you have tried, or where to go next.
14th Jan 2020, 11:43 PM
Ipang
+ 3
I was thinking that you can first check, whether there is any slash / in the input string. If there exists a slash, extract the values from the string using "%d/%d/%d" format. Otherwise extract the values from the string using "%s, %d %d" format. As Coder Kitten said, to accomplish this you can do value extraction using `sscanf`, although, you are free to choose which function to use for reading the raw string, before value extraction of course 😁 * Take care to make sure the input buffer is big enough to contain value of month, day and year including the possible extra characters user may write in their input (the / or comma between values).
15th Jan 2020, 7:49 AM
Ipang
+ 1
sure the following works as long as user enters date in that format but i want to have the option of accepting MM/DD/YYYY format and converting it inside my code to work with it like the one in my code above. is there a way to scan verify and scan again? https://code.sololearn.com/c8gv97r2NRK9/?ref=app
15th Jan 2020, 2:52 AM
Shay Christensen
Shay Christensen - avatar
+ 1
sscanf is exactly what i needed! Great suggestion! thank you to all who helped!
15th Jan 2020, 6:48 PM
Shay Christensen
Shay Christensen - avatar
+ 1
Here is what I came up with and it will work perfect for my purposes. https://code.sololearn.com/cbV21hvRuT1X
16th Jan 2020, 12:48 AM
Shay Christensen
Shay Christensen - avatar