I get this when once I run the program listed below: ~$ gcc week5.c j ~ $ ./a.ex
ID: 3530565 • Letter: I
Question
I get this when once I run the program listed below:
~$ gcc week5.c j
~ $ ./a.exe Enter the filename: Cans Segmentation fault (core dumped)
Please advise on what needs to be fixed. I run my programs through cygwin and save them in C.
# include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include<string.h>
typedef
struct INVENTORY
{
int rn;
char name[1000];
int q; float cost;
struct INVENTORY* next;
}
inv; inv* I;
int size=0;
void printinv()
{
inv* temp = I;
printf("Inventory: ");
while(temp!=NULL)
{
printf("%d %s %d %f ",temp->rn,temp->name,temp->q,temp->cost);
temp = temp->next;
}
}
bool addnew(char name[], int q, float cost)
{
inv* temp = I;
while(temp->next != NULL)
{
temp = temp->next;
}
inv* node = (inv*)malloc(sizeof(inv));
node->rn = temp->rn+1;
strcpy(node->name,name);
node->q = q;
node->cost=cost;
node->next = NULL;
temp->next = node;
size++;
return true;
}
bool updateq(int q, int rn)
{
inv* temp = I;
while(temp != NULL)
{
if(temp-> rn == rn) break;
temp = temp->next;
}
if(temp==NULL)
return false;
temp -> q = q;
return true;
}
bool updatecost(float cost, int rn)
{
inv* temp = I;
while(temp != NULL)
{
if(temp-> rn == rn) break;
temp = temp->next;
}
if(temp==NULL) return false;
temp -> cost = cost;
return true;
}
int main()
{
printf("Enter the filename: ");
char file[1000];
scanf("%s",file);
FILE* fp = fopen(file,"r");
int cn,q; char name[1000];
float cost;
inv* temp;
while(1)
{
if(fscanf(fp,"%d",&cn) == EOF) break;
fscanf(fp,"%s",name);
fscanf(fp,"%d",q);
fscanf(fp,"%f",cost);
inv* node = (inv*)malloc(sizeof(inv));
node->rn = temp->rn+1;
strcpy(node->name,name);
node->q = q; node->cost=cost;
node->next = NULL; I->next = node;
size++;
}
fclose(fp);
int choice;
while(1)
{
printf("PRESS 1 for view Inventory ");
printf("PRESS 2 to add a new Item ");
printf("PRESS 3 to update quantity of an Item ");
printf("PRESS 4 to update cost of an Item ");
printf("PRESS 0 to EXIT ");
printf("ENTER YOUR CHOICE ");
scanf("%d",&choice);
switch(choice)
{
fp = fopen(file,"w");
case 0: while(I!=NULL)
{
fprintf(fp,"%d %s %d %f ",I->rn,I->name,I->q,I->cost);
I = I->next;
}
fclose(fp);
return 1;
case 1:
printinv();
break;
case 2:
printf("Enter the name quantity and cost of this new item ");
scanf("%s",name);
scanf("%d",&q);
scanf("%f",&cost);
addnew(name,q,cost);
break; case 3:
printf("Enter the unique id and quantity of the item ");
scanf("%d%d",&cn,&q);
if(updateq(q,cn))
printf("Update Successful ");
else printf("Update Failed ");
break;
case 4:
printf("Enter the unique id and cost of the item ");
scanf("%d%f",&cn,&cost);
if(updatecost(cost,cn))
printf("Update Successful ");
else printf("Update Failed ");
break;
default:
printf("Please Enter a Valid Choice ");
}
}
return 0;
}
Explanation / Answer
segmentation fault may occur , if you are acessing memory which is out of bounds.
for example, you declared an array of char name which is of 1000 size. if u are acessing name[1001] then segmentation fault may occur.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.