Thread: Calculate Days Between 2 Months

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    55

    Calculate Days Between 2 Months

    Hi,

    i need help in writing a simple program which can calculate the number of days between 2 months, for the year 2010.

    E.g. if the user enters start month as 1, and end month as 3, the program should be able to calculate the numbers of days From 1st January To 31 March.

    Thanks

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    What have you done so far?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    While waiting for you to give it your best shot before posting code, here's a couple of general hints to get you started...

    1) you're going to need a data base with the number of days in each month.
    2) your software is going to have to account for leap years.
    3) You will also most likely want the day of the month for your star and end periods.

    It's a fairly simple calculation so I'm betting you can figure that out on your own.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    I have been trying various things.

    So far i can get the user to enter a start month and a end month. I am not sure of what to do next.

    Thanks
    Last edited by Khadafi; 11-27-2010 at 08:19 PM.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well... you need to know when to start calculating and when to stop.... so what do you think?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    Need to start calculating once the user has entered both months.


    I'm thinking i need an array to say how many days are in each month,

    Code:
    int NumDaysInEachMonth[] ={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    But i don't understand how i will say that January has 31 days, February has 28 days.
    I will then add the 2 values together, then display that value.
    Last edited by Khadafi; 11-27-2010 at 08:20 PM.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You do need an array, but like this....

    Code:
    int NumDaysInEachMonth[] ={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    For the days calculation...
    Code:
    // user enters 3 (March)
    int StartMonth;
    
    Days = NumDaysInEachMonth[StartMonth];
    You're on your own for the rest.
    But here's a hint... there are only 31 days from January 1 to February 1...

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    Hi, thanks i can get it to add the values entered for start date and end date.

    But the problem is, that at the moment it only adds 13 and 13, as the program keeps asking for start and end months until 13 is entered (as 13 is the one for exit), how can i get the program to stop asking for a start / end date after it has been entered once?

    Thanks

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    Hi, I've got it working I think. Going to test it properly
    Thanks

  10. #10
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    The program i've got working adds the number of days for 2 months, e.g Jan 31 and Oct 31 = 62. I need the program to calculate days for all the months in between also, so Jan + Feb + March ... all the way to the end of october. How would i do this?
    Thanks

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Khadafi View Post
    The program i've got working adds the number of days for 2 months, e.g Jan 31 and Oct 31 = 62. I need the program to calculate days for all the months in between also, so Jan + Feb + March ... all the way to the end of october. How would i do this?
    Thanks
    Think "loops"....

  12. #12
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    yea thats what ive been trying, but can't figure it out.

    i put month 1 and month 2 values into i and j. then try and use the loop to add all values from i, till the value of j?


    Code:
    i=month1;
    j=month2;
    
    
    for (counter=0; counter < 13; counter++)
    {
    		numdaysbetween2months = monthdays[i] + monthdays[counter];
    }
    thanks

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Khadafi View Post
    yea thats what ive been trying, but can't figure it out.

    i put month 1 and month 2 values into i and j. then try and use the loop to add all values from i, till the value of j?

    Code:
    for (counter=START; counter < END; counter++)

  14. #14
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    could you please explain, i don't understand
    thanks

  15. #15
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    Like CommonTater mentioned:
    int NumDaysInEachMonth[] ={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    You get the start month assign it to a variable let say int startMonth, and end month as int endMonth. If 1-12 is the input of the user from January to December, then:
    for(i = startMonth; i<=endMonth; i++)
    {
    totalDay = totalDay+NumDaysInEachMonth[i];
    }
    note that your totalDay should be set to 0 by default.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advancing Days
    By nhubred in forum C++ Programming
    Replies: 0
    Last Post: 06-01-2009, 06:22 PM
  2. summing number of days between 2 months
    By duff_johnny in forum C Programming
    Replies: 5
    Last Post: 11-03-2005, 02:36 PM
  3. Algorithm help
    By mmyers1 in forum C++ Programming
    Replies: 7
    Last Post: 04-05-2004, 09:56 PM
  4. Small program that has to calculate miles per gallon
    By Guti14 in forum C++ Programming
    Replies: 6
    Last Post: 01-06-2004, 02:47 PM
  5. while, sentinel, if, switch
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-11-2001, 11:50 PM