Advertisement
Yonka2019

Untitled

Dec 8th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.07 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {  
  5.     int day = 0, month = 0, year = 0, dayLimit = 31;
  6.  
  7.     printf("Input the date (syntax: dd.mm.yyyy (example: 08.12.2005) (year should be lower or equals to 2017)): ");
  8.     scanf("%d.%d.%d", &day, &month, &year);
  9.     getchar(); //clean buffer
  10.  
  11.     if ((day < 1 || day > 31) || (month < 1 || month > 12) || (year < 1 || year > 2017)) //check if wrong input (check 1/2)
  12.     {
  13.         printf("[a] wrong input.\n");
  14.         return -1; //stop program
  15.     }
  16.     switch(month) //check if the number of days correct (according the month, for example: in february - 28 days, and not 29) (check 2/2)
  17.     {
  18.         case 2: //check if the month equals to february, and if the days don't greater than 28
  19.             if (day > 28)
  20.             {
  21.                 printf("[b] wrong input.\n");
  22.                 return -1; //stop program
  23.             }
  24.             dayLimit = 28; //setting the limit of the days in this month to 28
  25.             break;
  26.  
  27.         case 4: //check if the month equals to: april(4), june(6), september(9), november(11), and if the days don't greater than 30
  28.         case 6:
  29.         case 9:
  30.         case 11:
  31.             if (day > 30)
  32.             {
  33.                 printf("[b] wrong input.\n");
  34.                 return -1; //stop program
  35.             }
  36.             dayLimit = 30; //setting the limit of the days in this month to 30
  37.             break;
  38.     }
  39.    
  40.     day++; //adding one day
  41.  
  42.     if (day > dayLimit) //checks if the day passed the limit (according the months)
  43.     {
  44.         month++; //increase month by 1
  45.         day = 1;
  46.  
  47.         if (month > 12) //checks if the month passed the max limit (12)
  48.         {
  49.             year++; //increase year by 1
  50.             month = 1;
  51.         }
  52.     }
  53.     printf("The next day will be: %02d.%02d.%04d\n", day, month, year);
  54.     //02d -> if the days lower than 2 numbers it will add 0 to the beginning (days: 2, output: 02)
  55.     //04d -> if the year lower than 4 numbers it will add zeros to the beginning (year: 10, output: 0010)
  56.        
  57.     getchar();
  58.     return 0;
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement