MCPcopy Create free account
hub / github.com/Apress/beginning-cpp20 / date

Function date

Exercises/Modules/Chapter 08/Soln8_01.cpp:63–80  ·  view source on GitHub ↗

Reads in the date in the given month and year

Source from the content-addressed store, hash-verified

61
62// Reads in the date in the given month and year
63int date(int month_number, int year)
64{
65 const int date_min {1};
66 const int feb {2};
67
68 // Days in month: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
69 static const int date_max[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
70 // With the above array declared as static, it will only be created the first
71 // time the function is called. Of course, this doesn't save anything in this
72 // example as we only call it once...
73
74 // Feb has 29 days in a leap year. A leap year is a year that is divible by 4
75 // except years that are divisible by 100 but not divisible by 400
76 if (month_number == feb && year % 4 == 0 && !(year % 100 == 0 && year % 400 != 0))
77 return validate_input(date_min, 29, "a date");
78 else
79 return validate_input(date_min, date_max[month_number - 1], "a date");
80}
81
82// Select the ending of the ordinal day number
83std::string ending(int date_day)

Callers 1

mainFunction · 0.70

Calls 1

validate_inputFunction · 0.70

Tested by

no test coverage detected