【发布时间】:2021-03-09 13:24:51
【问题描述】:
我得到格式 %s 需要 *char 类型的参数,但参数是 int 类型,这里出错,谁能帮我解决这个问题?这是一个简单的代码,但我还没有更精简的指针,我不知道如何解决这个问题。
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]);
}
【问题讨论】:
-
您正在尝试读取一个字符。它应该去:
scanf("%c", &code[i]);和printf("%c\t\t%d\t\t", code[i],qty[i]); -
我正在尝试读取像“WB100”这样的字符串
-
那么你应该做
scanf("%s", code);,因为你试图阅读整个单词而不是单个字符。以及类似地用 printf 打印整个单词。 -
你的错误是什么?编译错误或运行时出错。您能否也将错误文本添加到您的问题中?
-
“谁能帮我解决这个问题”。发布的第一个答案是您问题的真实答案,而不是您接受的答案。即解释您在编译警告中看到的内容的答案提供了您可以用来自己解决问题的信息。您接受的答案给了您一个不需要您考虑的解决方案。如果您对编码很认真,请避免走阻力最小的道路,并接受有助于您学习的建议。