c:带有strut和array

时间:2016-12-06 17:12:34

标签: c arrays string struct

我正在开发一个程序,当用户输入月份和日期编号时,将打印出年初至今的月份和天数。预期的输入和输出低于。

Enter month and day number?  december 30
December (DEC) 31 means 364 days YTD.

我的程序编译,我正在使用microsoft visual studio。使用与预期输入相同的输入我尝试运行程序;但是,输入月份和日期后,程序崩溃了。

编译器的警告:

'scanf_s' : format string '%s' requires an argument of type 'unsigned int', but variadic argument 2 has type 'int *'    

'scanf_s' : not enough arguments passed for format string   

当我使用调试器时,程序在输入月和日之后给出这些消息,然后继续逐步执行代码。它似乎没有足够的内存继续?如果这是问题,解决它的最合理的方法是什么?

Exception thrown at 0x0FCA0BA9 (ucrtbased.dll) in lab14program345words.exe: 0xC0000005: Access violation writing location 0x003C0000.

以下是我的代码:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int finddata(int month, int days);

struct month
{
    char name[10];
    char abbrev[4];
    int days;
    int monumb;
};



int main()
{
    struct month months[12] =
    {
        { "January", "jan", 31, 1 },
        { "February", "feb", 28, 2 },
        { "March", "mar", 31, 3 },
        { "April", "apr", 30, 4 },
        { "May", "may", 31, 5 },
        { "June", "jun", 30, 6 },
        { "July", "jul", 31, 7 },
        { "August", "aug", 31, 8 },
        { "September", "sep", 30, 9 },
        { "October", "oct", 31, 10 },
        { "November", "nov", 30, 11 },
        { "December", "dec", 31, 12 }
    };
    struct month userdata;
    int i = 0;
    int total;

    do
    {
        printf("Enter month and day number:");

        scanf_s("%s %d", userdata.name, &userdata.days); //line 42, where the warnings occur

        toupper(userdata.name[0]);

        if (strcmp(userdata.name, months[i].name) == 0) // should compare user's entered month to months in month montths array
        {
            puts(months[i].name);
            puts(months[i].abbrev);
        }
        else
        {
            i++;
        }


        total = finddata(months[i].monumb, userdata.days); // should pass the month number and days the user entered. 
        printf("%d days YTD.\n", total);

    } while ((userdata.monumb > 0) || (userdata.monumb < 13));


    return 0;
}



int finddata(int months, int days)
{
    int total = 0;

    if (months == 1)
    {
        total = days;
    }
    else if (months == 2)
    {
        total = days + 31;
    }
    else if (months == 3)
    {
        total = 31 + 28 + days;
    }
    else if (months == 4)
    {
        total = 31 + 28 + 31 + days;
    }
    else if (months == 5)
    {
        total = 31 + 28 + 31 + 30 + days;
    }
    else if (months == 6)
    {
        total = 31 + 28 + 31 + 30 + 31 + days;
    }
    else if (months == 7)
    {
        total = (31 * 3) + 28 + (30 * 2) + days;
    }
    else if (months == 8)
    {
        total = (31 * 4) + 28 + (30 * 2) + days;
    }
    else if (months == 9)
    {
        total = (31 * 5) + 28 + (30 * 2) + days;
    }
    else if (months == 10)
    {
        total = (31 * 5) + 28 + (30 * 3) + days;
    }
    else if (months == 11)
    {
        total = (31 * 6) + 28 + (30 * 3) + days;
    }
    else if (months == 12)
    {
        total = (31 * 6) + 28 + (30 * 4) + days;
    }
    else
    {
        //blank
    }



    return total;
}

1 个答案:

答案 0 :(得分:1)

scanf_s("%s %d", userdata.name, &userdata.days);

如果您阅读scanf_s,则需要传递另一个参数,其中%s%c代表长度。

所以在你的情况下,你需要做的是 -

scanf_s("%s %d", userdata.name, (rsize_t)sizeof userdata.name, &userdata.days);  

您收到警告,因为&userdata.days被视为长度参数且类型不匹配。 如果是%c,您需要通过1来读取一个字符。