Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help finding the segmentation fault and getting case 3 to work the user t

ID: 3659492 • Letter: I

Question


I need help finding the segmentation fault and getting case 3 to work the user types what they want and when they are done the console shows what they got. 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: 1.Search a beer by id number 2.View the entire inventory 3.Place an Order ");

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(" %s Name: %d ID: $%d Quantity: %.2fPrice ",beer[j].name,beer[j].id,beer[j].quan,beer[j].price);
searchCount++;
}
}

if(searchCount==0)
{
printf("Invalid Beer id ");
}
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(" %s Name: %d Id: $%d Quantity: %.2fPrice ",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(" %s Name: %d ID: $%d Quantity: %.2fPrice ",var[10].name,var[10].id,var[10].quan,var[10].price);
}
if(searchCount==0)
{
printf("Invalid Beer id ");
}
break;



default:
printf("Please Enter Valid Input (1,2,3) ");
}


fclose(f);


return 0;

}[/code]

Explanation / Answer

Can you tell me what case 3 is suppose to do? And what is an requirement for an order? Does it have to have 10 beer to work or can orders be less? what is global variable val[] used for? //I'll post code in comment afterwards

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote