days between dates.

Hey guys, I'm pretty new here and I need some help with a program I'm suppose to do.
I have to make a program that counts days between two dates, from 1900 and onwards. And I have to have a main function and 3 help functions.
This is what I've made so far, any feedback would be great.


Code - See below!


So far it works well with dates over a year between and not taking in account leap years, and that's the problem.

So first question is, why can't it calculate within a year? for example setting the dates to "2001 1 1" and the second to "2001 2 2"? What did I do wrong.

The line "/*leapYear(year)? days = 29 :*/" is suppose to check if the leapYear function is true or false, the problem is I don't know how to define the true or false and use it, how do I do that?

I don't expect you to make the whole program for me, because that would be pointless, however any feedback or tips on the program would be awesome. It's not finished yet ofcourse, and it may look pretty ugly, but I'll fix that later when I finish it :)

Maybe some of you know a better way to use function daysFrom1900?

Cheers for any feedback!
Last edited on
Please use code tags so your code is readable.

In your daysFrom1990() function, you don't need to re-assign a bunch of variables like you are doing. Just use the parameters directly if you want to. You should also try to give more meaningful names to your variables. I have no idea what "q" means just by looking at it. In general, if you are using single letter variable names and you aren't working with some sort of equation for which they are often used, you probably should think of a better name.
Hi,

Sample a date code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

typedef struct {
int mDay, mMon, mYear;
};

#define isleap(y)		((y) % 4 == 0 && ((y) % 100 || (y) % 400 == 0))


static const int daytabs[2][13] = {
	{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
	{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
};


Date *setdate(Date *p, int day, int mon, int year)
{
	return set(p, day, mon, year);
}

Date *setToday(Date *p)
{
	time_t timer = time(NULL);
	struct tm *tp = localtime(&timer);
	return set(p, tp->tm_mday, tp->tm_mon + 1, tp->tm_year + 1900);
}

static Date *set(Date *p, int d, int m, int y)
{
	if (!isValidDate(d, m, y)){	
	       cout << "gecersiz tarih : " << d  << m  << y << endl;
	       exit(EXIT_FAILURE);
	}

	p->mDay = d;
	p->mMon = m;
	p->mYear = y;

	return p;

}

I just made some changes to function daysFrom1900 so now it works to calculate days in a year :)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using namespace std;

int daysInMonth (int month);
int leapYear (int year);
int daysFrom1900 (int year, int month, int day, int year2, int month2, int day2);

int main()
{

    int year, year2, month, month2, day, day2;
    
    cout << "Enter a year: ";
    
    cin >> year >> month >> day >> year2 >> month2 >> day2;
    
    int counter = daysFrom1900(year, month, day, year2, month2, day2);
    
    cout << counter;
    
    system("pause");
    return 0;
}



int leapYear (int year)
{
  int a = 365;

  if( year % 4 == 0 ){
      if(year % 100 == 0 && year % 400 != 0){
              FALSE;
      }
      TRUE;
  }
 FALSE;

}


int daysInMonth (int month)
{

    int days;
    switch (month){
           case 1 : days = 31; break;
           case 2 : /*leapYear(year)? days = 29 :*/ days = 28; break;
           case 3 : days = 31; break;
           case 4 : days = 30; break;
           case 5 : days = 31; break;
           case 6 : days = 30; break;
           case 7 : days = 31; break;
           case 8 : days = 31; break;
           case 9 : days = 30; break;
           case 10 : days = 31; break;
           case 11 : days = 30; break;
           case 12 : days = 31; break;
       }
    return days;
}




int daysFrom1900 (int year, int month, int day, int year2, int month2, int day2)
{
    
    int y, m, d, y2, m2, d2;
    
    y = year;
    y2 = year2;
    m = month;
    m2 = month2;
    d = day;
    d2 = day2;
    int counter = 0;
    
    
    
         if (y == y2)
              for ( int a = 1; a <= m2; a++)
                  if (a == m2){
                     for (int b = 1; b <= d2; b++)
                         q++;
                         }
                  else {
                     for (int b = 1; b <= daysInMonth(a); b++)
                         counter++;
                         }
                      
         else
             for (int c = y; c <= y2; c++){
    
                 if (c == y){
                    for ( int a = m; a <= 12; a++)
                         for (int b = d; b <= daysInMonth(a); b++)
                            counter++;
                             }
                 else if (y == y2){
                      for ( int a = 1; a <= m2; a++)
                           if (a == m2){
                                for (int b = 1; b <= d2; b++)
                                      counter++;
                                       }
                            else 
                                for (int b = 1; b <= daysInMonth(a); b++)
                                      counter++;
                                        }
                                        
                 else if (c != y){
                      for ( int a = 1; a <= 12; a++)
                           for (int b = 1; b <= daysInMonth(a); b++)
                               counter++;
                               }
             }
                     
        return counter - 1;
      
}
Last edited on
Topic archived. No new replies allowed.