Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Help (http://www.programmingforums.org/showthread.php?t=15024)

Simplesouljah Jan 24th, 2008 2:18 PM

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.

:

  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. }


mbd Jan 24th, 2008 2:45 PM

Re: Help
 
here is what one of your functions might look like
:

  1. int daysBetweenDates(int year1, month1, day1, year2, month2, day2)
  2. {
  3. }


Simplesouljah Jan 24th, 2008 2:49 PM

Re: Help
 
I'm sorry, I'm still confused....

Ancient Dragon Jan 24th, 2008 3:01 PM

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.

Sane Jan 24th, 2008 3:03 PM

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.

Simplesouljah Jan 28th, 2008 7:45 PM

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

Quote:

Originally Posted by Sane (Post 140213)
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 Jan 28th, 2008 8:12 PM

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.

Ancient Dragon Jan 28th, 2008 8:30 PM

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.

Sane Jan 28th, 2008 8:35 PM

Re: Help
 
Quote:

Originally Posted by Ancient Dragon (Post 140460)
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?

Simplesouljah Jan 28th, 2008 8:36 PM

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...


All times are GMT -5. The time now is 3:43 AM.

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