944,183 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 14703
  • C++ RSS
Feb 23rd, 2007
0

Calculating Time Difference

Expand Post »
Hi guys..I am trying to make a program that can calculate the time difference in days from the current date..here is my code...it is bringing some bit of a problem...please help me out!Thanks

C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2. #include <cdate.h>
  3.  
  4. class Date
  5.  
  6. {
  7.  
  8. public:
  9.  
  10. int month;
  11.  
  12. int day;
  13.  
  14. int year;
  15.  
  16. static int dtab[2][13];
  17.  
  18. public:
  19. int getMonth()
  20.  
  21. {
  22. return month;
  23. }
  24.  
  25. int getDay()
  26.  
  27. {
  28. return day;
  29. }
  30.  
  31. int getYear()
  32.  
  33. {
  34. return year;
  35. }
  36.  
  37. Date operator-(Date& d2);
  38.  
  39. Date& operator-()
  40. {
  41. month=-month;
  42.  
  43. day=-day;
  44.  
  45. year=-year;
  46.  
  47. return *this;
  48. }
  49. int compare(Date&);
  50.  
  51. int operator<(Date& d2)
  52.  
  53. {
  54.  
  55. return compare{d2) < 0;
  56.  
  57. }
  58.  
  59. int operator<=(const Date& d2)
  60.  
  61. {
  62.  
  63. return compare(d2) <= 0;
  64.  
  65. }
  66.  
  67. int operator>( Date& d2)
  68.  
  69. {
  70.  
  71. return compare(d2) > 0;
  72.  
  73. }
  74.  
  75. int operator>=( Date& d2)
  76.  
  77. {
  78.  
  79. return compare(d2) >= 0;
  80.  
  81. }
  82.  
  83. int operator==( Date& d2)
  84.  
  85. {
  86.  
  87. return compare(d2) == 0;
  88.  
  89. }
  90.  
  91. int operator!=( Date& d2)
  92.  
  93. {
  94.  
  95. return compare(d2) != 0;
  96.  
  97. }
  98.  
  99. static int isleap(int y)
  100.  
  101. {
  102.  
  103. return y%4 == 0 && y%100 != 0 || y%400 == 0;
  104.  
  105. }
  106.  
  107. };
  108.  
  109. int main()
  110.  
  111. {
  112.  
  113. Date today, d2;
  114.  
  115. cout << "Today's date is "<< today << endl;
  116.  
  117. cout << "Enter another date: ";
  118.  
  119. cin >> d2;
  120.  
  121. cout << "today -d2 = "<< today - d2 << endl;
  122.  
  123. cout << "d2 - today = "<< d2 - today << endl;
  124.  
  125. return 0;
  126.  
  127. }
Similar Threads
hui
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hui is offline Offline
6 posts
since Nov 2006
Feb 23rd, 2007
0

Re: Calculating Time Difference

#1: You don't need to double space everything. It's too hard to read.
#2: You need to indent consistently, and 3-4 spaces, not 1 or 2
#3: "it is bringing some bit of a problem" has no meaning. What is the problem. Describe it, don't make us guess.
Moderator
Reputation Points: 3281
Solved Threads: 896
Posting Sage
WaltP is offline Offline
7,753 posts
since May 2006
Feb 23rd, 2007
0

Re: Calculating Time Difference

Click to Expand / Collapse  Quote originally posted by hui ...
Hi guys..I am trying to make a program that can calculate the time difference in days from the current date..here is my code...it is bringing some bit of a problem...please help me out!Thanks

C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2. #include <cdate.h>
  3.  
  4. class Date
  5.  
  6. {
  7.  
  8. public:
  9.  
  10. int month;
  11.  
  12. int day;
  13.  
  14. int year;
  15.  
  16. static int dtab[2][13];
  17.  
  18. public:
  19. int getMonth()
  20.  
  21. {
  22. return month;
  23. }
  24.  
  25. int getDay()
  26.  
  27. {
  28. return day;
  29. }
  30.  
  31. int getYear()
  32.  
  33. {
  34. return year;
  35. }
  36.  
  37. Date operator-(Date& d2);
  38.  
  39. Date& operator-()
  40. {
  41. month=-month;
  42.  
  43. day=-day;
  44.  
  45. year=-year;
  46.  
  47. return *this;
  48. }
  49. int compare(Date&);
  50.  
  51. int operator<(Date& d2)
  52.  
  53. {
  54.  
  55. return compare{d2) < 0;
  56.  
  57. }
  58.  
  59. int operator<=(const Date& d2)
  60.  
  61. {
  62.  
  63. return compare(d2) <= 0;
  64.  
  65. }
  66.  
  67. int operator>( Date& d2)
  68.  
  69. {
  70.  
  71. return compare(d2) > 0;
  72.  
  73. }
  74.  
  75. int operator>=( Date& d2)
  76.  
  77. {
  78.  
  79. return compare(d2) >= 0;
  80.  
  81. }
  82.  
  83. int operator==( Date& d2)
  84.  
  85. {
  86.  
  87. return compare(d2) == 0;
  88.  
  89. }
  90.  
  91. int operator!=( Date& d2)
  92.  
  93. {
  94.  
  95. return compare(d2) != 0;
  96.  
  97. }
  98.  
  99. static int isleap(int y)
  100.  
  101. {
  102.  
  103. return y%4 == 0 && y%100 != 0 || y%400 == 0;
  104.  
  105. }
  106.  
  107. };
  108.  
  109. int main()
  110.  
  111. {
  112.  
  113. Date today, d2;
  114.  
  115. cout << "Today's date is "<< today << endl;
  116.  
  117. cout << "Enter another date: ";
  118.  
  119. cin >> d2;
  120.  
  121. cout << "today -d2 = "<< today - d2 << endl;
  122.  
  123. cout << "d2 - today = "<< d2 - today << endl;
  124.  
  125. return 0;
  126.  
  127. }
http://www.cprogramming.com/
http://www.cplusplus.com/doc/tutorial/
http://www.codersource.net/codersour...ogramming.html

Good luck, LamaBot
Last edited by Lazaro Claiborn; Feb 23rd, 2007 at 4:39 pm.
Reputation Points: 11
Solved Threads: 13
Junior Poster
Lazaro Claiborn is offline Offline
171 posts
since Jan 2007
Feb 23rd, 2007
0

Re: Calculating Time Difference

difference in days between two dates is very easy to calculate.
1. get time_t for first date. If this is not today's date, then fill out a struct tm with day, month-1, year-1900 -- make all other structure members 0, then pass your struct tm object to mktime(), which will convert it to time_t.
2. get time_t for second date
3. subtract them -- this is difference between two dates in seconds
4. there are 24 * 60 * 60 seconds in one day. So take the result of step 3 above and divide by the number of seconds in one day.
5. finished.

  1. #include <iostream.h>
  2. #include <cdate.h>
1. iostream.h is very old and obsolete. current version does not have .h file extension. If you are using an acient compiler such as Turbo C++ then this is ok because your compiler probably does not support the new file naming convention.

2. cdate.h -- there is no such file. use either date.h or cdate (no file extension). This is how it might appear for modern compilers.

  1. #include <iostream>
  2. #include <cdate>
Last edited by Ancient Dragon; Feb 23rd, 2007 at 6:37 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Problems installing MySQL++ wrapper for C++ language
Next Thread in C++ Forum Timeline: Study problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC