![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2007
Posts: 28
Rep Power: 0
![]() |
once again I'm trying to write a program. This program determines the number of days between two dates that are input by the user. Must take into account leap years.
the program must: 1. at least 1 'if' statement 2. 1 switch statement 3. 1 loop structure 4. 2 functions, value returning and/or viod functions 1-3 may be implemented in the user defined functions rather than main function. I have started but not sure if I'm on thee right path. c++ Syntax (Toggle Plain Text)
|
|
|
|
|
|
#2 |
|
Programmer
Join Date: Nov 2007
Posts: 86
Rep Power: 1
![]() |
Re: Help
here is what one of your functions might look like
c Syntax (Toggle Plain Text)
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Oct 2007
Posts: 28
Rep Power: 0
![]() |
Re: Help
I'm sorry, I'm still confused....
|
|
|
|
|
|
#4 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 547
Rep Power: 4
![]() |
Re: Help
Calculating the number of days between dates can be very tricky, especially if one of the two dates is before the gregorian calendar was invented. If you limit the two dates to be after 1970 then you can use the functions in time.h. create an object of struct tm, fill in the date, them call mktime() to get the number of seconds. Do the same for the second date. Then call datediff() to return the difference.
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
Re: Help
Yes, unless the assignment restricts you from using predefined functions, do what Ancient Dragon suggested. There's no sense in reinventing the wheel. Unless that's the purpose of this homework assignment.
|
|
|
|
|
|
#6 |
|
Newbie
Join Date: Oct 2007
Posts: 28
Rep Power: 0
![]() |
Re: Help
ok, so can someone tell me what I'm doing wrong? I am so lost
|
|
|
|
|
|
#7 |
|
Programming Guru
![]() |
Re: Help
Read what Ancient Dragon said. You can use the function
mktime() to calculate the epoch of a date. Do that once for each date. Then use the function difftime() to figure out the number of days between the two. |
|
|
|
|
|
#8 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 547
Rep Power: 4
![]() |
Re: Help
Another way to do it is to just convert both dates to days and subtract the two. Something like this.
unsigned int day1 = 1, month1 = 1, year1 = 2007;
unsigned int day2 = 1, month2 = 1, year2 = 2008;
unsigned int days1 = year1*365 + day1;
if( month1 > 1)
// we do not want to include the number of days in the
// current month, so we have to subtract 2.
days1 += DaysInYear[month1-2];
unsigned int days2 = year1*365 + day1;
if( month2 > 1)
days1 += DaysInYear[month2-2];
unsigned int delta = days2 - days1;The above is not 100% accurate because it does not account for leap years. So I would create a loop to run from year 0 to year1 and from 0 to year2 and add 1 for each leap year found. There are probably faster ways to do it, but that will do. |
|
|
|
|
|
#9 |
|
Programming Guru
![]() |
Re: Help
But then are you not just back to square one, where it would become easier to use predefined library functions?
|
|
|
|
|
|
#10 |
|
Newbie
Join Date: Oct 2007
Posts: 28
Rep Power: 0
![]() |
Re: Help
i read what he wrote, I was going to give a time from from 1900-2099...anything below or beyond those years and you would get an error, and have to input another date...
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|