This is a discussion on Counting within the C++ Programming forums, part of the General Programming Boards category; I have come to a blank on how to do this. I have to calculate how many days there are ...

  1. #1
    Registered User
    Join Date
    Oct 2001


    I have come to a blank on how to do this. I have to calculate how many days there are between a range of years. I have declared everything and I have gave it the rules to how many days there are in a year due to leap years and nonleapyears. I know I need to create a loop that will add all those days but I dont know where to start at for this loop. Do i use a for loo or a while loop. If I use one of these loops what would be the structure. I am not asking for the code by anymeans because i want to figure that out myself I just need to get my wheels spinning and am at a blank right now. Tahnks for any help.

  2. #2
    Registered User
    Join Date
    Aug 2001
    From what i understand you need to run a loop based on the number of years you are adding, this could be a for.

    Inside the loop just iadd up a counter to stand for the total number of days, and be sure to check for a leap year.

  3. #3
    Registered User
    Join Date
    Sep 2001
    I don't think you need a loop, I think you can calculate it.

    EndYear - StartYear + 1 = YearRange

    YearRange / 4 = LeapYears

    If the first occurance of a leap year starting at StartYear is equal to or less than YearRange%4 then the calculation would be:

    ( YearRange / 4 ) + 1 = LeapYears

    Then finally

    days = ( 365 * (YearRange - LeapYears) ) + (364 * LeapYears )

    Its been a long day so I would double check my thinking here. If its not right I think its pretty close.

  4. #4
    Join Date
    Aug 2001
    Are you sure you have to use a loop?

    You can find out the number of years divisible by 4, the number of
    years divisible by 100, the number of years divisible by 400, and so on.

    Then you can draw one of those nice venn diagrams and count how
    many leap years.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Ok here is what I got, I am still stuck on it and it makes me mad. I hate not being able to figure something out. This is supposed to be an intro to C++ class but i think the teacher is making it harder then that. I dont feel a student should have to work about 2-3 hours a day for a week straight on one problem for an intro class. here is what i have for the code so far. Before all of this I have the user inputting in the years. That part works fine. What i need the program to do is add up all the days in the range of years then also output which of the years in that range are leapyears. Any help is appreciated. Just something to get me int hte right direction.

    int leapyear;
    int year;
    if (year % 400 == 0)
    year = leapyear;
    else if (year % 4 != 0 || year % 100 == 0)
    year != leapyear;
    if (year == leapyear)
    days = 366;
    days = 365;
    for (year = year1; year <=year2; year++)

  6. #6
    There is an alternative (potentially) called difftime() located in time.h if you are allowed to use precompiled functions and know how to handle structs. It returns the difference in time between two separate dates, which can then be manipulated as you deem necessary.

    I don't see any way around using a loop in the slug em out way, but it shouldn't be hard to write the loop.

    create function to check if year is leap year--isLeapYr().//This is the "hard thingy".

    create a variable to hold total days and intitalize to zero.

    create two variables to hold the two years

    Obtain input for the two years.

    Then use a loop to deterimine if a given year starting at the first year incrementing by one until the last year is a leap year by calling isLeapYr() on each year in the interval. If yes add 366, if no add 365, to total. Even with a thousand year interval, the task will be completed before you can blink your eye with todays CPUs.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting up(and down) using recursive function. Please help.
    By MarkSquall in forum C++ Programming
    Replies: 6
    Last Post: 06-06-2008, 04:26 AM
  2. How to implement reference counting with 'Smart_ptr'?
    By meili100 in forum C++ Programming
    Replies: 3
    Last Post: 06-10-2007, 05:28 AM
  3. Counting Numbers in Array, not counting last number!
    By metaljester in forum C++ Programming
    Replies: 11
    Last Post: 10-18-2006, 11:25 AM
  4. counting and output, with arrays and functions
    By Wraithan in forum C++ Programming
    Replies: 7
    Last Post: 12-04-2005, 11:46 PM
  5. Counting using Lib Cstring
    By niroopan in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2002, 04:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21