-1

When I am using gets function to scan item name in the first loop it runs properly but in the 2nd loop, it skips to Quantity quant without scanning item name. I also tried using %s previously then it is working fine.

#include <stdio.h>
struct Dmart
{
    char item[10];
    int quant, price;
};
int main()
{
    int i;
    struct Dmart cust1[3], cust2[3];

    printf("\nFor Customer 1\n");
    for (i = 0; i < 3; i++)
    {
        printf("Item: ");
        gets(cust1[i].item);
        printf("Quantity: ");
        scanf("%d", &cust1[i].quant);
        printf("Price: ");
        scanf("%d", &cust1[i].price);
        printf("\n");
    }
    printf("\nFor Customer 2\n");
    for (i = 0; i < 3; i++)
    {
        printf("Item: ");
        gets(cust2[i].item);
        printf("Quantity: ");
        scanf("%d", &cust2[i].quant);
        printf("Price: ");
        scanf("%d", &cust2[i].price);
        printf("\n");
    }

    printf("\nBill of Customer 1\n");
    printf("Item\t\tQuantity\tPrice\n");
    for (i = 0; i < 3; i++)
    {
        printf("%s\t\t%d\t\t%d\n", cust1[i].item, cust1[i].quant, cust1[i].price);
    }

    printf("\nBill of Customer 1\n");
    printf("Item\t\tQuantity\tPrice\n");
    for (i = 0; i < 3; i++)
    {
        printf("%s\t\t%d\t\t%d\n", cust2[i].item, cust2[i].quant, cust2[i].price);
    }
    return 0;
}

[VS Code Terminal Output][1]

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 2
    Side note: You may want to read this: [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/q/1694036/12149471) – Andreas Wenzel Apr 15 '22 at 10:53
  • 4
    Use exclusively `fgets()` for user input. Forget `scanf()` exists. – pmg Apr 15 '22 at 10:54
  • 1
    Mixing `scanf` and `fgets` will lead to problems unless you're very careful. As mentioned by @pmg, use only `fgets` to read full lines (well, hopefully), and then you can use e.g. `sscanf` to parse the lines. – Some programmer dude Apr 15 '22 at 10:55
  • You may want to read this for alternatives to using the function `scanf`: [A beginners' guide away from scanf()](http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html) – Andreas Wenzel Apr 15 '22 at 11:21

1 Answers1

1

The function gets is unsafe and is not supported by the C Standard. So do not use it.

The problem of the code is that after this call of scanf

scanf("%d", &cust2[i].price);

the new line character '\n' that corresponds to the pressed key Enter still is in the input buffer. So the next call of gets reads an empty string encountering at once the new line character.

Instead you could either always use the function fgets and then convert the entered string to an integer for integer data members of the structure or you can use scanf instead of gets like for example

for (i = 0; i < 3; i++)
{
    printf("Item: ");
    scanf( " %9[^\n]", cust1[i].item);
    printf("Quantity: ");
    scanf("%d", &cust1[i].quant);
    printf("Price: ");
    scanf("%d", &cust1[i].price);
    printf("\n");
}

Pay attention to the leading space in the format string

    scanf( " %9[^\n]", cust1[i].item);
           ^^^^

It allows to skip white spaces in the input buffer.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335