简体   繁体   中英

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. * 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

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
  • Nested loop is not using j . It needs to add [j] to both obj and price .

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] ),
  • Add a limit to the outer loop in case m is above 50,
  • Add a limit to the nested loop in case b[i].numb is above 10,
  • Add checks of return values for scanf .
  • num is undeclared. 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)
  • change num by b[i].numb in the inner for loop. 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)
  • Use inside the inner loop &b[i].price[j] for the 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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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