gpt4 book ai didi

c:带有strut和array的字符串输入

转载 作者:行者123 更新时间:2023-11-30 15:02:32 26 4
gpt4 key购买 nike

我正在开发一个程序,当用户输入月份和日期数字时,会打印出年初至今的月份和天数。预期的输入和输出如下。

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;
}

最佳答案

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 来读取一个字符。

关于c:带有strut和array的字符串输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41001090/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com