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.");
}
}
}
}
よりシンプルで、このコードを試してみてください?これを判断できる入力値を教えてください。 –
[コードフォーマッタ](http://codebeautify.org/c-formatter-beautifier)を使用してください。開いている括弧を1つ閉じるのを忘れて、 'switch(..)else {INVALID; } 'は構文的には有効ですが、意味はありません。これはタイプミス/書式設定の問題として閉じることにしました。 –
そして 'check_date(day、month、year)'関数を、間違った順序で呼び出します。 –