C++ output date in number of days per year

In summary, the conversation is about creating a program that calculates the total number of days in a given month and year. The user inputs the month, day, and year and the program takes into account leap years, which occur every 4 years except for years that are divisible by 100 but not 400. The conversation also mentions using boolean statements and switch cases to write the program, and suggests picking up a C or C++ book for more help.
  • #1
SqrachMasda
42
0
for example
user puts in the month as 1-12
the day for whatever month
and the year, which has to include leapyears
leap years only occur when the year is divisible by 4 y%4==o or if divisible by 400 but NOT 100, so 2000 is a leap year but not 2100

so if somebody puts in month 1 day 1 and 2005 it will say 1
if it's month 12 and day is 31 and year is 2005 it will say 365
but if it was 2004 it would say 366
month 3, day 10, year 2005, would be 31+28+10 or just 69

can anybody do this
i guess it needs boolean stuff for the leap year
and a few switch and case things for the rest

i'm not feeling it though

i got one to kind of work with if statements but not correctly, and it was long
 
Technology news on Phys.org
  • #2
Code:
if (year%4 == 0 && !(year%100 == 0 && year%400 != 0))
    DaysPerMonth = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
else
    DaysPerMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

for (i=0; i<month - 1; i++)
    ndays += DaysPerMonth[i];

ndays += day;
 
  • #3
Well put :)
 
  • #4
and what am i putting in right above that (i've been doing this for less than a month so bear with me)
i don't know what exactly i am calling what
int days, etc.?
 
  • #5
pick up a standard C OR C++ book. there are tutorials about such things usual in those or there are websites...sorry I can't remember C++tutorials.com cplus.com i think are some...
 
  • #6
yeah i got a book, but it's no good, or it's too good
 
  • #7
neurocomp2003 said:
pick up a standard C OR C++ book. there are tutorials about such things usual in those or there are websites...sorry I can't remember C++tutorials.com cplus.com i think are some...

heh I don't think that's a legal URL format, only alpha-numerical chars are allowed :tongue2:
 

1. How can I output the current date in C++ in number of days per year?

To output the current date in C++ in number of days per year, you can use the chrono library. First, include the library using #include <chrono>. Then, create a system_clock object using chrono::system_clock::now(). Finally, use std::chrono::time_point_cast to cast the system clock object to std::chrono::days and output the result.

2. How do I convert a date in C++ from number of days per year to a specific date format?

To convert a date in C++ from number of days per year to a specific date format, you can use the gmtime function from the ctime library. First, include the library using #include <ctime>. Then, use gmtime to convert the number of days to a tm struct. Finally, use std::put_time to format the date according to your desired format.

3. Can I output a specific date in C++ in number of days per year?

Yes, you can output a specific date in C++ in number of days per year by using the mktime function from the ctime library. First, include the library using #include <ctime>. Then, create a tm struct with the desired date. Next, use mktime to convert the tm struct to a time_t object. Finally, use std::difftime to calculate the number of days between the current date and the desired date.

4. How can I calculate the number of days in a specific month in C++?

To calculate the number of days in a specific month in C++, you can use the <ctime> library. First, use the mktime function to convert the desired month to a tm struct. Then, use the tm_mon field to get the month and tm_year field to get the year. Finally, use the algorithm (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 to determine if the year is a leap year and then use a switch statement to return the correct number of days for the given month.

5. How do I output the current date and time in C++ in number of days per year?

To output the current date and time in C++ in number of days per year, you can use the chrono library. First, include the library using #include <chrono>. Then, create a system_clock object using chrono::system_clock::now(). Finally, use std::chrono::time_point_cast to cast the system clock object to std::chrono::hours and use std::chrono::duration_cast to get the number of hours since the start of the year. Then, use std::chrono::minutes and std::chrono::seconds to get the remaining minutes and seconds.

Similar threads

  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
6K
Replies
2
Views
11K
  • Programming and Computer Science
Replies
9
Views
2K
Replies
1
Views
976
Replies
4
Views
755
  • Precalculus Mathematics Homework Help
Replies
5
Views
2K
Replies
1
Views
2K
Back
Top