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

#include <iostream.h>
#include <cdate.h>
 
class Date
 
{
 
public:
 
 int month;
 
 int day;
 
 int year;
 
 static int dtab[2][13];
 
public:
 int getMonth()
 
 {
  return month;
 }
 
 int getDay()
 
 {
  return day;
 }
 
 int getYear()
 
 {
  return year;
 }
 
 Date operator-(Date& d2);
 
 Date& operator-()
 {
  month=-month;
 
  day=-day;
 
  year=-year;
 
  return *this;
 }
 int compare(Date&);
 
 int operator<(Date& d2)
 
 {
 
  return compare{d2) < 0;
 
 }
 
 int operator<=(const Date& d2) 
 
    {
 
  return compare(d2) <= 0;
 
 }
 
   int operator>( Date& d2) 
 
    {
 
    return compare(d2) > 0;
 
 }
 
   int operator>=( Date& d2) 
 
     {
 
    return compare(d2) >= 0;
 
 }
 
   int operator==( Date& d2) 
 
     {
 
    return compare(d2) == 0;
 
 }
 
   int operator!=( Date& d2) 
 
     {
 
    return compare(d2) != 0;
 
 }
 
 static int isleap(int y)
 
     {
 
  return y%4 == 0 && y%100 != 0 || y%400 == 0;
 
 }
 
 };
 
int main()
 
{  
 
 Date today, d2;
 
 cout << "Today's date is "<< today << endl;
 
 cout << "Enter another date: ";
 
 cin >> d2;
 
 cout << "today -d2 = "<< today - d2 << endl;
 
 cout << "d2 - today = "<< d2 - today << endl;
 
   return 0;
 
}

Recommended Answers

All 3 Replies

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

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

#include <iostream.h>
#include <cdate.h>
 
class Date
 
{
 
public:
 
 int month;
 
 int day;
 
 int year;
 
 static int dtab[2][13];
 
public:
 int getMonth()
 
 {
  return month;
 }
 
 int getDay()
 
 {
  return day;
 }
 
 int getYear()
 
 {
  return year;
 }
 
 Date operator-(Date& d2);
 
 Date& operator-()
 {
  month=-month;
 
  day=-day;
 
  year=-year;
 
  return *this;
 }
 int compare(Date&);
 
 int operator<(Date& d2)
 
 {
 
  return compare{d2) < 0;
 
 }
 
 int operator<=(const Date& d2) 
 
    {
 
  return compare(d2) <= 0;
 
 }
 
   int operator>( Date& d2) 
 
    {
 
    return compare(d2) > 0;
 
 }
 
   int operator>=( Date& d2) 
 
     {
 
    return compare(d2) >= 0;
 
 }
 
   int operator==( Date& d2) 
 
     {
 
    return compare(d2) == 0;
 
 }
 
   int operator!=( Date& d2) 
 
     {
 
    return compare(d2) != 0;
 
 }
 
 static int isleap(int y)
 
     {
 
  return y%4 == 0 && y%100 != 0 || y%400 == 0;
 
 }
 
 };
 
int main()
 
{  
 
 Date today, d2;
 
 cout << "Today's date is "<< today << endl;
 
 cout << "Enter another date: ";
 
 cin >> d2;
 
 cout << "today -d2 = "<< today - d2 << endl;
 
 cout << "d2 - today = "<< d2 - today << endl;
 
   return 0;
 
}

http://www.cprogramming.com/
http://www.cplusplus.com/doc/tutorial/
http://www.codersource.net/codersource_cppprogramming.html

Good luck, LamaBot

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.

#include <iostream.h>
#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.

#include <iostream>
#include <cdate>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.