Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 24th, 2008, 1:18 PM   #1
Simplesouljah
Newbie
 
Join Date: Oct 2007
Posts: 28
Rep Power: 0 Simplesouljah is on a distinguished road
Question Help

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)
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7.  
  8. int main (void)
  9. {
  10. int month = 0;
  11. int month2 = 0;
  12. int day = 0;
  13. int day2 = 0;
  14. int year = 0;
  15. int year2 = 0;
  16. int NumofDays = 0;
  17. int jDate = 0;
  18.  
  19. const int DaysInMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  20. const int DaysInYear[13] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
  21. const int DaysInLeapYear[13] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366};
  22.  
  23. cout << "Please enter your birthday or other significant date in the proper"
  24. << " format: "
  25. << " MM-DD-YYYY Example: (10-17-1982)." << endl << endl;
  26. cin >> month >> day >> year;
  27.  
  28. if (month < 0 || month > 12 || day < 0 || day > 31 || year < 1900 || year > 2099)
  29.  
  30. {
  31. cout << "One of the numbers are entered is incorrect" << endl;
  32. cout << " Please re-enter the date again" << endl;
  33. cin >> month >> day >> year;
  34. }
  35.  
  36.  
  37. }
Simplesouljah is offline   Reply With Quote
Old Jan 24th, 2008, 1:45 PM   #2
mbd
Programmer
 
Join Date: Nov 2007
Posts: 86
Rep Power: 1 mbd is on a distinguished road
Re: Help

here is what one of your functions might look like
c Syntax (Toggle Plain Text)
  1. int daysBetweenDates(int year1, month1, day1, year2, month2, day2)
  2. {
  3. }
mbd is offline   Reply With Quote
Old Jan 24th, 2008, 1:49 PM   #3
Simplesouljah
Newbie
 
Join Date: Oct 2007
Posts: 28
Rep Power: 0 Simplesouljah is on a distinguished road
Re: Help

I'm sorry, I'm still confused....
Simplesouljah is offline   Reply With Quote
Old Jan 24th, 2008, 2:01 PM   #4
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 547
Rep Power: 4 Ancient Dragon is on a distinguished road
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.
Ancient Dragon is offline   Reply With Quote
Old Jan 24th, 2008, 2:03 PM   #5
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,886
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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.
Sane is offline   Reply With Quote
Old Jan 28th, 2008, 6:45 PM   #6
Simplesouljah
Newbie
 
Join Date: Oct 2007
Posts: 28
Rep Power: 0 Simplesouljah is on a distinguished road
Re: Help

ok, so can someone tell me what I'm doing wrong? I am so lost

Quote:
Originally Posted by Sane View Post
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.
Simplesouljah is offline   Reply With Quote
Old Jan 28th, 2008, 7:12 PM   #7
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,886
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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.
Sane is offline   Reply With Quote
Old Jan 28th, 2008, 7:30 PM   #8
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 547
Rep Power: 4 Ancient Dragon is on a distinguished road
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.
Ancient Dragon is offline   Reply With Quote
Old Jan 28th, 2008, 7:35 PM   #9
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,886
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Help

Quote:
Originally Posted by Ancient Dragon View Post
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.
But then are you not just back to square one, where it would become easier to use predefined library functions?
Sane is offline   Reply With Quote
Old Jan 28th, 2008, 7:36 PM   #10
Simplesouljah
Newbie
 
Join Date: Oct 2007
Posts: 28
Rep Power: 0 Simplesouljah is on a distinguished road
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...
Simplesouljah is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:10 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC