I am trying to write a program to check validity of month entered by user. Here are my codes:
int monthcheck(int m) {
int month = 0;
while (m <= 0 || m > 12) {
printf("Month must be between 1 and 12, re-enter month:");
scanf("%d", &m);
}
if (m == 2) {
month = 1; // February
}
if (m==1||m==3||m==5||m==7||m==8||m==10||m==12) month=2;
return month;
}
int main()
{
int m;
printf("Enter month");
scanf("%d",&m);
if (monthcheck(m)==0) printf("The month is valid");
else if (monthcheck(m)==1) printf("The month is february");
}
And here is my output:
Enter month-2
Month must be between 1 and 12, re-enter month:13
Month must be between 1 and 12, re-enter month:2
Month must be between 1 and 12, re-enter month:2
The month is february
My question is: why does the program still ask me to enter another input (at line 4) although it is not meet the conditions of while loop? Help is appreciated. Many thanks!