I've been trying to figure the problem but after 3 hours I quit, anyone have any suggestion how this problem might occur. Any help would be appreciated! Currently using codeblock 20.23. img of the empty blank
#include <stdio.h>
#include <stdbool.h>
int main()
{
int dd, mm, yy, temp = 0;
bool isLeapYear = temp;
printf("Enter the year: ");
scanf("%d", &yy);
if(yy < 0)
{
printf("Year number cannot be smaller than 0!");
return 0;
} else
{
if(yy%4==0 && yy@0==0 || yy%4==0)
temp = 1;
}
printf("Enter the month: ");
scanf("%d", &mm);
if(mm > 12 || mm < 0)
{
printf("Month number cannot be smaller than 0 or bigger than 12!");
return 0;
}
printf("Enter the date: ");
scanf("%d", &dd);
if(dd > 30 || dd < 0)
{
printf("Date number cannot be smaller than 0 or bigger than 30!");
return 0;
} else
{
if(dd == 29 && mm == 2 && temp == 0)
printf("Since given year is not a leap year therefore your input date is invalid.");
return 0;
}
printf("dd/mm/yy of your given number: d/d/d", dd, mm, yy);
return 0;
}
CodePudding user response:
Did you forget to block correctly ?
if(dd == 29 && mm == 2 && temp == 0){
printf("Since given year is not a leap year therefore your input date is invalid.");
return 0;
}
instead of
if(dd == 29 && mm == 2 && temp == 0)
printf("Since given year is not a leap year therefore your input date is invalid.");
return 0;
CodePudding user response:
If we indent the code properly we see that the last if-statement looks like this:
if (dd > 30 || dd < 0) {
printf("Date number cannot be smaller than 0 or bigger than 30!");
return 0;
} else {
if (dd == 29 && mm == 2 && temp == 0)
printf("Since given year is not a leap year therefore your input date is invalid.");
return 0;
}
This means that the return statement in the else-clause is always executed for a valid day and hence the final print statement is never executed. You should also print to the standard error stream (with a newline) and return an error code (instead of zero which means success):
fprintf(stderr, "...\n");
return 1;