Hello,
I need to write a program that calculates how many days from year January 1, 0000 to any specified date including leap years. When I run the program that I have, I am off by a few days for any given date. Can someone please check out my code and see what I'm doing wrong. I am new to C++ so please cut me some slack if there are simple errors that I have missed. Just a side note...in "library.h" there are commands that are different that in just C++ (i.e. print command).

#include "library.h"
int const day_of_forever(int const year, int const month, int const day)
 {
     int count[]={0,31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
     if ((year % 4==0) && (year % 100 != 0) && (year % 400 == 0 )&& (month>=2))  ((count[month]+day)+1);
     return (count[month-1]+day)+(((year)*365)+1) +((year)/4)-(year/100)+(year/400);
  }

  void main()
{ 
    int const year=1776;
    int const day=7;
    int const month=4;
    int const something=day_of_forever(year,month,day);
    print (something);
}

How about a "partial" solution? Like how many days since the epoch, the year 1970 Jan 1, 7:00 a.m.?

You have to limit the scope of your inputs somehow...

I have a function that works for the years 2000-2099 using the leap year criterion of 4 years. I used part of that function's logic to create the one that is posted. According to the instructions, November 27, 2737 should be the millionth day from day January 1, 0000. This program's function returns a value of 1000020 for that date. Very frustrating:-/

(year % 100 != 0) && (year % 400 == 0 )

something wrong there... if (year % 100 != 0) then (year % 400 cannot be == 0 )

ps: I guess you know that the current (Gregorian) calendar is only valid from 1752?

The simplest way is to convert the dates to Julian dates (days since start of "time"), and subtract one from the other. Bingo, done... Wikipedia has some good articles about this: http://en.wikipedia.org/wiki/Julian_day - skip to "Converting Julian or Gregorian calendar date to Julian Day Number".

The project states that it must start from January 1, 0000 to make things "simpler". The rules for leap years from what I understand are as follows...
Any year that is divisible by 400 is a leap year,
any other year that is divisible by 100 is not a leap year,
any other year that is divisible by 4 is a leap year, and
any other year is not a leap year.

I think my logic is correct but there is something within the return command itself that is incorrect. Not sure.

The year zero does not exist! See here. The number zero, has we now use it had to be invented. People in those days couldn't cope with "nothingness".

Yes I am aware that the year zero does not exist in reality but it is still what the project calls for. Not my rules. I think it was made that way to make things more simple but it failed.

Leap years are irrelevant here if you follow my comments previously posted. You can calculate the number of days from a base for any date. Subtract the older from the later, and you get the correct number of days between the two dates. I have implemented this algorithm multiple times for multiple employers when we needed accurate date calculations. In fact, you can even use it down to the second! I could post the C++ code here, but the header file is 10K and the source is another 40K. Contact me via private email of you need more information. My date class has a lot of functionality you do not need. The math in the wikipedia article I referenced has everything you need for a simple implementation.

That would be great but I don't know how to send pms on here. Im a newbie.

Click on an avatar. In the window that opens you will find a button to send private message.

Ok so I click the avatar but I do not see a "private message" option. Am I looking in the wrong place?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.