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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#include <iostream>
#include <iomanip>
using namespace std;
int print_month(int num_day, int start_day)
int main(void)
{
char month_length[12] ={31,28,31,30,31,30,31,31,30,31,30,31}; //non leap year
char month_leap[12]= {31,29,31,30,31,30,31,31,30,31,30,31}; //leap year
char month_names[12][10] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};//
int blanks; // number of blank spaces before first day
int j; // index for looping through each day
int valid_input = 1; // flag to track whether valid input is obtained
int leap_year; // if it is a leap year or if it is not
int day_year; // what day the year starts on
cout << "\nIs the year a leap year (0=No, 1=Yes)";
cin >> leap_year;
if (leap_year <0 || leap_year >1)
{
cout << "The number for a leap year is incorrect.\n";
cout << "Is the year a leap year (0=No, 1=Yes)";
cin >> leap_year;
if (leap_year < 0 || leap_year > 1)
{
valid_input = 0;
}
}
if (valid_input)
{
cout << "Enter day of week on which year starts (1=Sun, 7=Sat): ";
cin >> day_year;
if (day_year < 1 || day_year > 7)
{
cout << "\nThe day the year starts on must be a choice of one of these options:\n";
cout << " 1 for Sunday\n 2 for Monday\n 3 for Tuesday\n 4 for Wednesday\n 5 for Thursday\n 6 for Friday\n 7 for Saturday\n";
cout << "Enter day the year starts on: ";
cin >> day_year;
if (day_year < 1 || day_year > 7)
{
valid_input = 0;
}
}
} // if both input numbers are valid, then display calendar
for (j=0; j<12; j++)
{
cout << month_names[j] << endl;
print_month (num_days[j], start_day);
}
return 0;
}
int print_month(int num_day, int start_day)
{
blanks = day_year - 1; // Find how many blank spaces to insert before the first day
cout << "\nSun Mon Tue Wed Thu Fri Sat\n\n"; // Display days
if (blanks > 0)
{
for(i=0;i<blanks;i++)
{
cout<<"";
}
}
for (i = 1; i <= 31; i++)
{
cout << setw(3) << right << i; // format i right-aligned space 3 wide
if ((i + blanks) % 7 == 0)
{
cout << "\n";
}
else
{
cout << " "<< endl;
}
else // if input isn’t valid, then display message
{
cout << "The input obtained was invalid and can’t be used to print a calendar.\n";
}
return (start_day);
}
|