• 0

Number days in the month


Question

7 answers to this question

Recommended Posts

  • 0

I don't see any point in making a program like that, but if u already knew how many days were in each months, you could make a menu (with the format like 1.January,ect), then use cin to get a number and then use a if else statement, to display how many days in a month.

Link to comment
Share on other sites

  • 0
Write an array:

int static iaMonths[] = {

0,

30,

30,

30,

.

.

.

30

};

Then refer to each month by number

That won't work for every year. You could do it that way, but you would have to account for leap years. A year is a leap year if it is divisible by 4, unless it is divisible by 100 and not by 400.

So, something like:

int getDays(int month, int year) {
    if(year%4 == 0 && month == 1 && !(year%100 == 0 && year%400 != 0))  // Asssuming January begins at index 0.
         return 29;
    else {
         return daysInMonth[month]  // Assuming you had an array setup as in the quote.
    }
}

Edited by Miran
Link to comment
Share on other sites

  • 0

I think you need to use % and not / in the above code.

If you want the weekday that the month starts on you can use strftime. Specify a date, f.e., September 1, 2000, and you use the "%w" format. You will get the day number in the string, which you can use with atoi to get the actual integer day.

Edited by Andareed
Link to comment
Share on other sites

  • 0
I think you need to use % and not / in the above code.

If you want the weekday that the month starts on you can use strftime. Specify a date, f.e., September 1, 2000, and you use the "%w" format. You will get the day number in the string, which you can use with atoi to get the actual integer day.

You're right - it should be %. I'll change it.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.