/* Date Printer - This program reads a string from the user containing a
date in the form mm/dd/yyyy. It prints the date in the form March 12,
2014. */
#include "Utility.h"
/* Performs various checks to confirm validity of input,
returns the result */
bool checkDate(string);
/* Determines the correct month name,
returns the month name */
string months(int);
/* Inserts month, day and year into the date string,
displays the date */
void printDate(string, int, int);
int main()
{
int month = 0,
day = 0,
year = 0;
string date = " ";
cout << "\n\tDATE PRINTER\n\n"
<< "\tEnter a date in the format 'mm/dd/yyyy' and I will\n"
<< "\tconvert and display it. For example, after entering\n"
<< "\t'01/12/2017', the output will be 'January 12, 2017.'\n\n"
<< "\tPlease enter a date: ";
getline(cin, date);
while (checkDate(date) == false)
{
if (checkDate(date) == false)
{
cout << "\n\tThe date entered is invalid.\n"
<< "\tPlease enter a date: ";
getline(cin, date);
}
}
month = stoi(date.substr(0));
day = stoi(date.substr(3));
year = stoi(date.substr(6));
date = months(month);
printDate(date, day, year);
pauseSystem();
return 0;
}
/* **********************************************************
Definition: checkDate
This function performs the following checks on the string
* Correct length of the date string (This is the only
condition that immediately returns the result in case
the validity check fails)
* Existence of delimiting characters '/'
* Correctness of month, day, year input
* Determins whether the year is a leap year. Based on
the result, the month and day input is validated once
more.
The result is returned.
********************************************************** */
bool checkDate(string date)
{
bool validDate = true;
bool isLeapYear = false;
int month = 0,
day = 0,
year = 0;
char delimiter = '/';
if (date.length() < 10)
{
return validDate = false;
}
if (date.at(2) != delimiter || date.at(5) != delimiter)
{
validDate = false;
}
month = stoi(date.substr(0));
if (month < 1 || month > 12)
{
validDate = false;
}
day = stoi(date.substr(3));
if (day < 1 || day > 31)
{
validDate = false;
}
year = stoi(date.substr(6));
if (year < 1 || year > 2100)
{
validDate = false;
}
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
isLeapYear = true;
}
if (isLeapYear && month == 2 && day > 29)
{
validDate = false;
}
if (isLeapYear == false && month == 2 && day > 28)
{
validDate = false;
}
return validDate;
}
/* **********************************************************
Definition: months
This function determines the name of a month, based on the
integer value passed to it. The name is returned.
********************************************************** */
string months(const int month)
{
const string monthNames[12] = { "January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December" };
return monthNames[month-1];
}
/* **********************************************************
Definition: printDate
This function accepts a string and two int values as
arguments. It inserts day and year into the date string,
and displays the date.
********************************************************** */
void printDate(string date, int day, int year)
{
date.insert(date.length(), " " + to_string(day) + ", " + to_string(year));
cout << "\n\tYour date is: " << date << endl;
}
Friday, April 28, 2017
Programming Challenge 10.13 - Date Printer
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment