When you put back in the missing "=" after static const DATE_RANGE date_range[] it will compile, but it doesn't actually work, as all values of "julian" (bar zero) give the answer january!
Nice try though.
When you put back in the missing "=" after static const DATE_RANGE date_range[] it will compile, but it doesn't actually work, as all values of "julian" (bar zero) give the answer january!
Nice try though.
Code:/****************************** * C Program Code::Blocks v1.3 * * Written by: EvoTone * * Date: Sept., 2006 * *******************************/ #include <stdio.h> typedef struct /*This structs DATE_RANGE w/ 2 constants num, char */ { const int lower; /*Defines a number as lower*/ const char * month; /*Defines month names*/ } DATE_RANGE; /*Name of the struct*/ static const DATE_RANGE date_range[] = { { 335, "December" }, { 305, "November" }, { 274, "October" }, { 244, "September" }, { 213, "August" }, { 182, "July" }, { 152, "June" }, { 121, "May" }, { 91, "April" }, { 59, "March" }, { 31, "February" }, { 0, "January" } }; int main(void) { int i; int julian; /*input value from user, needing conversion.*/ /*Intro. & Instructions*/ printf("This is a Julian to Calendar Date conversion program\n"); printf("you may input any Julian date and the corresponding\n"); printf("Calendar Date will be calculated for you\n\n"); /*Input Section*/ printf("Please enter the Julian date: (whole number format)\n"); scanf("%i", &julian); printf("Well silly we know it's Year: 2006!\n\n"); /*Calculation Section*/ if ( (julian < 1) || (julian > 365) ) { printf("Don't mess with me stupid!!...Julian Dates are only 1-365!\n"); return(1); } for (i = 0; i < (sizeof(date_range) / sizeof(date_range[0])); i++) { if (julian > date_range[i].lower) { printf("And we know it's: %s\n", date_range[i].month); break; } } return (0); }
Or (more obtusely), and less portable, but shorter and leap-year compliant!
Code:/****************************** * C Program Code::Blocks v1.4 * * Written by: EvoTone * * Date: Sept., 2006 * *******************************/ #include <stdio.h> #include <time.h> int main(void) { int julian; /*input value from user, needing conversion.*/ time_t t; struct tm * ptm; char date_str[ 50 ]; /* Introduction & Instructions */ printf("This is a Julian to Calendar Date conversion program\n"); printf("you may input any Julian date and the corresponding\n"); printf("Calendar Date will be calculated for you\n\n"); /* Input Section */ printf("Please enter the Julian date: (whole number format)\n"); scanf("%i", &julian); /* Calculation Section */ if ( (julian < 1) || (julian > 365) ) { printf("Don't mess with me stupid!!...Julian Dates are only 1-365!\n"); return(1); } t = time( (time_t *) NULL ); /* Today's date/time */ ptm = localtime( &t ); if ( ptm != NULL ) { ptm->tm_mday = 1; /* 1st of month */ ptm->tm_mon = 0; /* January */ /* Add "julian - 1" days to start of year */ t = mktime( ptm ) + ( (julian - 1) * 24 * 60 * 60); if ( t >= 0 ) { ptm = localtime( &t ); if ( ptm != NULL ) { /* Convert to string in format "Monday January 01 2006" */ if ( strftime(date_str, sizeof(date_str), "%A %B %d %Y", ptm) != 0) printf("And we know it's: %s\n", date_str); } } } return (0); }
Looks like the program has been served, with different versions.
I'm sure the thread starter will be so happy. =P
Code:Noob - a word used to describe someone like me. (noun)
You are right I found that I can test for the upper value but, the lower doesn't seem to work due to the fact that "January" is displayed everytime julian is greater than i.You only need to test for either the upper or the lower bound, and as the loop breaks out afterwards it will not continue.
Thanks guys!! I have learned a lot.... I am still to unexperienced to worry about leap years yet but, I have enough with the code still needing to define the days of the month compared to the days of a calendar year. After all I am trying to convert a "Julian Date" into a 200x/January/2 format. I'm sure as I head into this next phase of the project I will have more questions. For now thanks to all who helped out.
~EvoTone~
__________________________________________________ _____________
If you wake up every morning and you can think of nothing else but, programming. You are a programmer!
As an added bonus, you may also want to make it work for the year 1752 (check the month of September for the number of days that month had)
Code:server:/home$ cal 09 1752 September 1752 Sun Mon Tue Wed Thu Fri Sat 1 2 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
I think you can put a signature here.