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]