Problem description

Write a program that reads a month and year. If the month and year are valid, the program writes a calendar for that month and year in the specified format, and function main returns 0.

Otherwise, the program writes, "Error: invalid date", and function main returns 1.

Hint: In order to print the month body, you need to know which day of the week is the first day of the month and how many days the month has. These problems have been solved in earlier assignments.

Input specification

Input is read from the standard input stream and consists of two integers separated by whitespace.

The first integer, m, represents the month, where [1 <= m <= 12]. The second integer, y represents the year, where [1752 < y].

Output specification

Output is written to the standard output stream.

If the month and year are valid:

  • The first line contains the month name, a space, and the year.
  • The second line consists of the weekday abbreviations { "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su" }, each separated by a space.
  • The calendar body begins on line 3. The calendar body contains the numeric days right-aligned in seven weekday columns. The first column is Monday, the second Tusday, and the last Sunday. Columns are separated by a single space. The first day of the month is positioned in the first row, and the calendar is completed left-to-right, top-to-bottom order.
  • Note: Depending upon the first day of the month and the number of days in the month, the calendar body may be 4-6 lines.
  • All lines are terminated with a new line character

else:

  • A single line of output stating "Error: invalid date", followed by a newline character.

Sample interaction

$echo "2 2021" | ./pa05
February 2021
Mo Tu We Th Fr Sa Su
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
$ echo "8 2021" | ./pa05
Mo Tu We Th Fr Sa Su
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
$ echo "helloooo" | ./pa05
Error: invalid date
$
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.