2016-11-21 5 views
0

私はユーザーの入力を受け取り、有効な日付かどうかを言うが、いくつかの日付は有効であるか無効であるかを印刷し、それ以外の時間は入力を受け取り、プログラムは出力なしで終了する。いくつかの数字セットが機能していて、他のセットは機能していないという私の機能には何が問題なのですか?

ユーザーが15/11/11と入力すると、11/15/15に並べ替えられます。ユーザーが 12/12/12と入力した場合は正常に動作しますが、04/16/16では出力が得られません。

私の機能でこの問題の原因はどこですか?

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

//Declares function 
int check_date(int month, int day, int year); 

int main(void) { 
    int day, month, year; 

    //Asks user for input. 
    printf("Enter a date in this format : MM/DD/YY:"); 


    //If user enters a wrong format it will be invalid. 
    if (scanf_s("%d/%d/%d", &month, &day, &year) == 3) { 
     check_date(day, month, year); 
    } 
    else { 
     printf("You entered a invalid date.\n"); 
    } 
    system("pause"); 
    return 0; 

} 

int check_date(int month, int day, int year) { 

    // If user inputs a valid date this will run. 
    if (month > 0 && month <= 12 && day > 0 && day <= 31 && year > 0 && year <= 99) 
    { 
     switch (month) { 

     //If users month input is 2(febuary) this case will run. 
     case 2: 
      //If it is a leap year and the day 29 is entered it is invalid. 
      if (year % 4 == 0) { 
       if (day == 29) { 
        printf("Invalid date."); 
       } 
       else { 
        printf("You entered a valid date, %d/%d/%d", month, day, year); 
       } 
       break; 
     //If user inputs 4 this case will run. 
     case 4: 
      //There is not 31 days in this month. 
      if (day == 31) { 
       printf("Invalid date."); 
      } 
      else { 
       printf("You entered a valid date, %d/%d/%d", month, day, year); 
      } 
      break; 

     //If user inputs 4 this case will run. 
     case 6: 
      //There is not 31 days in this month. 
      if (day == 31) { 
       printf("Invalid date."); 
      } 
      else { 
       printf("You entered a valid date, %d/%d/%d", month, day, year); 
      } 
      break; 

     //If user inputs 4 this case will run. 
     case 9: 
      //There is not 31 days in this month. 
      if (day == 31) { 
       printf("Invalid date."); 
      } 
      else { 
       printf("You entered a valid date, %d/%d/%d", month, day, year); 
      } 
      break; 
     //If user inputs 4 this case will run. 
     case 11: 
      //There is not 31 days in this month. 
      if (day == 31) { 
       printf("Invalid date."); 
      } 
      else { 
       printf("You entered a valid date, %d/%d/%d", month, day, year); 
      } 
      break; 
     //If user enters any other date other then the cases it is valid. 
     default: 
      printf("You entered a valid date, %d/%d/%d", month, day, year); 
      break; 
     } 
      //If user enters anything other then the correct format, this else will run. 
      else { 
       printf("You entered a invalid date."); 
      } 
     } 
    } 
} 
+1

よりシンプルで、このコードを試してみてください?これを判断できる入力値を教えてください。 –

+0

[コードフォーマッタ](http://codebeautify.org/c-formatter-beautifier)を使用してください。開いている括弧を1つ閉じるのを忘れて、 'switch(..)else {INVALID; } 'は構文的には有効ですが、意味はありません。これはタイプミス/書式設定の問題として閉じることにしました。 –

+0

そして 'check_date(day、month、year)'関数を、間違った順序で呼び出します。 –

答えて

0

加工した日付としませんでした日付どのどの

/*C program to validate date (Check date is valid or not).*/ 

#include <stdio.h> 

int main() 
{ 
    int dd,mm,yy; 

    printf("Enter date (DD/MM/YYYY format): "); 
    scanf("%d/%d/%d",&dd,&mm,&yy); 

    //check year 
    if(yy>=1900 && yy<=9999) 
    { 
     //check month 
     if(mm>=1 && mm<=12) 
     { 
      //check days 
      if((dd>=1 && dd<=31) && (mm==1 || mm==3 || mm==5 || mm==7 || mm==8 || mm==10 || mm==12)) 
       printf("Date is valid.\n"); 
      else if((dd>=1 && dd<=30) && (mm==4 || mm==6 || mm==9 || mm==11)) 
       printf("Date is valid.\n"); 
      else if((dd>=1 && dd<=28) && (mm==2)) 
       printf("Date is valid.\n"); 
      else if(dd==29 && mm==2 && (yy%400==0 ||(yy%4==0 && yy%100!=0))) 
       printf("Date is valid.\n"); 
      else 
       printf("Day is invalid.\n"); 
     } 
     else 
     { 
      printf("Month is not valid.\n"); 
     } 
    } 
    else 
    { 
     printf("Year is not valid.\n"); 
    } 

    return 0;  
} 
関連する問題