-2

there is an easy way that includes all of the "days" conditions that can be in the year? leap year, long/shot months, etc... trying to make it shorter but easy to understand for someone new at code like me

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

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

int main()
{
    date d;
    printf("\nPlease enter the date DD/MM/YYYY format and i will raise it up \n");
    scanf_s("%d/%d/%d", &d.day, &d.month, &d.year);
    printf("you enter: %02d/%02d/%04d", d.day, d.month, d.year);
    d.day++;
    if (d.day == 29 && d.month == 2)
    {
        d.month++;
        d.day = 1;
    }
    if (d.day == 32 && d.month == 3)
    {
        d.month++;
        d.day = 1;
    }
    if (d.day == 32 && d.month == 12)
    {
        d.year++;
        d.month = 1;
        d.day = 1;
    }
    printf("  tomorrow date: %02d/%02d/%04d", d.day, d.month, d.year);
}
daniel
  • 3
  • 3
  • 2
    `if (d.day == 32 && d.month == 12)` ? – 001 Dec 29 '20 at 20:47
  • 1
    Why you dont' make condition of leap year ? – MED LDN Dec 29 '20 at 20:50
  • 1
    You don't seem to validate that the date the user entered is plausible. You don't check that `scanf_s()` worked. Given a valid month and a day, you can check whether the current day is the last day of the month, and if it is, cycle to the first of the next month, remembering to change the year too. At the moment, your code only handles March correctly; December has been resolved; but you still need to sort out the other months, remembering to handle leap years for February. – Jonathan Leffler Dec 29 '20 at 21:02
  • Anyway stop using Turbo C. It's obsolete. You should conform to the C standards – anotherOne Dec 29 '20 at 22:00

3 Answers3

1

An easy way to include all conditions would be to use the standard library <time.h>. It defines a structure to hold the date, struct tm (so you don't need to create your own) and has many useful functions such as mktime, which adjusts the date taking care of everything (change of month, change of year, leap years etc). Here is an example:

#include <stdio.h>
#include <conio.h>
#include <time.h>

int main(void)
{
    struct tm d;

    printf("Please enter the date DD/MM/YYYY format and I will raise it up\n");
    scanf_s("%d/%d/%d", &d.tm_mday, &d.tm_mon, &d.tm_year);
    d.tm_mon -= 1; // struct tm saves months from 0 to 11
    d.tm_year -= 1900; // struct tm requires years passed since 1900

    printf("You entered: %02d/%02d/%04d\n", d.tm_mday, d.tm_mon + 1, d.tm_year + 1900);

    d.tm_mday++;
    mktime(&d); // Make the necessary adjustments after having modified the day

    printf("Tomorrow date: %02d/%02d/%04d\n", d.tm_mday, d.tm_mon + 1, d.tm_year + 1900);
}

Here I used printf so I had to re-add the values I subtracted before, but <time.h> actually includes functions to easily print the date both in standard (asctime, ctime) and custom formats (strftime). There are many resources online (including SO!) to understand in depth everything it offers, have a look!

Sidenote: you don't use anything from <stdlib.h>, you should't include unnecessary libraries

MDXZ
  • 160
  • 12
  • Uhm, I don't, I code in VIM and compile with Clang, why? – MDXZ Dec 29 '20 at 21:35
  • I used to relate `conio.h` to Turbo C. https://stackoverflow.com/questions/59812014/why-use-conio-h Anyways just use the standard stuff – anotherOne Dec 29 '20 at 21:40
  • Well, I didn't even know about that library before today, I just copied it from OP (as a matter of fact when I tested the code I used `scanf` instead of `scanf_s`) – MDXZ Dec 29 '20 at 21:43
0

To answer your original question: "What am I doing wrong?"

if (d.day == 29 && d.month == 2)

Should be

if (d.day == 28 && d.month == 2)

And in the same way 32 should be 31

To answer the question that you posted as an answer: "there is an easy way that includes all of the "days" conditions that can be in the year?"

const int dInM[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

if( d.day == dInM[d.month-1] ) {
    
    ++d.month;
    d.day = 1;
}
anotherOne
  • 1,513
  • 11
  • 20
  • and you use a this condition for leap year ; ((d.year%400==0)||(d.year%4==0&&d.year%100!=0)) – MED LDN Dec 29 '20 at 21:21
  • 2
    The line `d.day++;` makes the first part of your answer incorrect. And the 2nd part does not increase the year in December. – 001 Dec 29 '20 at 21:49
  • I don't have any `d.day++` in the answer and yes it doesn't account for the year stuff. It was meant to be an answer to his particular question, not a working program. – anotherOne Dec 29 '20 at 21:54
  • I think your comment was for MED LDN answer. Lol – anotherOne Dec 29 '20 at 21:58
0
#include <stdio.h>

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

int main()
{
  date d;
  printf("\nPlease enter the date DD/MM/YYYY format and i will raise it up \n");
  scanf("%d%d%d",&d.day, &d.month,&d.year);
  printf("you enter: %02d/%02d/%04d", d.day, d.month, d.year); //Keep in mind that the %02d means two characters minimum width,The 0 means to pad the field using zeros and the 2 means that the field is two characters wide
  d.day++;

  switch(d.month)
  {
      case 1:
      case 3:
      case 5:
      case 7:
      case 8:
      case 10:
      case 12:
      {
           if(d.day>=32)
           {
                 d.month++;
                 d.day=d.day-31;
           }
      }break;
      case 4:
      case 6:
      case 9:
      case 11:
      {
           if(d.day>=31)
           {
                 d.month++;
                 d.day=d.day-30;
           }break;
      }
      case 2:
      {
             if ((d.year%400==0)||(d.year%4==0&&d.year%100!=0)&&d.day>=30)
             {
                   d.month++;
                   d.day=d.day-29;
             }
             else
             {
                   d.month++;
                   d.day=d.day-28;
             }
      }break;

  }

   printf("\ntomorrow date: %02d/%02d/%04d\n", d.day, d.month, d.year);
}
MED LDN
  • 684
  • 1
  • 5
  • 10
  • Here's @Jonny_Mopp comment to your answer: "The line d.day++; makes the first part of your answer incorrect. And the 2nd part does not increase the year in December. " – anotherOne Dec 29 '20 at 22:03