-
Structures in C
Im having problems with segmentation faults and I also need help with case 3 deleting a ship from the array thx for the help.
Code:
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
char name[25];
char ship_type; //‘T’ = tanker ‘S’ = schooner ‘Y’ = yacht
int hostages;
float shipValue;
float ransomeDemand;
}VESSEL;
int main(){
char searchName[25];
int ShipsCaptured,i,input,c,j,searchCount=0,size=0;
int MAX =500;
VESSEL *vess;
vess=(VESSEL*)calloc(MAX,sizeof(VESSEL));
i=0;
while(strcmp(vess[i].name,"RUM")!=0){
printf("Name of ship ");
scanf("%s",&vess[i].name);
if(strcmp(vess[i].name,"RUM")!=0){
printf("Type of ship either a 'T' for tanker 'S' for schooner or 'Y' for yacht ");
scanf("%s",&vess[i].ship_type);
printf("Number of Hostages ");
scanf("%d",&vess[i].hostages);
printf("Ship Value ");
scanf("%f",&vess[i].shipValue);
printf("Ransom Demanded ");
scanf("%f",&vess[i].ransomeDemand);
i++;
size++;
}
}
while(input!=5){
printf("Type 1 to see a captured ships data\n2 add a ship\n3 remove a ship\n4 generatereport\n5 exit");
scanf("\n%d",&input);
switch(input)
{
case 1:
printf("Please enter ship name you wish to search: ");
scanf("%s",&searchName);
for(j=0;j<size;j++)
{
if(strcmp(vess[j].name,searchName)==0)
{
printf("\n%s\nName: %s\nShip Type: %d\nHostages: %.2f\nShip Value: %.2f Ransome Demanded\n\n",vess[j].name,vess[j].ship_type,vess[j].hostages,vess[j].shipValue,vess[j].ransomeDemand);
searchCount++;
}
}
if(searchCount==0)
{
printf("Invalid Ship Name\n");
}
break;
case 2: for(c=0;c<10;c++){
printf("Name of ship");
scanf("%s",&vess[c].name);
printf("Type of ship either a T for tanker S for schooner or Y for yacht");
scanf("%s",&vess[c].ship_type);
printf("Number of Hostages");
scanf("%d",&vess[c].hostages);
printf("Ship Value");
scanf("%f",&vess[c].shipValue);
printf("Ransom Demanded");
scanf("%f",&vess[c].ransomeDemand);
printf("congrats you added a ship");
}
break;
case 3:
printf("Please enter ship name you wish to delete: ");
scanf("%s",&searchName);
for(j=0;j<size;j++)
{
if(strcmp(vess[j].name,searchName)==0)
{
//vess[j].name="";
size--;
}
}
if(searchCount==0)
{
printf("Invalid ship Name\n");
}
break;
case 4:for(j=0;j<size;j++)
{
printf("\n%s\nName: %s\nShip Type: %d\nHostages: %.2f\nShip Value: %.2f Ransome Demanded\n\n",vess[j].name,vess[j].ship_type,vess[j].hostages,vess[j].shipValue,vess[j].ransomeDemand);
}
break;
}
}
return 0;
}
-
You should start by indenting your code properly. After doing that, run your code with a debugger and find out where it crashes. There is a bug somewhere before that point.
-
Your indentation is terrible.
Some things that I noticed -
Code:
||=== Build finished: 0 errors, 7 warnings ===|
...
scanf("%s",&vess[i].name);
should be
scanf("%s", vess[i].name);
char ship_type; //‘T’ = tanker ‘S’ = schooner ‘Y’ = yacht
should be
char ship_type[2]; //‘T’ = tanker ‘S’ = schooner ‘Y’ = yacht
You have no bounds checking for the user inputs
-
Just so that we know what to look for, what is the sequence of interaction you are having with this program that is causing a seg fault?
-
That seemed to be the problem thx for the help