【问题标题】:Runtime error while using exit_failure in a C program在 C 程序中使用 exit_failure 时出现运行时错误
【发布时间】:2019-06-24 09:01:35
【问题描述】:

所以我创建了一个程序,你必须输入一个日期 dd/mm/yyyy 然后你得到 date.day + 1 ,但我不想验证输入的日期是否有效,即 date.day 之间1 和 31,月份 1 和 12 之间,年份 1 和 9999 之间,并且输入的日期小于该月的天数,如果其中一个失败返回退出失败

     `// Program to determine tomorrow's date

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

    struct date
    {
     int day;
     int month;
     int year;
    };

    int main(void)
    {
        struct date today, nextDay;
        bool isValidDate(struct date VarName);
        struct date dateUpdate(struct date today);


        printf("Enter today's date (dd mm yyyy): ");
        scanf("%i %i %i", &today.day, &today.month, &today.year);

        if (isValidDate(today) == false)
        {
            printf("Invalid date format. \n");
            exit(EXIT_FAILURE);
        }

        nextDay = dateUpdate(today);

        printf("Tomorrow's date is: %i/%i/%i \n", nextDay.day, 
        nextDay.month,             
        nextDay.year % 100);

        return 0;
    }
    // Function to update today's date to tomorrow's date
    struct date dateUpdate(struct date today)
    {
        struct date tomorrow;
        int numberOfDays(struct date VarName);

        if (today.day != numberOfDays(today)) // End of day
        {
            tomorrow.day = today.day + 1;
            tomorrow.month = today.month;
            tomorrow.year = today.year;
        }
        else if (today.month == 12)         // End of year
        {
            tomorrow.day = 1;
            tomorrow.month = 1;
            tomorrow.year = today.year + 1;
        }
        else                                // End of month
        {
            tomorrow.day = 1;
            tomorrow.month = today.month + 1;
            tomorrow.year = today.year;
        }
        return tomorrow;
    }
    // Function to find the numbers of days in a month
    int numberOfDays(struct date VarName)
    {
        const int daysPerMonth[13] = {0, 31, 28, 31, 30, 31, 30,
                                  31, 31, 30, 31, 30, 31
                                 };
        int days;
        bool isLeapYear(struct date VarName);
        bool isValidDate(struct date VarName);

        if (isLeapYear(VarName) == true && VarName.month == 2)
        {
            days = 29;
        }
        else
        {
            days = daysPerMonth[VarName.month];
        }

        return days;

    }
    // Function to determine if a year is a leap year
    bool isLeapYear(struct date VarName)
    {
        bool leapYearFlag;
        if ((VarName.year % 4 == 0 && VarName.year % 100 != 0) ||
            VarName.year % 400 == 0)
        {
            leapYearFlag = true; // It's a leap year
        }
        else
        {
            leapYearFlag = false; // Not a leap year
        }
        return leapYearFlag;
    }
    bool isValidDate(struct date VarName)
    {
        if ( (VarName.day < 1 && VarName.day > 31) || (VarName.day >         
         numberOfDays(VarName)) ) // Day format verifier
        {
            return false;
        }
        else if (VarName.month < 1 && VarName.month > 12) 
                                                // Month format verifier      

        {
            return false;
        }
        else if (VarName.year < 1 && VarName.year > 9999)
                                                 // Year format verifier

        {
            return false;
        }
        else
        {
            return true;
        }
       }`

测试A

输入今天的日期(dd mm yyyy):31 13 2018

结果

prog_8.4.c:78:16:运行时错误:索引 13 超出类型“const int [13]”的范围 日期格式无效。

测试 B

输入今天的日期(dd mm yyyy):31 2 2018

结果

日期格式无效。

测试 C

输入今天的日期(dd mm yyyy):31 12 2018

结果

明天的日期是:1/1/19

如果您注意到,如果有一个大于 13 的月份,我会收到一个我不想收到的运行时错误,我不想收到与测试 B 相同的消息,其中输入的日期大于那个月的几天,如果我在 DateUpdate 函数之前有我的格式验证器,为什么编译器会运行 DateUpdate 函数,因为我认为 A 错误与此函数有关,但如果我的错误验证器正常工作,程序将不会运行这个函数,因为它会在到达那里之前终止,至少我是这么认为的,你能帮我吗?

【问题讨论】:

    标签: c standard-library


    【解决方案1】:

    我猜问题出在这里:

    if ( (VarName.day < 1 && VarName.day > 31) || (VarName.day >         
         numberOfDays(VarName)) ) // Day format verifier
    

    如下所示,const in daysPerMonth[13] 中没有 13 的索引。您只有从 0 到 12 的索引,而在函数 bool isValidDate() 中,您放置了 Varname 而不检查索引是否在 1 到 12 之间。

    在将Varname 放入numberOfDays() 之前检查索引将解决您的问题。

    【讨论】:

    • 你是对的,我在没有验证 VarName.month 是否在 1 到 12 之间的情况下调用 numberOfDays() 函数,为了修复它我只是重新安排了 isValidDate() 函数所以第一个 if 语句将用于验证输入的月份是否有效
    猜你喜欢
    • 1970-01-01
    • 2013-11-30
    • 2013-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多