Skip to content

Instantly share code, notes, and snippets.

@rhysforyou
Created March 22, 2012 04:10
Show Gist options
  • Select an option

  • Save rhysforyou/2155817 to your computer and use it in GitHub Desktop.

Select an option

Save rhysforyou/2155817 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
int main()
{
int day = -1;
char mon1, mon2, mon3; // The three letters for the name of the month
int year = -1;
cout << "Input a date formatted like DD-MMM-YYYY" << endl;
// Process the user's input
cin >> day;
cin.ignore(1, '-');
cin.get(mon1);
cin.get(mon2);
cin.get(mon3);
cin.ignore(1, '-');
cin >> year;
if (day == -1 || year == -1)
{
cerr << "bad input" << endl;
return -1;
}
if (day < 1 || day > 31)
{
cerr << "Invalid day entered" << endl;
return -1;
}
if (mon1 < 'A' || mon1 > 'Z')
{
cerr << "Invalid month entered" << endl;
return -1;
}
cout << "Valid date entered" << endl;
return 0;
}
puts (gets =~ /[0-3]\d-[A-Z]{3}-\d{4}/) ? "valid" : "invalid"

The Task

Implement a C++ program that reads eleven (11) characters and verifies if a sequence of characters is consistent with a date pattern DD-MON-YYYY where DD is a number of a day in a month, MON is an abbreviation of a month name and YYYY is a year number.

For example a sequence of characters 12-DEC-2011 is consistent with the pattern. A sequence 45-JUN-2000 is not consistent with the pattern because June does not have 45 days. Of course a sequence of characters AB-FEB-@@@@ is not consistent with the pattern because of the obvious reasons.

@rhysforyou
Copy link
Copy Markdown
Author

WHICH ONE IS SIMPLER? CAN YOU TELL?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment