Question: Write a commented C++ program to re-format dates. Prompt the user to input a date in the form: Sunday, January 2, 2010. Display the date

Write a commented C++ program to re-format dates. Prompt the user to input a date in the form:

Sunday, January 2, 2010. 

Display the date in each of the following formats:

January 2 was a Sunday in 2010 Sun, Jan 2 10 2010-01-02 

(a)

Write commented C++ code which will use getline(cin,someString) to get a string from the user (in the form shown above), and then extract the day, month, date, and year into separate string variables. To do this you will have to search the input string for substrings or characters that will uniquely identify each portion of the string you plan to extract, in order. Test your code with various different dates to make sure the extraction process is working properly.

(b)

Write commented C++ code which will use the strings extracted in part (b) to write out the date in the formats given. You will need to employ substring functions for the second format. For the third format, you will need to convert from the month in string format to the numerical value for that month; for example,January should be converted to 01. In order to accomplish this, you are provided with the file months.txt containing the months and their corresponding numbers, for example 01January 02February etc. Use getline(yourFile,yourString) to read in a string from the file, and use the yourstring.find() function to search for the month and extract the corresponding number which immediately precedes it.

For format 3, you should also make sure that single digit days or months are formatted in a width field of 2, that is, your code should output "02" instead of "2" for the second of January.

(c)

Test your program by trying a number of different input combinations, including different months, days, and days of the week. See what happens if the user inputs non-well-formed input, for example if the user forgets one part of the input, adds or forgets commas or spaces (or writes "May 5th" for example), enters Blurnsday, Decembruary 39, 20112 or even something like This program isnt working for me, help!

(d)

Speculate on how you might check to make sure the input is well-formed, how to handle non-well formed input, and even how to ensure that the day of the week is correct.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!