Thread: my problem...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    15

    Unhappy my problem...

    need help with my program, it's a Julian Date conversion. The part that i'm having difficulty with is when you enter a julian date, there are 6 numbers after the decimal. How do i get the 6 numbers after the decimal so that i can calculate the hours, minutes and seconds?? here's my code:


    Code:
    #include <stdio.h>
    
    int main()
    {
    double jdn;
       long lc, nc, ic, jc;
       int day, month, year, hours, minutes, seconds, a, b, meridian ;
    
    
    
     /*print title*/
    
     printf(" * * Julian Date Conversion * *\n") ;
    
     /*ask user for a Julian date */
    
     printf ("Please enter a Julian Date:\n") ;
     scanf("%lf", &jdn) ;
    
    if (jdn <= 2299160)
      printf ("Error: You have entered an invalid number.") ;
    
    /* Perform calculations */
       lc = jdn + 68569;
       nc = 4 * lc / 146097;
       lc = lc - (146097 * nc + 3) / 4;
       ic = 4000 * (lc + 1) / 1461001;
       lc = lc - 1461 * ic / 4 + 31;
       jc = 80 * lc / 2447;
       day = lc - 2447 * jc / 80;
       lc = jc / 11;
       month = jc + 2 - 12 * lc;
       year = 100 * (nc - 49) + ic + lc;
       hours= jdn % * 24 ;
       a= hours % 60 ;
       minutes= a * 60 ;
       b= minutes % 60 ;
       seconds= b * 60 ;
    
    if (meridian >= 12)
       printf ("p.m.") ;
    else printf ("a.m.") ;
    
    /* print date*/
    
       printf("%d %d %d %d %d %d\n", month, day, year, hours, minutes, seconds,
              meridian);
       return 0;
    }
    my problem more specifically, is in the calculations portion where hours begins. to help understand what i mean:
    Calculation of time portion:
    (0.1 days equals 0.1 * 24 hours = 2 hours (remainder 0.4 hours),
    0.4 * 60 minutes = 24 minutes (remainder 0.0 seconds)
    0.0 * 60 seconds = 0 seconds

    I dont know how to get only the numbers after the decimal, or if there is another way i dont see it yet, so that i can calculate the hours, minutes, and seconds!! please help.

  2. #2
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by lyojarhhs
    there are 6 numbers after the decimal. How do i get the 6 numbers after the decimal so that i can calculate the hours, minutes and seconds?
    If you truncate the double jdn by storing it into a long integer and then subtract that integer from jdn you'll be left with the decimal part. You could then multiply by 1,000,000 to move it up past the decimal if it's easier.
    ===
    Don't grumble about what you can't have;
    be grateful you don't get what you deserve.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    I'm not sure i did what you asked, but i tried:

    jdn = lc; /*truncate*/
    jdn - lc ;
    hours= jdn * 24 ;
    a= jdn % 60 ;
    minutes= a * 60 ;
    b= minutes % 60 ;
    seconds= b * 60 ;

    when i try to compile i get an error message saying "Operations between types 'double' and 'int' is not allowed"..
    obviously im doing something wrong.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    The modulus operator is an integer operation and jdn is a double. That's a problem.
    Sent from my iPad®

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    You should use type castin. Like [edited for C]
    Code:
    jdn = (double)lc;
    a= (long)jdn % 60 ;
    But what is wrong in your code is
    Code:
    jdn = lc; /*truncate*/
    jdn - lc ; /*It just does nothing*/
    [EDIT]
    No sorry. I didn't noticed there is no assignment.
    Last edited by siavoshkc; 10-03-2006 at 03:45 PM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #6
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by lyojarhhs
    I'm not sure i did what you asked, but i tried:

    jdn = lc; /*truncate*/
    jdn - lc ;
    hours= jdn * 24 ;
    a= jdn % 60 ;
    minutes= a * 60 ;
    b= minutes % 60 ;
    seconds= b * 60 ;

    when i try to compile i get an error message saying "Operations between types 'double' and 'int' is not allowed"..
    obviously im doing something wrong.
    No, I was thinking of something like:
    Code:
    double tm;          /* <-------- for storing fractional part */
    
    lc = jdn;             /* <------ this gets the integer part; the number of days */
    tm = jdn - lc;      /* <------ this gets the fractional part of the day */
    Your example seems to be obliterating the julian date entered by the user
    with an uninitialized integer.
    ===
    Don't grumble about what you can't have;
    be grateful you don't get what you deserve.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    Quote Originally Posted by risby
    No, I was thinking of something like:
    Code:
    double tm;          /* <-------- for storing fractional part */
    
    lc = jdn;             /* <------ this gets the integer part; the number of days */
    tm = jdn - lc;      /* <------ this gets the fractional part of the day */
    Your example seems to be obliterating the julian date entered by the user
    with an uninitialized integer.
    when i do

    lc = jdn;

    i still get the message "Operation between types 'double' and 'int' not allowed.

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Unless you're on some strange compiler that line won't give you an error... I feel like you probably completely ignored the two posts before it that told you that you can't use a double with the modulus operator. Convert it to an integral type first. I'd almost garuntee that's what's giving you the error.
    Sent from my iPad®

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM