Note that there are some explanatory texts on larger screens.

plurals
  1. PODifficulty with a program that prints out the number of days between two separate years selected by the user
    text
    copied!<p>I'm in an intro to C class, and my professor just moved us from python (as an introduction) to C. I'm having a bit of difficulty getting the C syntax down. The program is supposed to calculate the number of days between two separate years and account for leap years.</p> <p>This is the code I have so far:</p> <pre><code>#include &lt;stdio.h&gt; int main() { int startyear, endyear; int totalyears, index; int difference, numyears, days; printf("What is the starting year of your time period?\n"); scanf("%d", &amp;startyear); printf("What is the ending year of your time period?\n"); scanf("%d", &amp;endyear); difference = startyear; numyears = 0; days = 0; while (difference &lt;= endyear) { difference++; numyears++; if (difference / 4 == 1) { days + 1; } } printf("There are %d days between 1/1/%d and 1/1/%d\n", days + numyears * 365 , startyear, endyear); return 0; </code></pre> <p>}</p> <p>When I use 2003 and 2005 as the two years, but the program prints out the result as 4,201,453 days, which is clearly wrong. The actual result should be about 731. I know I've probably made a few errors in my while loop, but I'm not quite sure how to fix it. I'm trying to make sure that the while loop checks to see if the month is divisible by 4, which would mark a leap year, and I assumed that in order to do this, I'd use the line:</p> <pre><code>if (difference / 4 == 1) { days + 1; </code></pre> <p>The "1" meaning to return the boolean expression as "true," but I'm not quite sure if that's how C reads it. I apologize for the long question, but I'm just trying to be as clear as possible. I also apologize because I feel like I'm posting a mess of code, but I really have no idea where to go from here in order to fix this.</p> <p>Thanks in advance, guys and gals.</p> <p><strong>EDIT: I've followed the comments so far, and I've managed to get the program to calculate 1095 days. I'm getting closer, but there must still be something off in my calculations.</strong></p>
 

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