简体   繁体   English

scanf没有读取值

[英]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; 没有必要; remove it 去掉它
  • Nested loop condition should use j < b[i].numb as its condition 嵌套循环条件应使用j < b[i].numb作为其条件
  • Nested loop is not using j . 嵌套循环未使用j It needs to add [j] to both obj and price . 它需要在objprice都添加[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: 但是,这还不足以使代码更健壮:您需要添加错误检查以确保无效输入不会导致未定义的行为:

  • Add limits to string format specifiers in scanf to avoid buffer overflows (eg %49s to read name[50] ), scanf为字符串格式说明符添加限制,以避免缓冲区溢出(例如, %49s读取name[50] ),
  • Add a limit to the outer loop in case m is above 50, 如果m大于50,则为外循环添加一个限制,
  • Add a limit to the nested loop in case b[i].numb is above 10, 如果b[i].numb大于10, b[i].numb嵌套循环添加一个限制,
  • Add checks of return values for scanf . scanf添加返回值检查。
  • num is undeclared. num未声明。 The program does not compile to executable. 该程序无法编译为可执行文件。 See below. 见下文。
  • Don't pass &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是)
  • change num by b[i].numb in the inner for loop. 在内部for循环中通过b[i].numb更改num This is the proper number of items. 这是正确的项目数。 You have to check for array sizes also, as you have hardwired them in the code. 您还必须检查数组大小,因为已在代码中对它们进行了硬接线。 If you don't you can overrun 如果不这样做,您可以超支
  • Use inside the inner loop b[i].obj[j] for the object name as the reference to scanf(3) 在内部循环b[i].obj[j]中使用对象名称作为对scanf(3)的引用
  • Use inside the inner loop &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. 您在这里和上面已经忘记了它。
  • you have to add code to print the individual objects (the inner loop has no 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM