Problem calculating days between 2 dates

Reply

Join Date: Apr 2009
Posts: 20
Reputation: patrick k is an unknown quantity at this point 
Solved Threads: 1
patrick k patrick k is offline Offline
Newbie Poster

Problem calculating days between 2 dates

 
0
  #1
Jun 19th, 2009
Hi everyone!
I'm trying to learn C from my dad's old college textbooks, and I'm doing some of the problems he had to do. This problem is meant to calculate the number of days between two dates given by the user. I made a function to account for leap years, and to input the data. My program is running, but is coming up with numbers that are just a few days off. I already wrote this program in Pascal, and it works, but I'm trying to practice it in C.

Here's the source code.

  1. /*This program counts the days between two dates input by the user*/
  2. #include <stdio.h>
  3.  
  4. int yr1; /*global variables because all these need to be visible by multiple functions*/
  5. int yr2; /*I don't know how to use pointers to return more than 1 variable from a function*/
  6. int m1;
  7. int m2;
  8. int d1;
  9. int d2;
  10. int leap;
  11.  
  12. main()
  13. {
  14. int numdays [13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; /*13 members*/
  15. int totaldays; /*so that 1 would match up with January, fixing notation problems*/
  16. int year;
  17. int month;
  18. int day;
  19. inputdata();
  20. for (year = (yr1 + 1); year < yr2; year ++) /*processes complete years*/
  21. {
  22. leapyr(year);
  23. if (leap == 1)
  24. totaldays += 366;
  25. else totaldays += 365;
  26. }
  27. year = yr1;
  28. for(month = m1 + 1; month <= 12; month++) /*processes incomplete year 1*/
  29. {
  30. leapyr (year);
  31. if (leap == 1)
  32. numdays[2] = 29;
  33. else numdays[2] = 28;
  34. totaldays = totaldays + numdays[month];
  35. }
  36. month = m1;
  37. for(day = d1; day <= numdays [month]; day++) /*processes incomplete month 1*/
  38. {
  39. leapyr (year);
  40. if (leap == 1)
  41. numdays[2] = 29;
  42. totaldays += 1;
  43. }
  44.  
  45. year = yr2;
  46. for(month = m2 - 1; month >= 1; month--) /*processes incomplete year 2*/
  47. {
  48. leapyr (year);
  49. if (leap == 1)
  50. numdays[2] = 29;
  51. totaldays += numdays[month];
  52. }
  53. month = m2;
  54. for(day = d2; day > 1; --day) /*processes incomplete month 2*/
  55. {
  56. totaldays += 1; /*no need for leapyr function here- don't need to know month max*/
  57. }
  58. printf("Total number of days between two given dates is: %d days", totaldays);
  59. getch();
  60.  
  61.  
  62. }
  63.  
  64. int inputdata() /*function works as planned- already tested*/
  65. {
  66. printf("This program calculates the number of days between two given dates\n");
  67. printf("Enter beginning date in form month/day/year: ");
  68. scanf( "%d/%d/%d", &m1, &d1, &yr1);
  69. printf("Enter ending date in form month/day/year: ");
  70. scanf("%d/%d/%d", &m2, &d2, &yr2);
  71. }
  72.  
  73. int leapyr (year)
  74. {
  75. int leap = 0;
  76. if (year % 4 == 0)
  77. leap = 1;
  78. else
  79. if (year % 100 == 0)
  80. leap = 0;
  81. else
  82. if (year % 400 == 0)
  83. leap = 1;
  84. printf("leap is: %d\n", leap);
  85. return leap;
  86. }

Thanks for the help!
Last edited by patrick k; Jun 19th, 2009 at 12:54 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Problem calculating days between 2 dates

 
0
  #2
Jun 19th, 2009
here's one problem that immediately jumps out:
  1. leapyr (year);
  2. if (leap == 1)

you need to assign the variable leap to the return value of the function
  1. leap = leapyr(year)

but, more importantly, you should really consider using the standard C library <time.h>

pay attention to the time_t and tm structures. it will reduce your code to a few lines to accomplish the same thing.


.
Last edited by jephthah; Jun 19th, 2009 at 1:11 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Problem calculating days between 2 dates

 
0
  #3
Jun 19th, 2009
IMO the easiest way is to convert both date strings into time_t integers, then just subtract the two integers. That will give you the difference in seconds. After that its just dividing up the seconds into minutes, hours, and days.

The instructions say nothing about calculating the number of years between two dates. so leap years are not relevant to the problem.

  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. time_t tokenizedate(const char* datestr)
  6. {
  7. struct tm tm;
  8. memset(&tm,0,sizeof(struct tm));
  9. tm.tm_mday = ((datestr[0] - '0') * 10) + (datestr[1] - '0');
  10. tm.tm_mon = ((datestr[2] - '0') * 10) + (datestr[3] - '0') - 1;
  11. tm.tm_year = atoi(&datestr[4]) - 1900;
  12. return mktime(&tm);
  13. }
  14.  
  15. int main()
  16. {
  17. time_t t1, t2;
  18. char date1[40], date2[40];
  19. time_t seconds = 0, minutes = 0, hours = 0, days = 0, months = 0;
  20. printf("Enter first date (DDMMYYYY format)\n");
  21. fgets(date1, sizeof(date1), stdin);
  22. printf("Enter second date (DDMMYYYY format)\n");
  23. fgets(date2, sizeof(date2), stdin);
  24. t1 = tokenizedate(date1);
  25. t2 = tokenizedate(date2);
  26. seconds = t2 - t1;
  27. minutes = seconds / 60;
  28. hours = seconds /(60 * 60);
  29. days = seconds / (60 * 60 * 24);
  30.  
  31.  
  32. }
Last edited by Ancient Dragon; Jun 19th, 2009 at 1:22 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 20
Reputation: patrick k is an unknown quantity at this point 
Solved Threads: 1
patrick k patrick k is offline Offline
Newbie Poster

Re: Problem calculating days between 2 dates

 
0
  #4
Jun 19th, 2009
Originally Posted by jephthah View Post
you need to assign the variable leap to the return value of the function
.
I may be showing my ignorance... I thought return leap; was assigning leap the the return value of the function. Or is that something different?
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 20
Reputation: patrick k is an unknown quantity at this point 
Solved Threads: 1
patrick k patrick k is offline Offline
Newbie Poster

Re: Problem calculating days between 2 dates

 
0
  #5
Jun 19th, 2009
I've never dealt with the library functions in this program... I've only been programming in C for a couple days actually... So how can I figure out the functions in the library? Right now some of this is just gibberish to me.... Maybe it's just because I haven't had much experience yet though.
Last edited by patrick k; Jun 19th, 2009 at 1:40 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Problem calculating days between 2 dates

 
0
  #6
Jun 19th, 2009
ahhh... i see what you're trying to do.... i wasnt really paying attention ... and no, what you're doing is not quite right.

think of this:

  1. returnValue = myFunction(argument);

whatever the function *returns* is assigned via the assignment operator, in this case to the variable "returnValue".

the variable you pass in to the function, the "argument" can either be an input or an output or both.

in your case, leapyr(leap); you are passing the *value* of the argument, which in your example is "leap". leap, in your case is strictly undefined in the "main" routine, you have not given it a value. it may be zero, it may be negative eleventy-thousand, you dont know.

your function "leapyr" gets this undefined "leap" value and throws it around and changes it to something meaningful, then tries to *return* it back to the calling function (main), who ignores it, since the return value is unassigned.

meanwhile, "main" still has the original, locally-scoped variable "leap" which is still unknown, and anything that the "leapyr" function did to it was just temporary within the "leapyr" function, and so is lost on "leapyr"'s memory stack.

so, the easiest fix is to redefine your function, leapyr, to look like this:
  1. int leapyr(void)
  2. {
  3. int leap; // locally defined, not the same variable in "main"
  4.  
  5. // do your stuff
  6.  
  7. return leap;
  8. }

and when you call leapyr, call it like so

  1. leap = leapyr();

it's very important that you understand this concept: the variable "leap" that you declared (but did not define) that is local to the "main()" function, will NOT be the same variable as "leap" defined local to the "leapyr" function. they will be two totally independent and completely unrelated variables, just like a guy named "Joe Smith" in New York is unrelated to some other "Joe Smith" in Los Angeles.

the only way they are connected, is that the numeric *VALUE* that the one contains is *RETURNED* through the function and that value is assigned to the other. the variables may as well have two different names, there would be no difference.
Last edited by jephthah; Jun 19th, 2009 at 1:46 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 20
Reputation: patrick k is an unknown quantity at this point 
Solved Threads: 1
patrick k patrick k is offline Offline
Newbie Poster

Re: Problem calculating days between 2 dates

 
0
  #7
Jun 19th, 2009
I understand that declaring leap in the main function and in the leapyr function are different, but that's why I declared leap globally, which I know is bad practice, but I did it anyway. I thought that made leap visible to all functions, and thus any changes applied by one function could be seen by other functions. Is this in concept wrong?
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Problem calculating days between 2 dates

 
0
  #8
Jun 19th, 2009
Originally Posted by patrick k View Post
I've never dealt with the library functions in this program... I've only been programming in C for a couple days actually... So how can I figure out the functions in the library? Right now some of this is just gibberish to me.... Maybe it's just because I haven't had much experience yet though.
well, you're doing fine then. good job programming your day calculator "the hard way"

library functions are very powerful. essentially, they're just routines someone else has written to do some operation that is commonly used.

like "printf" is a library function (found in the <stdio.h> library) to print stuff to the terminal. <time.h> is another such library. learn the rules to use the functions in that library and life gets much easier.

look at Ancient Dragon's example above. it's probably a bit complex to you right now, but its easy to understand if you spend some time on it.

teh best way to learn is to use a debugger and "step through" the code one line at a time and watch the variables as they change values.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Problem calculating days between 2 dates

 
0
  #9
Jun 19th, 2009
Originally Posted by patrick k View Post
I understand that declaring leap in the main function and in the leapyr function are different, but that's why I declared leap globally
good lord, i really am not paying attention, am i?

i'm sorry.

let me re-look at this.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 20
Reputation: patrick k is an unknown quantity at this point 
Solved Threads: 1
patrick k patrick k is offline Offline
Newbie Poster

Re: Problem calculating days between 2 dates

 
0
  #10
Jun 19th, 2009
When I compiled this and added a few printf lines to help with the debugging before I asked for help, the leapyr function seemed to be working as planned. The problem was located somewhere else.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC