[英]The scanf is not reading the values
Hey this is the entire code. 嘿,这是完整的代码。 I ma beginner in C, I was trying to make a code which has a structure mall and takes into input the name, number of items shopped , the name of each item and the cost of each item.
我是C语言的初学者,我试图制作一个具有结构商城的代码,并输入名称,购物商品数量,每个商品的名称和每个商品的成本。 * For small programs like this i fix the max size of the structure object But the program cant take the input in the manner desired.
*对于像这样的小型程序,我固定了结构对象的最大大小,但是该程序无法以所需的方式获取输入。
#include<stdio.h>
struct mall
{
char name[50];
char obj[10][30];
float price[10];
int numb;
}b[50];
void main()
{
int m; // number of persons who shopped at the mall
scanf("%d",&m);
for(int i=0;i<m;i++)
{
num=0;
scanf("%s",&b[i].name);
scanf("%d",&b[i].numb);
printf("%s\n%d",b[i].name,b[i].numb);
for(int j=0;j<num;j++)
{
scanf("%s",&b[i].obj);
scanf("%f",&b[i].price);
}
}
}
For the input : 对于输入:
1
Ram 2 bread 50.00 jam 25.00
I m getting the output as 我得到的输出为
2500 2500
Your code has many small mistakes: 您的代码有许多小错误:
scanf("%s", &b[i].name);
does not need &
&
num=0;
is not necessary; j < b[i].numb
as its condition j < b[i].numb
作为其条件 j
. j
。 It needs to add [j]
to both obj
and price
. obj
和price
都添加[j]
。 Once you fix these problems, your code runs as expected as long as the input is correct ( demo ). 解决这些问题后,只要输入正确( 演示 ),代码就会按预期运行。
However, this is not enough to make your code robust: you need to add error checking to ensure that invalid input does not cause undefined behavior: 但是,这还不足以使代码更健壮:您需要添加错误检查以确保无效输入不会导致未定义的行为:
scanf
to avoid buffer overflows (eg %49s
to read name[50]
), scanf
为字符串格式说明符添加限制,以避免缓冲区溢出(例如, %49s
读取name[50]
), m
is above 50, m
大于50,则为外循环添加一个限制, b[i].numb
is above 10, b[i].numb
大于10, b[i].numb
嵌套循环添加一个限制, scanf
. scanf
添加返回值检查。 num
is undeclared. num
未声明。 The program does not compile to executable. &b[i].name
to scanf(3)
, as the %s
format requires a char *
and the &b[i].name
is not that type (it is, indeed, char *[50]
, while compatible, it is not the same type, while b[i].name
is) &b[i].name
传递给scanf(3)
,因为%s
格式需要使用char *
,而&b[i].name
并非该类型(实际上是char *[50]
,而兼容,它不是同一类型,而b[i].name
是) num
by b[i].numb
in the inner for
loop. for
循环中通过b[i].numb
更改num
。 This is the proper number of items. b[i].obj[j]
for the object name as the reference to scanf(3)
b[i].obj[j]
中使用对象名称作为对scanf(3)
的引用 &b[i].price[j]
for the j
-esim price
. &b[i].price[j]
中使用j
-esim price
。 You have forgotten it here and in the point above. printf(3)
at all. printf(3)
。 A valid (but not completely correct, as array sizes are not checked) could be: 有效(但不完全正确,因为未检查数组大小)可能是:
#include<stdio.h>
struct mall {
char name[50];
char obj[10][30];
float price[10];
int numb;
}b[50];
int main()
{
int m; // number of persons who shopped at the mall
scanf("%d",&m);
for(int i=0; i<m; i++) {
scanf("%s",b[i].name);
scanf("%d",&b[i].numb);
printf("* name = %s\n"
" numb = %d\n",b[i].name,b[i].numb);
for(int j=0; j < b[i].numb; j++) {
scanf("%s",b[i].obj[j]);
scanf("%f",&b[i].price[j]);
printf(" * obj = %s\n", b[i].obj[j]);
printf(" price = %.2f\n", b[i].price[j]);
}
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.