im trying to convert julian date to calendar date and vice versa
im not getting any errors, but the output im getting is ridiculous.
any help is appreciated
#include "stdafx.h"
/*
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
*/
int isLeapYear(int year){
return ((!(year % 4) && year % 100) || !(year % 400));
}
int getChoice(void)
{
int result = 1;
scanf_s("%d", &result);
return result;
}
int sum_array(int dayspermonth[], int n);
void getCalendarDate(int *month, int *day, int *year)
{
// Input from console month day year
int m, d, y;
scanf_s("%d %d %d", &m, &d, &y);
*month = m;
*day = d;
*year = y;
}
void calendarToJulian(void)
{
int m, d, y;
// Array indices 0, 1, 2, ...
int dayspermonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int sum;
int i; // Loop index
getCalendarDate(&m,&d,&y);
// if (m = 5) // Is month May? Error = is assignment
// if (m == 5) // Is month May? Equivalent test good
// m - 1 translates human counting to an array index
// Start out with summing the days and then add a leap year day
sum = 0;
for (i = 0; i < (m - 1); i++)
{
// sum = sum + dayspermonth[i];
sum += dayspermonth[i] + d;
}
for (i = 1; i < (m - 1); i++)
{ sum += dayspermonth[i] + d;
}
if (isLeapYear(y))
{
sum++;
}
for (i = 2; i < (m - 1); i++)
{ sum += dayspermonth[i] + d;
}
if (isLeapYear(y))
{
sum++;
}
for (i = 3; i < (m - 1); i++)
{ sum += dayspermonth[i] + d;
}
if (isLeapYear(y))
{
sum++;
}
for (i = 4; i < (m - 1); i++)
{ sum += dayspermonth[i] + d;
}
if (isLeapYear(y))
{
sum++;
}
for (i = 5; i < (m - 1); i++)
{ sum += dayspermonth[i] + d;
}
if (isLeapYear(y))
{
sum++;
}
for (i = 6; i < (m - 1); i++)
{ sum += dayspermonth[i] + d;
}
if (isLeapYear(y))
{
sum++;
}
for (i = 7; i < (m - 1); i++)
{ sum += dayspermonth[i] + d;
}
if (isLeapYear(y))
{
sum++;
}
for (i = 8; i < (m - 1); i++)
{ sum += dayspermonth[i] + d;
}
if (isLeapYear(y))
{
sum++;
}
for (i = 9; i < (m - 1); i++)
{ sum += dayspermonth[i] + d;
}
if (isLeapYear(y))
{
sum++;
}
for (i = 10; i < (m - 1); i++)
{ sum += dayspermonth[i] + d;
}
if (isLeapYear(y))
{
sum++;
}
for (i = 11; i < (m - 1); i++)
{ sum += dayspermonth[i] + d;
}
if (isLeapYear(y))
{
sum++;
}
}
void getJulianDate(int *julianDay, int *year)
{
int y, jd;
// Input from console month day year
scanf_s("%d %d", &jd, &y);
*julianDay = jd;
*year = y;
}
void julianToCalendar(void)
{
int m, y, jd, d;
int totalToSub;
int dayspermonth[] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
int i; // Loop index
getJulianDate(&jd,&y);
for (i = 0; i < 11; i++)
{totalToSub = dayspermonth[i];
}
if (i >= 1)
{
// This is Feb, check for leap year
if (isLeapYear(y)) {
totalToSub++;
}
}
// if ((julianDay - dayspermonth[i]) > 0)
if ((jd - totalToSub) > 0)
{ m = i + 1;
d = jd - totalToSub;
y = y;
// positive, past the given month
}
else ((jd - totalToSub) < 0);
{ m = i - 1;
d = jd - totalToSub;
y = y;
// number is negative, i - 1 is the month we are currently within
}
}
FILE *csis;
int main(void) {
void menu(void);
int choice;
fopen_s(&csis, "csis.dat", "w");
printf("Date Selection Menu\n");
printf("1) Calendar to Julian\n");
printf("2) Julian to Calendar\n");
printf("3) Exit\n");
printf("Enter Selection (1 - 3)\n");
do {
choice = getChoice();
switch (choice) {
case 1 : calendarToJulian();
printf("Julian day: %d %d\n", calendarToJulian); break;
case 2 : julianToCalendar();
printf("Calendar day: %d %d %d\n", julianToCalendar); break;
case 3 : break;
}
}
while (choice !=3);
return 0;
}