title: C: 'else' being executed when if condition is satisfied - structures
body: <p>I'm currently learning C from a book, and a question was to write a program to find the time elapsed between 2 times input by the user, which I've done so using a 24 hour format.</p>
  
  <p>The problem is the while loop in the main function does not execute when it should. Which is when the user has entered a time that is out of boundaries (under 0, over 24 hours etc.)</p>
  
  <pre><code>while(error == 1) {
      printf("Enter time 1 in the format hours:mins:sec\n");
      scanf("%i:%i:%i", &amp;time1.hours, &amp;time1.minutes, &amp;time1.seconds);
  </code></pre>
  
  <p>The while loop should then keep executing until the user enters a valid time, but instead it just gets skipped.</p>
  
  <p>I think the reason might be because the function that checks the time and assigns the global variable 'error' to equal 1 or 0 is always executing the else statement (which sets error = 0),even when the if statements are true.</p>
  
  <pre><code>void checkTime (struct time a)
  {   
  
  if(a.hours &lt; 0 || a.hours &gt; 23) {
      printf("Please enter a valid figure for hours in the range 1 to 23\n");
      error = 1;
  }
  
  if(a.minutes &lt; 0 || a.minutes &gt; 59) {
      printf("Please enter a valid figure for minutes in the range of 1 to 59\n");
      error = 1;
  }
  
  if(a.seconds &lt; 0 || a.seconds &gt; 59) {
      printf("Please enter a valid figure for seconds in the range of 1 to 59\n");
      error = 1;
  
  }
  
  else {
      error = 0;
  }
  }
  </code></pre>
  
  <p>Is this a logic problem that has something to do with the way structures work?</p>
  
  <p>here is the full code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  //Program to find the time elapsed between 2 times entered by user
  
  int error = 0;
  
  struct time 
  {
  int hours;
  int minutes;
  int seconds;
  };
  
  //Function to find elapsed time between the 2 arguments
  struct time elapsed_time (struct time time1, struct time time2)
  {
  
  struct time eTime;
  
  
  if (time2.hours &gt; time1.hours)
      eTime.hours = time2.hours - time1.hours;
  else
      eTime.hours = (24 - time1.hours) + time2.hours;
  
  
  if (time2.minutes &gt; time1.minutes)
      eTime.minutes = time2.minutes - time1.minutes;
  else
      eTime.minutes = (60 - time1.minutes) + time2.minutes;
  
  
  if (time2.seconds &gt; time1.seconds)
      eTime.seconds = time2.seconds - time1.seconds;
  else
      eTime.seconds = (60 - time1.seconds) + time2.seconds;
  
  
  return eTime;
  }
  
  //Function to check if inputed times are within valid parameters
  void checkTime (struct time a)
  {   
  
  if(a.hours &lt; 0 || a.hours &gt; 23) {
      printf("Please enter a valid figure for hours in the range 1 to 23\n");
      error = 1;
  }
  
  if(a.minutes &lt; 0 || a.minutes &gt; 59) {
      printf("Please enter a valid figure for minutes in the range of 1 to 59\n");
      error = 1;
  }
  
  if(a.seconds &lt; 0 || a.seconds &gt; 59) {
      printf("Please enter a valid figure for seconds in the range of 1 to 59\n");
      error = 1;
  
  }
  
  else {
      error = 0;
  }
  
  }
  
  
  int main (void)
  {   
  struct time time1;
  struct time time2;
  struct time main_eTime;
  
  printf("Enter time 1 in the format hours:mins:sec\n");
  scanf("%i:%i:%i", &amp;time1.hours, &amp;time1.minutes, &amp;time1.seconds);
  
  checkTime(time1);
  
  while(error == 1) {
      printf("Enter time 1 in the format hours:mins:sec\n");
      scanf("%i:%i:%i", &amp;time1.hours, &amp;time1.minutes, &amp;time1.seconds);
  }
  
  printf("Enter time 2 in the format hours:mins:sec\n");
  scanf("%i:%i:%i", &amp;time2.hours, &amp;time2.minutes, &amp;time2.seconds);
  
  checkTime(time2);
  
  while(error == 1) {
      printf("Enter time 2 in the format hours:mins:sec\n");
      scanf("%i:%i:%i", &amp;time2.hours, &amp;time2.minutes, &amp;time2.seconds);
  }
  
  main_eTime = elapsed_time(time1, time2);
  
  printf("Elapsed time between time 1 and time 2 is %i:%i:%i\n", main_eTime.hours, 
          main_eTime.minutes, main_eTime.seconds);
  
  return 0;
  
  }
  </code></pre>