lundi 25 octobre 2021

Slight change in input makes the program go nuts

I have this piece of code to check whether second date is earlier than the first date the program works normally when I input these with this order without commas 9,9,2021 but when I do input 09 for the first input It skips the second input and directly jumps into the third input I can't figure out why this is the case since I am fairly new to C

#include <stdio.h>

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

// ANSI color codes
#define RED "\x1b[31m"
#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]);

    // Checking if the second date's year is less than the first one
    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");
    }
    // Checking if the second date's month is less than the first one
    else 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");
    }
    // If months are equal then check the day
    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;
}


Aucun commentaire:

Enregistrer un commentaire