marcoo Posted September 23, 2004 Share Posted September 23, 2004 How can I get amount of days in the current month using preferably C++? Link to comment Share on other sites More sharing options...
0 mew2u Posted September 23, 2004 Share Posted September 23, 2004 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 More sharing options...
0 marcoo Posted September 23, 2004 Author Share Posted September 23, 2004 I just want to generate text file like this one attached. So, I have to know total number of days in the month. Calendar.txt Link to comment Share on other sites More sharing options...
0 Andareed Posted September 23, 2004 Share Posted September 23, 2004 Write an array: int static iaMonths[] = { 0, 30, 30, 30, . . . 30 }; Then refer to each month by number Link to comment Share on other sites More sharing options...
0 Miran Posted September 23, 2004 Share Posted September 23, 2004 (edited) 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 September 23, 2004 by Miran Link to comment Share on other sites More sharing options...
0 Andareed Posted September 23, 2004 Share Posted September 23, 2004 (edited) 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 September 23, 2004 by Andareed Link to comment Share on other sites More sharing options...
0 Miran Posted September 23, 2004 Share Posted September 23, 2004 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 More sharing options...
0 marcoo Posted September 23, 2004 Author Share Posted September 23, 2004 Thank you guys. I'll use it. :) Link to comment Share on other sites More sharing options...
Question
marcoo
How can I get amount of days in the current month using preferably C++?
Link to comment
Share on other sites
7 answers to this question
Recommended Posts