I am getting a segmentation fault and i know case 3 is wrong i just need help getting started on it i need to keep track what the use orders then once they are done print it to the console thx for the help.
Code:
#include<stdio.h>#include<stdlib.h>
#define SIZE 25
typedef struct
{
char name[SIZE];
int id;
int quan;
float price;
}BEER;
BEER var[10];
int main ()
{
FILE *f;
f=fopen("beer.dat", "r");
int numBeer,tempQuan,tempId,searchId,choice;
int i,j,n,p,q,h;
int searchCount=0;
float tempPrice;
char tempName[SIZE];
fscanf(f,"%d",&numBeer);
BEER *beer;
beer=(BEER*)calloc(numBeer,sizeof(BEER));
for(i=0;i<numBeer;i++)
{
fscanf(f,"%s%d%d%f",beer[i].name,&beer[i].id,&beer[i].quan,&beer[i].price);
}
printf("Would you like to:\n1.Search a beer by id number\n2.View the entire inventory\n3.Place an Order\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Please enter a beer id # you wish to search: ");
scanf("%d",&searchId);
for(j=0;j<numBeer;j++)
{
if(beer[j].id==searchId)
{
printf("\n%s\nName: %d\nID: $%d\nQuantity: %.2fPrice\n\n",beer[j].name,beer[j].id,beer[j].quan,beer[j].price);
searchCount++;
}
}
if(searchCount==0)
{
printf("Invalid Beer id\n");
}
break;
case 2:
for(n=0;n<numBeer;n++)
{
for(p=0;p<numBeer;p++)
{
if(beer[n].price>beer[p].price)
{
strcpy(tempName,beer[n].name);
strcpy(beer[n].name,beer[p].name);
strcpy(beer[p].name,tempName);
tempId=beer[n].id;
beer[n].id=beer[p].id;
beer[p].id=tempId;
tempQuan=beer[n].quan;
beer[n].quan=beer[p].quan;
beer[p].quan=tempQuan;
tempPrice=beer[n].price;
beer[n].price=beer[p].price;
beer[p].price=tempPrice;
}
}
}
for(q=0;q<numBeer;q++)
{
printf("\n%s\nName: %d\nId: $%d\nQuantity: %.2fPrice\n\n",beer[q].name,beer[q].id,beer[q].quan,beer[q].price);
}
break;
case 3:
for(i=0;i<10;i++){
printf("Please enter a beer id # you wish to buy, if you are finished enter the # 1: ");
scanf("%d",&searchId);
if(searchId==1)break;
for(j=0;j<numBeer;j++)
{
if(beer[j].id==searchId)
{
//var[10].name[25] = beer[j].name;
var[10].id = beer[j].id;
var[10].quan = beer[j].quan;
var[10].price = beer[j].price;
searchCount++;
}
}
} for(h=0;h<10;h++){
printf("\n%s\nName: %d\nID: $%d\nQuantity: %.2fPrice\n\n",var[10].name,var[10].id,var[10].quan,var[10].price);
}
if(searchCount==0)
{
printf("Invalid Beer id\n");
}
break;
default:
printf("Please Enter Valid Input (1,2,3)\n");
}
fclose(f);
return 0;
}