简体   繁体   English

为什么 printf 只打印一个空白

[英]Why does the printf prints nothing but an empty blank

I've been trying to figure the problem but after 3 hours I quit, anyone have any suggestion how this problem might occur.我一直在尝试解决问题,但 3 小时后我退出了,任何人都对这个问题可能如何发生有任何建议。 Any help would be appreciated.任何帮助,将不胜感激。 Currently using codeblock 20.23.当前使用代码块 20.23。 img of the empty blank空白的img

#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%400==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: %02d/%02d/%04d", dd, mm, yy);
    return 0;

}

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;

If we indent the code properly we see that the last if-statement looks like this:如果我们正确缩进代码,我们会看到最后一个 if 语句如下所示:

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.这意味着 else 子句中的 return 语句总是在有效的一天执行,因此永远不会执行最终的 print 语句。 You should also print to the standard error stream (with a newline) and return an error code (instead of zero which means success):您还应该打印到标准错误 stream(带换行符)并返回错误代码(而不是零,这意味着成功):

fprintf(stderr, "...\n");
return 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM