Write a commented C program to reformat dates Prompt the use
Write a commented C++ program to re-format dates. Prompt the user to input a date in the form:
Display the date in each of the following formats:
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.
Furthermore, Write commented C++ code which will use the strings extracted in the code you wrote 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.
Solution
Have user input date in numeric format mm dd yyyy.
2. Open \"answer.txt\" file for output.
. Use swicth statement for months
. Example: (month) case 1: if (day > 1 && day < 31) outAnswer << \"January\"
*/
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream outAnswer; // Output to a file
outAnswer.open(\"answer1.doc\"); // Saves output to txt file
cout << \"Enter date in numerical format separated by spaces:\";
// month calculations
int month, day, year; // integer variables
char month1, day1; //output variables
cin >> month, day, year; // user inputs numeric date format
switch(month)
{
case 1: if (day <=31)
outAnswer << month1 << \"January\";
else outAnswer << month1 << \"invalid # january days\";
break;
case 2: if ((day >= 1) && (day <=28)) {
outAnswer << month1 << \"Febuary\";
}
else outAnswer << month1 << \"invalid # Febuary days\";
break;
case 3: if ((day >= 1) && (day <=31)) {
outAnswer << month1 << \"March\";
}
else outAnswer << month1 << \"invalid # March days\";
break;
case 4: if ((day >= 1) && (day <= 30)) {
outAnswer << month1 << \"April\";
}
else outAnswer << month1 << \"invalid # April days\";
break;
case 5: if ((day >= 1) && (day <= 31)) {
outAnswer << month1 << \"May\";
}
else outAnswer << month1 << \"invalid # may days\";
break;
case 6: if ((day >= 1) && (day <=30)) {
outAnswer << month1 << \"June\";
}
else outAnswer << month1 << \"invalid # of June days\";
break;
case 7: if ((day >= 1) && (day <= 31)) {
outAnswer << month1 << \"July\";
}
else outAnswer << month1 << \"invalid # of july days\";
break;
case 8: if ((day >=1) && (day <= 31)) {
outAnswer << month1 << \"August\";
}
else outAnswer << month1 << \"invalid # of August days\";
break;
case 9: if ((day >= 1) && (day <= 30)) {
outAnswer << month1 << \"September\";
}
else outAnswer << month1 << \"invalid # of September days\";
break;
case 10: if ((day >=1) && (day <= 31)) {
outAnswer << month1 << \"October\";
}
else outAnswer << month1 << \"invalid # of October days\";
break;
case 11: if (( day >=1) && (day <= 30)) {
outAnswer << month1 << \"November\";
}
else outAnswer << month1 << \"invalid # of November days\";
break;
case 12: if (( day >=1) && (day <=31)) {
outAnswer << month1 << \"December\";
}
else outAnswer << month1 << \"invalid # of December days\";
break;
default : outAnswer << month1 << \"invalid Month #\";
break;
}
if (day == 1 || day == 21 || day ==31)
outAnswer << day1 << \"st,\";
else if (day == 2 || day == 22)
outAnswer << day1 << \"nd,\";
else if (day == 3 || day == 23)
outAnswer << day1 << \"rd,\";
else
outAnswer << day1 << \"th,\";
outAnswer << month1 << day << day1 << year << endl;
return 0;
}


