dimanche 24 octobre 2021

Why this program skips third input?

#include <stdio.h>

// Array format [DD,MM,YYYY]
int date[3];
int secondDate[3];

// ANSI color codes
#define GREEN "\x1b[32m"

int main() {
  printf("Enter the day of first date: ");
  scanf("%i", & date[0]);

  printf("Enter the month of first date: ");
  scanf("%i", & date[1]);

  printf("Enter the year of first date: ");
  scanf("%i", & date[2]);

  printf("Enter the day of the second date: ");
  scanf("%i", & secondDate[0]);

  printf("Enter the month of the second date: ");
  scanf("%i", & secondDate[1]);

  printf("Enter the year of the second date: ");
  scanf("%i", & secondDate[2]);

  if (secondDate[2] < date[2]) {
    printf(GREEN "Second date is earlier than the first \n");
  } else if (secondDate[2] > date[2]) {
    printf("Second date is not earlier than the first \n");
  } else if (secondDate[2] == date[2] ) {
      if (secondDate[1] < date[1]) {
        printf(GREEN "Second date is earlier than the first \n");
      } else if (secondDate[1] > date[1]) {
        printf("Second date is not earlier than the first \n");
      }
      else if (secondDate[1] == date[1]) {
        if (secondDate[0] < date[0]) {
          printf(GREEN "Second date is earlier than the first \n");
        } else if (secondDate[0] == date[0]) {
          printf("The dates are basically the same \n ");
        }
      }

  }

  return 0;
}

What would be the reason for this code to skip the last scanf function and jump right into the if else statements thus cause a wrong calculation? When the given inputs are used 10,10,2021 and 10,10 at this point it jumps to the if else statements

Aucun commentaire:

Enregistrer un commentaire