Thread: Structures in C

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    4

    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;
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    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
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    4
    That seemed to be the problem thx for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures as members of structures & offset
    By trish in forum C Programming
    Replies: 24
    Last Post: 07-16-2011, 05:46 PM
  2. Problems with Nested Structures and Arrays of Structures
    By Ignoramus in forum C Programming
    Replies: 4
    Last Post: 03-02-2010, 01:24 AM
  3. Accessing Structures Inside Structures
    By Mellowz in forum C Programming
    Replies: 1
    Last Post: 01-13-2008, 03:55 AM
  4. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM