Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reads a string from the user containing a date in the forma

ID: 3763076 • Letter: W

Question

Write a program that reads a string from the user containing a date in the format ( mm/dd/yyyy. )

You program should print the date in the form: November 17,2011.

Additional Program requirements:

1. You are to define and use a list of month names to convert month number input to string name. i.e. month_list = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]

Extra Credit: Your program handles any IndexError: list index out of range exceptions, but displaying a message. (using exception handling mechanisms.)

Hints: I recommend that you use the split method on the input date string to create a list of numbers between / marks. i.e. #split date_string date_list = date_string.split("/")

Explanation / Answer

#include <iostream>
#include <string>

int main()
{
const int NMONTHS = 12 ;
const std::string month[NMONTHS] = { "January", "February", "March", "April", "May","June", "July", "August", "September", "October", "November", "December" };
std::cout << "Enter a date in the form MM/DD/YYYY: ";
int mon;
int day;
int year;
char slash, slash2 ;
if( std::cin >> mon >> slash >> day >> slash2 >> year &&slash == '/' && slash2 == '/' &&mon > 0 && mon <= NMONTHS && day > 0 && day < 32 /* more validations */ )
{
std::cout << month[mon-1] << ' ' << day << ", " << year << ' ' ;
}
else std::cerr << "error in input " ;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote