Below is my new code for this julian day program. I have fixed some of the errors I mentioned earlier. However, the day is still one short for the julian day and my conditional if statement seems to be the problem. I have tried a couple of ways to fix it. Any suggestions.

Thanks.

//	Assignment #3, Programming I, Summer 2005
//	Name:  Darius Juan Bloomer


#include <iostream>
#include <string>
#include <cmath>

using namespace std;


long julian ( int year, int month, int day );

int main()
{
	const long int julian_day = 2450000;
	int year = 0;
	int month = 0;
	int day = 0;
	int t_year = 0;
	int t_month = 0;
	int t_day = 0;


	cout << " Enter a year, month, day ( ex. 1323 11 30 or -1400 8 11): ";
	cin >> year;
	cin >> month;
	cin >> day;
	cout << endl << endl;


	long julian_date = julian( year, month, day);

	cout << julian_date << endl;



	cout << " What is today's date in the same format(ex. 2005 2 22): ";
	cin >> t_year;
	cin >> t_month;
	cin >> t_day;


	long today_date = julian( t_year, t_month, t_day);
	long difference = today_date - julian_date;
	
	cout << difference << endl << endl;
	
	return 0;
}


		
	long julian ( int year, int month, int day)
	{

		int j_year = year;
		int j_month = month;
		int j_day = day;

		if ( j_year < 0)
			
			j_year += 1;
		
		if ( j_month > 2)
			
			j_month += 1;

		else
		{
			j_month += 13;
			j_year -= 1;
		}

		long jul =static_cast<long>(floor(365.25 * j_year) + floor(30.6001 * j_month) + j_day + 1720995.0);

	
	//	if  (j_year <= 1582) {
	//	if  (j_month <= 10) 
	//	if  ( j_day < 15 )  
	        
		if ( j_year < 1582 || ( j_year == 1582 && (j_month < 10 || ( j_month == 10 && j_day < 15))))
			return jul;
		

		else
		{
			int ja = static_cast<int>(0.01 * j_year);
			jul +=  static_cast<int>(2 - ja + 0.25 * ja);

			return jul;
		}
		
	}

Code tags added. -Narue

Below is my new code for this julian day program. I have fixed some of the errors I mentioned earlier. However, the day is still one short for the julian day and my conditional if statement seems to be the problem. I have tried a couple of ways to fix it. Any suggestions.

Could you better describe the problem?

C:\Test>testpp
Enter a year, month, day ( ex. 1323 11 30 or -1400 8 11): 2005 7 7


2453559
What is today's date in the same format(ex. 2005 2 22): 2005 7 8
1


C:\Test>testpp
Enter a year, month, day ( ex. 1323 11 30 or -1400 8 11): 2005 7 6


2453558
What is today's date in the same format(ex. 2005 2 22): 2005 7 8
2

long jul =static_cast<long>(floor(365.25 * j_year) + floor(30.6001 * j_month) + j_day + 1720995.0);

first of. 1720995.0 shuld be changed to 1720994.5
and second. remove the floor() - and it is doing it corectly.

>Could you better describe the problem?
As far as I know it is showing 1 day less in some years, (not when calculating, but the julian date is 1 less than is shuld be).

Will try it. But the floor() is the formula given in the book.

thanks.

I tested it with a convertor I found somwhere, and it worked without the floor().

This article has been dead for over six months. Start a new discussion instead.