1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#include <iostream>
using namespace std;
//======================================================================
int input( const char *msg, int low, int high )
{
int value;
bool ok = false;
while ( !ok )
{
cout << msg << " ";
cin >> value;
ok = ( value >= low && value <= high );
if ( !ok ) cout << "ERROR!" << endl;
}
return value;
}
//======================================================================
int main()
{
string monthNames[1+12] = { "", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; // ignore [0] element
int monthDays [1+12] = { 0, 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 };
string dayNames [1+7 ] = { "", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
int jan1 = 0, month = 0, day = 0;
jan1 = input( "Which day of the week is Jan 1st? (1=Sunday, 7=Saturday)", 1, 7 );
month = input( "Which month would you like? (1=January, 12=December)", 1, 12 );
if ( month >= 2 ) // deal with leap year
{
char ans;
cout << "Is it a leap year? (y/n) ";
cin >> ans;
if ( ans == 'y' || ans == 'Y' ) monthDays[2]++;
}
day = input( "Which day of the month?", 1, monthDays[month] );
int test = jan1 + day - 1;
for ( int i = 1; i < month; i++ ) test += monthDays[i];
test = ( test - 1 )%7 + 1; // between 1 and 7, not 0 and 6
cout << day << " " << monthNames[month] << " is a " << dayNames[test];
}
|