#include <cassert>
#include <iostream>
#include <sstream>

int char_2_month( char second_, char third_ );

int main(){
    const int PART_MONTH = 0;
    const int PART_DAY = 1;
    const int PART_YEAR = 2;

    for (;;) { // forever
        //////////////////////////////////////////////////
        // Initialize local variables
        //////////////////////////////////////////////////
        std::stringstream buffer_month; // temp buffer to accumulate month-characters
        std::stringstream buffer_day;   // temp buffer to accumulate day-characters
        std::stringstream buffer_year;  // temp buffer to accumulate year-characters
        int onechar;                    // single character from the standard input
        int part = PART_MONTH;          // assume input starts with the month

        //////////////////////////////////////////////////
        // Get user input and store it as separate parts:
        //////////////////////////////////////////////////
        std::cout << "Enter date: ";
        while ( ( onechar = std::cin.get() ) != EOF ) {
            // For every character:
            if ( onechar == '\n' ) {
                // date input is complete, stop the input:
                break;

            } else if ( onechar == 'Q' || onechar == 'q' ) {
                // user wants to exit program:
                return 0;

            } else if ( isspace( onechar ) ) {
                // whitespace separates date parts:
                ++part;

            } else if ( ispunct( onechar ) ) {
                // a punctuation character separates date parts:
                ++part;

            } else if ( isalpha( onechar ) ) {
                // alphabetic character can only be part of month:
                buffer_month << char( onechar );

            } else if ( part == PART_MONTH ) {
                // assume this character belongs to the month part:
                buffer_month << char( onechar );

            } else if ( part == PART_DAY ) {
                // assume this character belongs to the day part:
                buffer_day << char( onechar );

            } else if ( part == PART_YEAR ) {
                // assume this character belongs to the year part:
                buffer_year << char( onechar );
            }
        }//while


        //////////////////////////////////////////////////
        // Convert date to the integer representation:
        //////////////////////////////////////////////////
        int Month = 0;
        int Day = 0;
        int Year = 0;

        //////////////////////////////////////////////////
        // Interpret month part of the input:
        //////////////////////////////////////////////////
        int position = 0;
        while ( ( onechar = buffer_month.get() ) != EOF ) {
            ++position; // where are we?
            if ( isalpha( onechar ) ) {
                // Alphabetical month name was entered:
                char month_2nd = buffer_month.get(); // Second character entered
                char month_3rd = buffer_month.get(); // Third character entered
                Month = char_2_month( month_2nd, month_3rd );
                break;

            } else if ( position == 1 ) {
                // assume single-digit month was entered:
                Month = onechar - '0';

            } else if ( position == 2 ) {
                // assume two-digit month was entered:
                Month = Month * 10 + onechar - '0';
                break; // numeric month can have only two digits
            }
        }//while

        //////////////////////////////////////////////////
        // Since year and day are numeric, use formatted
        // input to convert to integer representation:
        //////////////////////////////////////////////////
        buffer_day >> Day; 
        buffer_year >> Year; 

        std::cout << "You entered: " << Month << '/' << Day << '/' << Year; 
        std::cout << std::endl; 
        std::cout << std::endl; 

    }//for
}

// converts second and third characters of the month name
// to an integer month:
int char_2_month( char second_, char third_ )
{
    switch( tolower( second_ ) + tolower( third_ ) )
    {
    case 'a' + 'n': return 1;    // Jan
    case 'e' + 'b': return 2;    // Feb
    case 'a' + 'r': return 3;    // Mar
    case 'p' + 'r': return 4;    // Apr
    case 'a' + 'y': return 5;    // May
    case 'u' + 'n': return 6;    // Jun
    case 'u' + 'l': return 7;    // Jul
    case 'u' + 'g': return 8;    // Aug
    case 'e' + 'p': return 9;    // Sep
    case 'c' + 't': return 10;   // Oct
    case 'o' + 'v': return 11;   // Nov
    case 'e' + 'c': return 12;   // Dec
    default:
        // bad month...
        return 0;
    }
}