Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Quick c++ question

Options
  • 06-02-2006 6:05pm
    #1
    Closed Accounts Posts: 1,299 ✭✭✭


    Ok I am complinig a program to conver the serial date to D/M/Year format,

    my question is how do i find the number of years when given the number of days since 1900 eg,
    for 380 days,

    is there some way of doing

    years = days - 365

    and getting the program to stop when the days left are less than 365 and recording how many times it took 365 away?


Comments

  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    I don't know something like this maybe, since each year has 365 1/4 days or 365 per year and 366 every fourth year. Easier just to use 365.25 days to each year.

    Subtract the number of days per year from total days until number is less than 365.25 in a loop. Very very straight forward really.


  • Closed Accounts Posts: 1,299 ✭✭✭Sandals


    yeah but how do i record how many times i have substracted the 365.25 days in the loop?


  • Closed Accounts Posts: 1,299 ✭✭✭Sandals


    no one know?


  • Registered Users Posts: 1,275 ✭✭✭bpmurray




  • Closed Accounts Posts: 1,299 ✭✭✭Sandals


    that link is causing my laptop computer to crash.


  • Advertisement
  • Moderators, Recreation & Hobbies Moderators, Science, Health & Environment Moderators, Technology & Internet Moderators Posts: 91,692 Mod ✭✭✭✭Capt'n Midnight


    http://en.wikipedia.org/wiki/Julian_day_number - formula to calculate number of days


  • Registered Users Posts: 1,275 ✭✭✭bpmurray


    Sandals wrote:
    that link is causing my laptop computer to crash.

    It's in gzip format - no reason to crash anything. Since you didn't spot that, I guess you're a Windoze-only person, so here's the readme + source attached.


  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    how do you record number of time 365.25 is subtracted???
    Count everytime the subtraction occurs. Was that not obvious???


  • Closed Accounts Posts: 268 ✭✭UberNewb


    Instead of using a loop and counter use the div command. This will give you the quotient (years) and the remainder (days), for example:

    int Numdays = 380;
    div_t Ans = div(Numdays, 365.25);

    so Ans.quot = 1 and Ans.rem = 15


  • Closed Accounts Posts: 1,299 ✭✭✭Sandals


    silas wrote:
    how do you record number of time 365.25 is subtracted???
    Count everytime the subtraction occurs. Was that not obvious???


    no it's not obvious and I still don't get what you mean, there is no c++ function called count.

    I don't see the need for your patronising tone and attitude.


  • Advertisement
  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    Count the number of times the loop.

    UberNewb's suggestion is the best, I don't know why I suggested the loop method its very inefficent, but a solution nonetheless.

    This should also work
    int main ( void )
    {
      const float DAYS_PER_YEAR = 365.25f ;
      /* However you get the days since 1900 */
      unsigned int days_since_1900 = get_days_since_1900 ( ) ;
    
      unsigned int yrs_since_1900 = ( days_since_1900 / DAYS_PER_YEAR ) ;
    }
    
    Right that should do it!!!

    There is no count function in C/C++ to increment a variable such as a number put ++ after it/before it, -- with decrement the variable. If your programming in C++, you really should know this, perhaps you should have a look at some online C++ tutorials.


Advertisement