Flushing stdin is a bad practice. It results in Undefined behavior and must be avoided. Fflush should be used on input streams only when you are working on an underlying seekable file and stdin is not.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct mobile
{
char brand[27];
int quantity;
int price;
};
int count=0;
void storing(struct mobile *ptr)
{
fflush(stdin);
printf("\n\nEnter the details of %d mobile \n",count+1);
printf("Brand name :");
gets(ptr[count]->brand) ;
printf("Quantity :");
scanf("%d", &ptr[count]->quantity) ;
printf("Price : ");
scanf("%d", &ptr[count]->price) ;
count++;
}
void search(struct mobile a[],int n)
{
fflush(stdin);
int key;
char find[27];
printf("enter the mobile brand\n");
gets(find);
for(int i=0; i<n; i++)
{
key = strcmp( a[i].brand , find ) ;
if( key == 0 )
{
printf("Mobile brand : %s \n", a[i].brand) ;
printf("Quantity : %d \n", a[i].quantity) ;
printf("Price : %d \n", a[i].price);
}
}
}
int main()
{
struct mobile m[3];
while(1)
{
printf("enter your choice \n 1.storting \n 2.searching\n 3.exit\n");
int ch;
scanf("%d",&ch);
switch(ch)
{
case 1:
storing(m);
break;
case 2:
search(m,3);
break;
case 3:
exit(0);
}
}
}
what is wrong with this ?