1

I am currently working on an assignment and was curious what this warning is when compiling and how to remedy it. It will build but when I debug it will get an error screen. Below is the warning that comes up.

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");
}
too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Chris
  • 65
  • 1
  • 6
  • 1
    `scanf_s` is safe scanf. Requires buffer size. `scanf_s("%s", item[i].name, 20); ` – 001 Nov 01 '15 at 13:50

2 Answers2

6

You should provide a size of the buffer. For example, if you reading only one char, it should be like this:

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

Please read the ref!

Also, structs are nice to be placed before main(). I always have my example in mind, on the basic usage of structs.

The prototype of main should be int main(void) in your case. Check this: int main() vs void main() in C


In your code, change this:

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

to this:

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

because of this:

struct details {
  char name[20];
  ..  

Do the same for the rest..

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • A downvote for what please? If there is something wrong, tell me to update for the future users... – gsamaras Nov 01 '15 at 13:53
  • I appreciate the help and corrected the placement of the structure as well – Chris Nov 01 '15 at 13:56
  • @Chris I updated with an example for structs. Glad it helped, +1 for taking time to edit your post. – gsamaras Nov 01 '15 at 13:57
  • 1
    I downvoted. I felt the 'RTFM' comment was a bit unprofessional, you could have quoted the relevant section of the docs at that link. Your comment _Also, structs should be before main(). I always have my example in mind._ should have some sort of explanation. Are local structs allowed per the standard or not? A link(and a quote would be nice). Is there a stylistic guideline that says you shouldn't etc?(A link to such info would be appropriate). As far as I know local structs are supported with C99, but not C89. – Michael Petch Nov 01 '15 at 14:58
  • @MichaelPetch thanks for commenting. I agree on RTFM, I will remove it. As for the structs, I will update too. If you have any other problem with that please let me know. – gsamaras Nov 01 '15 at 18:06
2
 scanf_s("%s", item[i].name);    

scanf_s require's size as 3rd argument with specifiers %s, %c and %[ .

You need to write like this -

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

Similar , for taking input a single character pass 1 as size .

ameyCU
  • 16,489
  • 2
  • 26
  • 41