Note that there are some explanatory texts on larger screens.

plurals
  1. POC program days between two dates -- output slightly too high?
    text
    copied!<p>This C program for my college course is supposed to find the number of days between two dates. It works for the most part, but the output is usually just a few days too high when month1 and month2 are different. Also, months which have 31 days don't seem to be recognized as such, resulting in the "Invalid month and day combination" error message when "31" is entered as the day. I'm not at all sure why either of these problems are occurring. Thanks in advance for your help guys!</p> <pre><code>// Calculates the number of calendar days between any two dates in history (beginning with 1/1/1). #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; void date(int *month1, int *day1, int *year1, int *month2, int *day2, int *year2); void leap(int year1, int year2, int *leap1, int *leap2); int main(void) { int month1, day1, year1, month2, day2, year2, leap1, leap2; date(&amp;month1, &amp;day1, &amp;year1, &amp;month2, &amp;day2, &amp;year2); leap(year1, year2, &amp;leap1, &amp;leap2); if(year1 == year2) { int i, total = 0; if(month1 == month2) // Total days if month1 == month2 { total = day2 - day1; printf("There are %d days between the two dates.", total); } else { if(month1 == 1||3||5||7||8||10) // Days remaining in first month total = 31 - day1; else if(month1 == 4||6||9||11) total = 30 - day1; else { if(leap1 == 1) total = 29 - day1; else total = 28 - day1; } for(i = month1 + 1; i &lt; month2; i++) // Days remaining between dates (excluding last month) { if(i == 3||5||7||8||10) total += 31; else if(i == 4||6||9||11) total += 30; else { if(leap1 == 1) total += 29; else total += 28; } } total += day2; // Final sum of days between dates (including last month) printf("There are %d days between the two dates.", total); } } else // If year1 != year2 ... { int i, total, century1 = ((year1 / 100) + 1) * 100, falseleap = 0; if(month1 == 1||3||5||7||8||10||12) // Days remaining in first month total = 31 - day1; else if(month1 == 4||6||9||11) total = 30 - day1; else { if(leap1 == 1) total = 29 - day1; else total = 28 - day1; } for(i = month1 + 1; i &lt;= 12; i++) // Day remaining in first year { if(i == 3||5||7||8||10||12) total += 31; else if(i == 4||6||9||11) total += 30; else { if(leap1 == 1) total += 29; else total += 28; } } for(i = 1; i &lt; month2; i++) // Days remaining in final year (excluding last month) { if(i == 1||3||5||7||8||10) total += 31; else if(i == 4||6||9||11) total += 30; else { if(leap2 == 1) total += 29; else total += 28; } } int leapcount1 = year1 / 4; // Leap years prior to and including first year int leapcount2 = year2 / 4; // Leap years prior to and NOT including final year if(year2 % 4 == 0) leapcount2 -= 1; int leaptotal = leapcount2 - leapcount1; // Leap years between dates for(i = century1; i &lt; year2; i += 100) // "False" leap years (divisible by 100 but not 400) { if((i % 400) != 0) falseleap += 1; } total += 365 * (year2 - year1 - 1) + day2 + leaptotal - falseleap; // Final calculation printf("There are %d days between the two dates.", total); } return 0; } void date(int *month1, int *day1, int *year1, int *month2, int *day2, int *year2) { for(;;) // Infinite loop (exited upon valid input) { int fail = 0; printf("Enter first date: "); scanf("%d/%d/%d", month1, day1, year1); if(*month1 &lt; 1 || *month1 &gt; 12) { printf("Invalid entry for month.\n"); fail += 1; } if(*day1 &lt; 1 || *day1 &gt; 31) { printf("Invalid entry for day.\n"); fail += 1; } if(*year1 &lt; 1) { printf("Invalid entry for year.\n"); fail += 1; } if((*month1 == 4||6||9||11) &amp;&amp; *day1 &gt; 30) { printf("Invalid month and day combination.\n"); fail += 1; } if(*month1 == 2) { if(*year1 % 4 == 0) { if(*year1 % 100 == 0) { if(*year1 % 400 == 0 &amp;&amp; *day1 &gt; 29) { printf("Invalid month and day combination.\n"); fail += 1; } if(*year1 % 400 != 0 &amp;&amp; *day1 &gt; 28) { printf("Invalid month and day combination.\n"); fail += 1; } } if(*year1 % 100 != 0 &amp;&amp; *day1 &gt; 29) { printf("Invalid month and day combination.\n"); fail += 1; } } if(*year1 % 4 != 0 &amp;&amp; *day1 &gt; 28) { printf("Invalid month and day combination.\n"); fail += 1; } } if(fail &gt; 0) continue; else break; } for(;;) { int fail = 0; printf("Enter second date: "); scanf("%d/%d/%d", month2, day2, year2); if(*month2 &lt; 1 || *month2 &gt; 12) { printf("Invalid entry for month.\n"); fail += 1; } if(*day2 &lt; 1 || *day2 &gt; 31) { printf("Invalid entry for day.\n"); fail += 1; } if(*year2 &lt; 1) { printf("Invalid entry for year.\n"); fail += 1; } if((*month2 == 4||6||9||11) &amp;&amp; *day2 &gt; 30) { printf("Invalid month and day combination.\n"); fail += 1; } if(*month2 == 2) { if(*year2 % 4 == 0) { if(*year2 % 100 == 0) { if(*year2 % 400 == 0 &amp;&amp; *day2 &gt; 29) { printf("Invalid month and day combination.\n"); fail += 1; } if(*year2 % 400 != 0 &amp;&amp; *day2 &gt; 28) { printf("Invalid month and day combination.\n"); fail += 1; } } if(*year2 % 100 != 0 &amp;&amp; *day2 &gt; 29) { printf("Invalid month and day combination.\n"); fail += 1; } } if(*year2 % 4 != 0 &amp;&amp; *day2 &gt; 28) { printf("Invalid month and day combination.\n"); fail += 1; } } if(fail &gt; 0) continue; else break; } } void leap(int year1, int year2, int *leap1, int *leap2) // Determines if first and final years are leap years { if(year1 % 4 == 0) { if(year1 % 100 == 0) { if(year1 % 400 == 0) *leap1 = 1; else *leap1 = 0; } else *leap1 = 1; } else *leap1 = 0; if(year2 % 4 == 0) { if(year2 % 100 == 0) { if(year2 % 400 == 0) *leap2 = 1; else *leap2 = 0; } else *leap2 = 1; } else *leap2 = 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload