# day number in yaer

• 05-23-2008
azsquall
day number in yaer
I'm conducting self-study C++ and currently reaching functions (void, returning-value function, etc).
the requirement is that users will input date in form of "month day year" like 12 25 2002 and the output will show that date's day number, which is day 359 ( 1 1 2002 to be the reference, or day 1).
I'm done with this, but I'm wondering is there any more efficient way to determine how many days in a month

I used this function
Code:

```int daysinMonth(int INmonth, int INyear) //Calculate days in months. {                 switch(INmonth)         {                 case(1): case(3): case(5): case(7):                 case(8): case(10): case(12):                         return 31;                         break;                 case(2):                         if (isLeapYear(INyear))                                                         return 29;                         else                                 return 28;                         break;                 case(4): case(6): case(9): case(11):                         return 30;                         break;         } }```
I've been thinking about a loop but don't know how to alternate return value between 30 and 31

Any insight will be so great!
thanks!
• 05-23-2008
Daved
Looks good to me. I wouldn't worry too much about efficiency here beyond not doing anything horribly inefficient. Worry more about clarity. For example, perhaps use an enum for the months so it is clearer which months are in which case.

BTW, do you check for an invalid month elsewhere in the code?
• 05-23-2008
azsquall
no..i din't implement that.
Will try.
I have not reached enum type yet! ;)

thanks!
• 05-23-2008
cyberfish
Just a thought -

Code:

```daysInMonth_noleap[12] = { 31, 28, 31 .... }; daysInMonth_leap[12] = { 31, 29, 31 .... };```
Remember to make it 0 - 11 (January = 0, December = 11).
• 05-24-2008
azsquall
I thought abt it too.
It surely will be better. I'm getting there yet, however ;)
thanks
• 05-24-2008
matsp
I've use a hybrid of the two:
Code:

```// This function assumes that month and year are correct values. // Month is 1..12! int daysInMonth(int month, int year) {     static const int dayArray[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };     int days = dayArray[month-1];     if (month == 2 && leapYear(year))       days = 29;     return days; }```
The reason for the hybrid is that the extra check of month == 2 is probably no bigger than the overhead of the second array, and you still need an if-statement anyways.

Edit: Fix up bug with off-by-one.
• 05-24-2008
Salem
> and you still need an if-statement anyways.
Perhaps
return days + (month == 2 && leapYear(year));
since the result of a boolean expression is 0 or 1

> but I'm wondering is there any more efficient way to determine how many days in a month
Maybe, but there are plenty of increasingly obscure (see above) shorter answers.

```enum {   January = 1, February, March, April, // you get the idea }; int daysInMonth ( int month, int year ) {   int result = 0;   switch ( month ) {     case January: case March:       result = 31;       break;     // and so on     default:     // error trap   } }```