c++ - C4473 结构分配警告

标签 c++ structure scanf

我目前正在做一项作业,很好奇编译时这个警告是什么以及如何补救。它会构建,但当我调试时会出现错误屏幕。下面是出现的警告。

1>c:\users\cesteves\documents\c programming\inventory\inventory\inventory.cpp(48): warning C4473: 'scanf_s' : not enough arguments passed for format string

note: placeholders and their parameters expect 2 variadic arguments, but 1 were provided

note: the missing variadic argument 2 is required by format string '%s' note: this argument is used as a buffer size

#include "stdafx.h"
#include <stdio.h>

void main()
{
    struct date {
        int day;
        int month;
        int year;
    };

    struct details {
        char name[20];
        int price;
        int code;
        int qty;
        struct date mfg;
    };

    struct details item[50];
    int n, i;

    printf("Enter number of items:");
    scanf_s("%d", &n);

    for (i = 0; i < n; i++) {
        printf("Item name: \n");
        scanf_s("%s", item[i].name);
        printf("Item code: \n");
        scanf_s("%d", &item[i].code);
        printf("Quantity: \n");
        scanf_s("%d", &item[i].qty);
        printf("price: \n");
        scanf_s("%d", &item[i].price);
        printf("Manufacturing date(dd-mm-yyyy): \n");
        scanf_s("%d-%d-%d", &item[i].mfg.day, &item[i].mfg.month, &item[i].mfg.year);    
    }

    printf("             *****  INVENTORY ***** \n");
    printf("----------------------------------------------------------------- - \n");
    printf("S.N.|    NAME           |   CODE   |  QUANTITY |  PRICE| MFG.DATE \n");
    printf("----------------------------------------------------------------- - \n");

    for (i = 0; i < n; i++)
        printf("%d     %-15s        %-d          %-5d     %-5d%d / %d / %d \n", i + 1, item[i].name, item[i].code, item[i].qty,item[i].price, item[i].mfg.day, item[i].mfg.month,item[i].mfg.year);
    printf("----------------------------------------------------------------- - \n");
}

最佳答案

您应该提供缓冲区的大小。例如,如果你只读取一个字符,它应该是这样的:

char c;
scanf_s("%c", &c, 1);

请阅读ref !

此外,structs 很适合放在 main() 之前。我总是有我的 example牢记结构的基本用法。

在您的例子中,main 的原型(prototype)应该是 int main(void)。检查这个:int main() vs void main() in C


在你的代码中,改变这个:

scanf_s("%s", item[i].name);

为此:

scanf_s("%s", item[i].name, 20);

因为这个:

struct details {
  char name[20];
  ..  

对其余的做同样的..

关于c++ - C4473 结构分配警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33462802/

相关文章:

c# - 从 C# 调用 C++ 函数,参数是接口(interface)指针

c++ - eclipse CDT : how to enable project debugging

c - 访问双指针

c - 错误 : request for member in something not a structure or union

c - 为什么 scanf 被执行了 11 次?

c++ - 使用 sscanf 读取格式化字符串数据

c++ - 我在 Visual Studio C++ 中遇到这些错误 : 'NuovoUtente' : undeclared identifier and 'CercareUtente' : undeclared identifier

c++ - 如何声明一个静态变量但不定义它

c - 数组 C 中的结构初始化行为

c - 扫描两个整数(如 scanf ("%d\n %d")时使用\n 有什么用?