-4

Im getting the format %s expects argument of type *char,but argument is of type int, error here, can anyone help me fix this problem please ? It s a simple code but i have not leanr pointers yet, i do not know how to fix this issue.

int main()
{
  
  int items;
  int i;

     //main menu of the program
    printf("*********WELCOME TO ABC FASHION STORE***********\n");
    printf("\t\t\t1.Make a new sale\n\t\t\t2.Exit from POS system\n");
    printf("-----------------------------------------------------");

   printf("How many different types of items in your sales:");
   scanf("%d",&items);

   char code[items];
   int qty[items];
   for(i=0;i<items;i++)
   {
    printf("Enter the item code:");
    scanf("%s", code[i]);
    printf("Enter the quantity:");
    scanf("%d",&qty[i]);
   }

   //displaying the details of consumer
  printf("\nITEM CODE \t\t QUANTITY \t\t UNIT PRICE \t\t TOTAL\n");
  for(i=0;i<items;i++)
  {
   printf("%s\t\t%d\t\t", code[i],qty[i]);
  }
Zinzin
  • 5
  • 6
  • You're trying to read a character. It should go: ```scanf("%c", &code[i]);``` and ```printf("%c\t\t%d\t\t", code[i],qty[i]);``` – wkkuna Mar 09 '21 at 13:29
  • im trying to read a string like "WB100" – Zinzin Mar 09 '21 at 13:32
  • Then you should do ```scanf("%s", code);``` since you're trying to read a whole word and not single character. As well as similarily printing whole word with printf. – wkkuna Mar 09 '21 at 13:33
  • What is your error here ? Compilation error or error at runtime. Could you also add the text of the error to your question? – XouDo Mar 09 '21 at 13:35
  • _"can anyone help me fix this problem please"_. The first answer posted was the real answer to your question, not the one you accepted. i.e. i.e. the answer that explains what you are seeing in your compile warnings provides information you can use to fix the problem yourself. The answer you accepted _gives_ you a solution that requires no thought on your part. If you are serious about coding, then avoid taking the path of least resistance and accept advice that will help you learn. – ryyker Mar 09 '21 at 14:14

3 Answers3

0

For starters you have:

   char code[items];
   ...
   scanf("%s", code[i]);

so code is an array of characters, and code[i] represents a single character -- not a string! What you are probably after is:

   #define MAX_LEN 50
   char code[items][MAX_LEN];
 ...
   scanf("%s", code[i]);

This makes code an array of strings, each up to MAX_LEN long.

Now, the above has a large security issue with it -- you don't specify the maximum size of your array, and a user can type in a really long string, overrunning the end of the array. A smart and malicious user can use this to override other parts of memory in your program... So to get around that you can limit the size of string that scanf accepts: scanf("%50s", code[i]);, which limits the input to 50 characters. But if you change MAX_LEN, and forget to change the width specifier, you get into trouble. The proper solution is described here;

   #define MAX_LEN 50
   #define STR(x) _STR(x)
   #define _STR(x) #x
   char code[items][MAX_LEN];
 ...
   scanf("%" STR(MAX_LEN) "s", code[i]);

For the printf, your issue is the same one -- code[i] is a character, not a string pointer, so it doesn't align with the %s format specifier. If you switch code to be an array of strings, it will work.

HardcoreHenry
  • 5,909
  • 2
  • 19
  • 44
-1

Replace this char code[items]; with char code[items][100];

If you want code as a string.

starboy_jb
  • 899
  • 1
  • 6
  • 13
  • @Zinzin It is also important to understand why it failed and not just *I changed the code and now it works* : a `char` is one character, a `char[]` (char array) is a string, so a `char[][]`is an array of string. There's a lot more to that, off course (memory allocation, pointers etc.), but that's a start. – XouDo Mar 09 '21 at 13:44
-1

Its unsafe to use scanf the way you do as it makes your program vulnerable to buffer overflow.

Your better off using e.g. fgets or getline, read input as string, and then verify the content and convert it to whatever data type needed afterwards.

There is plenty examples on stackoverflow on how to do this. One example here: https://stackoverflow.com/a/24177745/10956302

Linuxdevel
  • 26
  • 4
  • Please consider improving your answer to provide the code that fixes the problem using `fgets()` or `getline()`. – Null Mar 09 '21 at 14:05
  • A quick search on stackoverflow gives the answer. No need to duplicate answers all over. I updated my original post with a link. – Linuxdevel Mar 10 '21 at 09:02