0

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
}
Kathrine
  • 21
  • 5
  • 1
    (1) Please read [formatting help](https://stackoverflow.com/editing-help) then [edit] your post. (2) [Read your compiler warnings](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings). – n. m. could be an AI Mar 25 '22 at 18:48
  • I am new at this side and actually have problems when writing my code in here.İf I do something wrong,sorry for that. – Kathrine Mar 25 '22 at 18:50
  • @n.1.8e9-where's-my-sharem. The bad formatting is a result of the Stacks editor that was deployed this week. – Barmar Mar 25 '22 at 18:50
  • This code is a long way off. The scanf format specifiers don't match up with the arguments, and the function that is supposed to do your assigned task has no actual attempt at doing the task. – WhozCraig Mar 25 '22 at 18:52
  • Your `scanf()` calls are totally wrong. You have 4 `%d`, but only one variable. You can't scan into a struct and have it automatically spread to the members, you have to write `&date1.d, &date1.m, &date1.y`. And what is the 4th `%d` for? – Barmar Mar 25 '22 at 18:52
  • I had a mistake when wrting the fourth %d.Thank you all for helping. – Kathrine Mar 25 '22 at 18:53
  • What part of the function are you having trouble with? Just compare each member of the two variables. If the years are different, return the lower year. If the years are the same, compare the months. If the months are the same, compare the days. – Barmar Mar 25 '22 at 18:54
  • actually how can ı call that parts in main funtion?There is where I have problems. – Kathrine Mar 25 '22 at 18:55
  • If you're just learning, and if your compiler didn't give you any warnings about `scanf("%d/%d/%d", &d.date1);`, you need to get a better compiler, or figure out how to enable warnings for the one you have. There are lots more frustrating mistakes like this waiting to happen, that a good compiler will warn you about! – Steve Summit Mar 25 '22 at 18:56
  • `#ifdef __cplusplus` / `#error bad (or misconfigured) compiler` / `#endif` see [C11 6.10.8p3](https://port70.net/~nsz/c/c11/n1570.html#6.10.8p3): *The implementation shall not predefine the macro `__cplusplus` ...* – pmg Mar 25 '22 at 18:56
  • Yes,I have a lot of warnings and That is why ı want to get help. – Kathrine Mar 25 '22 at 18:57
  • And you're not validating dates? What if date1: `-332423/-2343242/2343242` & date2 is `31/02/1996` ? – जलजनक Mar 25 '22 at 19:12
  • Actually the book I am studying gives the instructions.That is why I am trying to stick that code. – Kathrine Mar 25 '22 at 19:16

2 Answers2

1

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);
}
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
  • May want to mention that it is vital to do bounds checking on the values entered for the day, month and year. Since none can be negative, an `unsigned` type may help. If the day is greater than 31 or month greater than 12, the input would not make much sense. – David C. Rankin Mar 26 '22 at 06:21
  • @DavidC.Rankin Then you also have to consider what months have 30 days, leap years, even integer overflow cases, etc., which is a whole another story. Validation is important, but you can only explain so much in a single StackOverflow answer before it gets out of hand :) – Aykhan Hagverdili Mar 26 '22 at 07:12
  • Yes, no dings for it, it takes a couple of lookup-tables, but worth mentioning -- even if you leave that to the OP to implement `:)` – David C. Rankin Mar 26 '22 at 07:23
0

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;
    }
}
Zakk
  • 1,935
  • 1
  • 6
  • 17