Home > Enterprise >  Checking the dates in c
Checking the dates in c

Time:03-26

I am writing a program which checks two dates but ı am having problems while doing that. program takes two dates as input and gives an output like this:

  1. date:7/9/12
  2. date:5/4/20

7/9/12 is sooner.

#include <stdio.h>

struct d
{
    int d, m, y;
} date1, date2;

int sooner_date(struct d date1, struct d date2);

int main()
{

    printf(" date1:");
    scanf("%d/%d/%d", &date1.d,&date1.m,&date1.y);
    printf("date 2:");
    scanf("%d/%d%d", &date2.d,&date2.m,&date2.y);

    return 0;
}

int sooner_date(d date1, d date2)
{ 
    // if date1 is sooner turn it -2
    // id date2 is sooner turn it 2
    // if they are equal then turn ıt as 1
}

CodePudding user response:

You have some problems regarding how to use scanf, etc. Here's how you do it:

#include <stdio.h>
#include <stdbool.h>

typedef struct { int d, m, y; } Date;

int compare(Date, Date);

bool prompt_date(Date *);

int main() {
    Date date1, date2;

    if (!prompt_date(&date1) || !prompt_date(&date2)) {
        puts("Invalid date.");
        return 1;
    }

    int result = compare(date1, date2);

    
    if (result > 0)
        puts("date1 is greater than date2");
    else if (result < 0)
        puts("date1 is less than date2");
    else
        puts("date1 is equal to date2");

    return 0;
}

/// <0 lhs is earlier
/// =0 lhs is equal to rhs
/// >0 rhs is earlier
int compare(Date lhs, Date rhs) {
    if (lhs.y - rhs.y) return lhs.y - rhs.y;
    if (lhs.m - rhs.m) return lhs.m - rhs.m;
    return lhs.d - rhs.d;
}

// true on success, false on failure
bool prompt_date(Date *pd) {
    printf("Enter date (dd/mm/yyyy): ");
    return (scanf("%d/%d/%d", &pd->d, &pd->m, &pd->y) == 3);
}

CodePudding user response:

Here is how you can do it. Note that scanf() is not recommended to read input from the user. fgets() and sscanf() are a much safer approach.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

struct date {
    int day;
    int month;
    int year;
};

int compare_dates(struct date *d1, struct date *d2)
{
    if (d1->year  < d2->year)  return -1;
    if (d1->year  > d2->year)  return  1;
    if (d1->month < d2->month) return -1;
    if (d1->month > d2->month) return  1;
    if (d1->day   < d2->day)   return -1;
    if (d1->day   > d2->day)   return  1;
    return 0;
}

bool read_line(char *line, size_t size, FILE *f)
{
    if (!fgets(line, size, f))
        return false;
    
    size_t npos = strcspn(line, "\n");
    line[npos] = '\0';
    return true;
}

bool read_date(struct date *d)
{
    char line[255];
    read_line(line, sizeof line, stdin);
    return sscanf(line, "%d/%d/%d", &d->day, &d->month, &d->year) == 3;
}

void print_date(struct date *d)
{
    printf("%d/%d/%d", d->day, d->month, d->year);
}

int main(void)
{
    struct date d1, d2;
    
    printf("Enter date 1 (format: dd/MM/yyyy): ");
    read_date(&d1);
    
    printf("Enter date 2 (format: dd/MM/yyyy): ");
    read_date(&d2);
    
    int result = compare_dates(&d1, &d2);
    switch (result) {
    case  0: printf("Same dates\n"); break;
    case -1: print_date(&d1); printf(" is sooner\n"); break;
    case  1: print_date(&d2); printf(" is sooner\n"); break;
    }
}
  •  Tags:  
  • c
  • Related