converting normal date to Julian date

okay, so here i am again asking for help (I do also ask my teacher for lots help also so please don't think I'm not trying lol) this code is suppose to convert a date to a Julian date using a function (i;m not very good at functions) and I have this code taking in the date that the user inputs and I believe I have the right conversion for the Julian date, but when I input a date it doesn't work, it just never ends. I think all I need is to figure out what to call for the function and put it in the right place to work.

here's my main funciton and the julian date function. I have three other functions for seeing if the date is valid or not as well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
void main(void)
{

	unsigned int mm = 0, dd = 0, yyyy, day_of_year = 0;
	unsigned int i = 0;
	char answer = 'Y';

	do {

		day_of_year = 0;

		cout << "Please enter the month, day and year like MM DD YYYY." << endl;
		cin >> mm >> dd >> yyyy;
		//
		//	The following WHILE LOOP is used to make sure that the 
		//	data entered are correct.
		//

		while (!valiDATE(mm, dd, yyyy))
		{
			cout << "Please enter the month, day and year like MM DD YYYY." << endl;
			cin >> mm >> dd >> yyyy;
		};

		cout << "The date has been validated." << endl << endl;

		unsigned int juliandate(mm, dd, yyyy);
		cout << "The Julian Date is: " << juliandate << endl;


		cout << "Do you want to continue (Y/N)?" << endl;
		cin >> answer;

	} while (answer == 'Y' || answer == 'y');

	cout << endl << endl << endl;
	system("pause");
}


//Function to take date of installation and convert it to a julian date
unsigned int juliandate(unsigned int year, unsigned int month, unsigned int day)
{//Passes in Installation date
	//Declare variables to be used in converting date
	int julianDate;
	int I;
	int J;
	int K;
	I = year;
	J = month;
	K = day;

	//Formula to convert gregorian date to julian date
	julianDate = K - 32075 + 1461 * (I + 4800 + (J - 14) / 12) / 4 + 367 *
		(J - 2 - (J - 14) / 12 * 12) * 2 / 12 - 3 * ((I + 4900 + (J - 14) / 12) / 100) / 4;

	return julianDate;
Last edited on
here'x the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

// Define function prototypes here.

unsigned int days_in_month(unsigned int, unsigned int);
unsigned int leap(unsigned int);
bool valiDATE(unsigned int, unsigned int, unsigned int);
unsigned int juliandate(unsigned int year, unsigned int month, unsigned int day);

void main(void)
{

	unsigned int mm = 0, dd = 0, yyyy, day_of_year = 0;
	unsigned int i = 0;
	char answer = 'Y';

	do {

		day_of_year = 0;

		cout << "Please enter the month, day and year like MM DD YYYY." << endl;
		cin >> mm >> dd >> yyyy;
		//
		//	The following WHILE LOOP is used to make sure that the 
		//	data entered are correct.
		//

		while (!valiDATE(mm, dd, yyyy))
		{
			cout << "Please enter the month, day and year like MM DD YYYY." << endl;
			cin >> mm >> dd >> yyyy;
		};

		cout << "The date has been validated." << endl << endl;

		unsigned int juliandate(mm, dd, yyyy);
		cout << "The Julian Date is: " << juliandate << endl;


		cout << "Do you want to continue (Y/N)?" << endl;
		cin >> answer;

	} while (answer == 'Y' || answer == 'y');

	cout << endl << endl << endl;
	system("pause");
}



unsigned int days_in_month(unsigned int month, unsigned int year){

	unsigned int days = 0;

	if (month == 2) {
		days = 28 + leap(year);  // function leap returns 1 for leap years
	}
	else if (month == 4 || month == 6 || month == 9 || month == 11) {
		days = 30;
	}
	else {
		days = 31;
	}

	return days;
}


unsigned int leap(unsigned int year)
{
	if (year % 400 == 0)
		return 1;
	else if (year % 100 != 0 && year % 4 == 0)
		return 1;
	else
		return 0;
}



bool valiDATE(unsigned int month, unsigned int day, unsigned int year) {
	unsigned int flag = 0;
	unsigned int days = 0;

	// Validate the month first

	if (month < 1 || month > 12) {
		cout << "Value for month (MM) is out of range." << endl;
		return false;
	}

	// Validate the day of month

	days = days_in_month(month, year);

	if (day < 1 || day > days) {
		cout << "Value for day (DD) is out of range." << endl;
		return false;
	}

	// Validate the year

	if (year < 1990 || year > 2020) {
		cout << "Value for year (YYYY) is out of range." << endl;
		return false;
	}

	return true;

}

//Function to take date of installation and convert it to a julian date
unsigned int juliandate(unsigned int year, unsigned int month, unsigned int day)
{//Passes in Installation date
	//Declare variables to be used in converting date
	int julianDate;
	int I;
	int J;
	int K;
	I = year;
	J = month;
	K = day;

	//Formula to convert gregorian date to julian date
	julianDate = K - 32075 + 1461 * (I + 4800 + (J - 14) / 12) / 4 + 367 *
		(J - 2 - (J - 14) / 12 * 12) * 2 / 12 - 3 * ((I + 4900 + (J - 14) / 12) / 100) / 4;

	return julianDate;
Last edited on
So what are you asking for help with?

You haven't shown your code, so we can't comment on that.



i had to comment it under the question because it was to long for the code and the asking for help paragraph to be in the same box.

Whenever i run the code, after you enter a date, nothing happens, you can just keep pressing enter. I'm not sure where to call the Julian date function or if it's even right.
Line 41: This line is causing a compile error.

Try this:
40
41
42
    unsigned jdate;
    jdate = juliandate(mm, dd, yyyy);
    cout << "The Julian Date is: " << jdate << endl;


Note: I did not check your algorithms. I only corrected the syntax error.

Last edited on
Thank you that fixed it!!
Topic archived. No new replies allowed.