Like Tree2Likes
  • 2 Post By anduril462

student database with linked list access violation (segmentation fault) problem

This is a discussion on student database with linked list access violation (segmentation fault) problem within the C Programming forums, part of the General Programming Boards category; hi all, when i run the following cod it ok but when sorting initial student name it shows me the ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    7

    student database with linked list access violation (segmentation fault) problem

    hi all, when i run the following cod it ok but when sorting initial student name it shows me the run time error "access violation (segmentation fault) raised in your program" in DEVc++
    please help me fix this error.
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,555
    Many people don't like downloading files, and it's much easier for us to spot your problems if we can actually see the code. In the future, please paste your code in code tags (as plain text, with proper indentation). It should look like this:
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<dos.h>
    
    
    struct stud
    {
        int rn,id,ph;
        char add[30],na[20],d[15],in[5];
        struct stud *next;
    }*h= NULL ,*p,*q,*t;
    
    
    void add()
    {
        p = new stud;
        printf("\nEnter the Initials of Student : ");
        scanf("%s",&p->in);
        printf("\nEnter the Last Name of Student : ");
        scanf("%s",&p->na);
        printf("\nEnter the ID of Student : ");
        scanf("%d",&p->id);
        printf("\nEnter the Roll No. of Student : ");
        scanf("%d",&p->rn);
        printf("\nEnter the Ph No. of Student : ");
        scanf("%d",&p->ph);
        printf("\nEnter the Address of Student : ");
        scanf("%s",&p->add);
        printf("\nEnter the D.O.B. of Student(dd/mm/yyyy) : ");
        scanf("%s",&p->d);
    
    
        p->next=NULL;
    
    
        if(h==NULL)
        {
            h=p;
        }
        else
        {
            q=h;
            while(q->next!=NULL)
                q=q->next;
            q->next=p;
        }
    }
    
    
    void addAt(int r)
    {
        q=h;
        while(q->rn!=r || q==NULL)
            q=q->next;
        if(q->rn==r)
        {
            p =(stud *)malloc(sizeof(stud));
            printf("\nEnter the Initials of Student : ");
            scanf("%s",&p->in);
            printf("\nEnter the Last Name of Student : ");
            scanf("%s",&p->na);
            printf("\nEnter the ID of Student : ");
            scanf("%d",&p->id);
            printf("\nEnter the Roll No. of Student : ");
            scanf("%d",&p->rn);
            printf("\nEnter the Ph No. of Student : ");
            scanf("%d",&p->ph);
            printf("\nEnter the Address of Student : ");
            scanf("%s",&p->add);
            printf("\nEnter the D.O.B. of Student(dd/mm/yyyy) : ");
            scanf("%s",&p->d);
            p->next=q->next;
            q->next=p;
    
    
        }
        else
        {
            printf("\n\nRecord Not Found.");
        }
    }
    
    
    
    
    void delAt(int r)
    {
        q=h;
        while((q->next)->rn!=r || q==NULL)
            q=q->next;
        if((q->next)->rn==r)
        {
            q->next=(q->next)->next;
            printf("\n\nRecord Deleted.");
    
    
        }
        else
            printf("\n\nRecord Not Found.");
    }
    
    
    void modAt(int id)
    {
        q=h;
        int ch;
        while(q->id!=id && q!=NULL)
            q=q->next;
        if(q->id==id)
        {
    
    
            printf("******* MODIFY *******\n1.Name Initials\n2.Last Name");
            printf("\n3.Roll No.\n4.Phone\n5.Add\n6.D.O.B.:\nEnter choice: ");
            scanf("%d",&ch);
            switch(ch)
            {
                case 1 :  printf("\n\nEnter the Initials of Student : ");
                          scanf("%s",&q->in);break;
                case 2 :  printf("\nEnter the Last Name of Student : ");
                          scanf("%s",&q->na);break;
                case 3 :  printf("\nEnter the Roll No. of Student : ");
                          scanf("%d",&q->rn);break;
                case 4 :  printf("\nEnter the Ph No. of Student : ");
                          scanf("%d",&q->ph);break;
                case 5 :  printf("\nEnter the Address of Student : ");
                          scanf("%s",&q->add);break;
                case 6 :  printf("\nEnter the D.O.B. of Student(dd/mm/yyyy) : ");
                          scanf("%s",&q->d);break;
            }
        }
        else
            printf("\nRecord not Found.");
    }
    
    
    void search(char ni[5])
    {
        int flag=0;
        q=h;
    
    
        while(q!=NULL)
        {
            if(stricmp(q->in,ni)==0)
            {
                flag=1;
                printf("\n\nInitals of Student :   %s ",q->in);
                printf("\n\nLast Name of Student : %s ",q->na);
                printf("\n\nID of Student :        %d ",q->id);
                printf("\n\nRoll No. of Student :  %d",q->rn);
                printf("\n\nPh No. of Student :    %d",q->ph);
                printf("\n\nAddress of Student :   %s",q->add);
                printf("\n\nD.O.B. of Student :    %s",q->d);
                printf("\n\n\n");
            }
            q=q->next;
        }
        if(flag==0)
            printf("\n\nNo Match Found.");
    }
    
    
    void sort()
    {
        p=h;
        while(p!=NULL)
        {
            q=h;
            while(q!=NULL)
            {
                if(stricmp(q->in,(q->next)->in)>0)
                {
                    strcpy(t->in,q->in);
                    strcpy(t->na,q->na);
                    t->id=q->id;
                    t->rn=q->rn;
                    t->ph=q->ph;
                    strcpy(t->add,q->add);
                    strcpy(t->d,q->d);
    
    
    
    
                    strcpy(q->in,(q->next)->in);
                    strcpy(q->na,(q->next)->na);
                    q->id=  (q->next)->id;
                    q->rn=  (q->next)->rn;
                    q->ph=  (q->next)->ph;
                    strcpy(q->add,(q->next)->add);
                    strcpy(q->d,(q->next)->d);
    
    
                    strcpy((q->next)->in,t->in);
                    strcpy((q->next)->na,t->na);
                    (q->next)->id = t->id;
                    (q->next)->rn = t->rn;
                    (q->next)->ph = t->ph;
                    strcpy((q->next)->add,t->add);
                    strcpy((q->next)->d,t->d);
    
    
                }
                q=q->next;
            }
            p=p->next;
        }
    }
    void disp()
    {
        p=h;
    
    
        while(p!=NULL)
        {
            printf("\nInitals of Student :   %s ",p->in);
            printf("\nName of Student :      %s ",p->na);
            printf("\nID of Student :        %d ",p->id);
            printf("\nRoll No. of Student :  %d",p->rn);
            printf("\nPh No. of Student :    %d",p->ph);
            printf("\nAddress of Student :   %s",p->add);
            printf("\nD.O.B. of Student :    %s",p->d);
            printf("\n\n");
            p=p->next;
        }
    }
    
    
    main()
    {
        int ch=0,r;
        char ni[5];
    
    
        while(ch!=8)
        {
    
    
            printf("1.Add the Record.\n\n2.Add Record at Locn.\n\n3.Delete Record.");
            printf("\n\n4.Modify Record.\n\n5.Search Record.\n\n6.Sort Records.");
            printf("\n\n7.Display\n\n8.Exit");
            printf("\n\nEnter the Choice: ");
            scanf("%d",&ch);
            switch(ch)
            {
                case 1:
                    add();
                    break;
    
    
                case 2:
                    printf("\nEnter the Roll No. : ");
                    scanf("%d",&r);
                    addAt(r);
                    break;
    
    
                case 3:
                    printf("\nEnter the Roll No. : ");
                    scanf("%d",&r);
                    delAt(r);
                    break;
    
    
                case 4:
                    printf("\nEnter the ID : ");
                    scanf("%d",&r);
                    modAt(r);
                    break;
    
    
                case 5:
                    printf("\nEnter the Initials : ");
                    scanf("%s",&ni);
                    search(ni);
                    break;
    
    
                case 6:
                    sort( );
                    printf("\n\nSorted");
                    break;
                case 7:
                    disp();
                    break;
            }
            getch();
        }
        getch();
    }
    Code:
    #include<conio.h>
    ...
    #include<dos.h>
    ...
    getch();
    Those lines are Windows-specific. conio.h and dos.h are very outdated. Since you're barely using them, remove them. Also, you can use getchar(), which is more portable, in place of getch().

    After I made that fix, I compiled your program:
    Code:
    $ make stud
    gcc -Wall -Werror -ggdb3 -std=c99 -pedantic  -lm -lpthread  stud.c   -o stud
    stud.c: In function ‘add’:
    stud.c:16:9: error: ‘new’ undeclared (first use in this function)
    stud.c:16:9: note: each undeclared identifier is reported only once for each function it appears in
    stud.c:16:13: error: expected ‘;’ before ‘stud’
    stud.c:18:5: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[5]’ [-Werror=format]
    stud.c:20:5: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[20]’ [-Werror=format]
    stud.c:28:5: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[30]’ [-Werror=format]
    stud.c:30:5: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[15]’ [-Werror=format]
    stud.c: In function ‘addAt’:
    stud.c:54:13: error: ‘stud’ undeclared (first use in this function)
    stud.c:54:19: error: expected expression before ‘)’ token
    stud.c:56:9: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[5]’ [-Werror=format]
    stud.c:58:9: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[20]’ [-Werror=format]
    stud.c:66:9: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[30]’ [-Werror=format]
    stud.c:68:9: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[15]’ [-Werror=format]
    stud.c: In function ‘modAt’:
    stud.c:110:23: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[5]’ [-Werror=format]
    stud.c:112:23: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[20]’ [-Werror=format]
    stud.c:118:23: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[30]’ [-Werror=format]
    stud.c:120:23: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[15]’ [-Werror=format]
    stud.c: In function ‘search’:
    stud.c:134:9: error: implicit declaration of function ‘stricmp’ [-Werror=implicit-function-declaration]
    stud.c: At top level:
    stud.c:211:1: error: return type defaults to ‘int’ [-Werror]
    stud.c: In function ‘main’:
    stud.c:250:17: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[5]’ [-Werror=format]
    cc1: all warnings being treated as errors
    make: *** [stud] Error 1
    I don't know how you are getting a seg fault, this doesn't even compile. If it compiles for you, you need to turn up the warnings on your compiler, or better yet, get a more current compiler (Dev-C++ is outdated and no longer supported).

    As for the problems

    • Line 16: You need to pick a language and stick to it. Your code is 99% C, so I would just use C. 'new' is a C++ operator. In C you can use malloc, which is roughly equivalent (you do this on line 54). Also, you must give the full type: struct stud. stud, on it's own, does not exist.
    • All the "error: format ‘%s’ expects" errors are because you don't need the & when using an array with scanf. The name of the array (e.g. p->add) is already a pointer to the first element, so adding the & is wrong.
    • Line 134: This is not a standard function, maybe it is provided by conio.h or dos.h, I don't know because I don't have those headers. You could write your own very easily however.
    • Line 211: You should declare main to explicitly return an int, and then actually return one at the end (see below).


    Code:
    int main(void)
    {
        ...
        return 0;
    }
    There may be more, but that is plenty for you to work on for now. When you get that finished, and if you still have this seg fault you can't find, then you can come back here for more help. If/when you do so, please provide your updated code along with whatever input causes the seg fault, so we can help you debug.
    Last edited by anduril462; 11-07-2012 at 12:12 PM.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,555
    One more thing, read this link: Global Variables Are Bad. Declare all your variables in the appropriate functions and pass them around as needed. Globals, in this case, probably hurt more than they help. Also, h, p, q and t are horrible names, as are the struct members rn, , ph, na, d and in. Use descriptive names, like head, tail, temp, phone, etc. That will help us (and you) identify the purpose of the variable, so we can know whether you're using the variable correctly.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    7
    anduril462 i did what you said but the same error appears the code is about student database which contains the student initial name, last name, id, roll no., phone no., address and bate of birth of student all things is ok but when i select the chice no 6 sort records according to initial name the error appears.
    i think the error in the sort function and what is the good compiler.
    thanks in advance.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,555
    Quote Originally Posted by Mohamed Sadek View Post
    anduril462 i did what you said but the same error appears the code is about student database which contains the student initial name, last name, id, roll no., phone no., address and bate of birth of student all things is ok but when i select the chice no 6 sort records according to initial name the error appears.
    i think the error in the sort function and what is the good compiler.
    thanks in advance.
    Quote Originally Posted by anduril462 View Post
    When you get that finished, and if you still have this seg fault you can't find, then you can come back here for more help. If/when you do so, please provide your updated code along with whatever input causes the seg fault, so we can help you debug.
    Please give new source code, and provide actual input, not just a description of the fields you want. It may be that the names you pick, your birthday format, etc is causing the bug. Don't make me guess at how to replicate this. I can probably find the seg fault in 2 min if you provide the info I asked for.

    EDIT: As for compilers, I really like GCC, which is the default for most Linux distributions. A Windows port is also available (called MinGW), and comes with Code::Blocks, an IDE. Another Windows alternative is Pelles C, and even MS Visual C++ has a free version that compiles C code.
    Last edited by anduril462; 11-08-2012 at 11:50 AM.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    7
    insert
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<dos.h>
    
    struct stud
    {
     int rn,id,ph;
     char add[30],na[20],d[15],in[5];
     struct stud *next;
    }*h= NULL ,*p,*q,*t;
    
    void add()
    {
      p =(struct stud *)malloc(sizeof(stud));
      printf("\nEnter the Initials of Student : ");
      scanf("%s",p->in);
      printf("\nEnter the Last Name of Student : ");
      scanf("%s",p->na);
      printf("\nEnter the ID of Student : ");
      scanf("%d",&p->id);
      printf("\nEnter the Roll No. of Student : ");
      scanf("%d",&p->rn);
      printf("\nEnter the Ph No. of Student : ");
      scanf("%d",&p->ph);
      printf("\nEnter the Address of Student : ");
      scanf("%s",p->add);
      printf("\nEnter the D.O.B. of Student(dd/mm/yyyy) : ");
      scanf("%s",p->d);
    
      p->next=NULL;
    
      if(h==NULL)
      {
       h=p;
      }
      else
      {
        q=h;
        while(q->next!=NULL)
        q=q->next;
        q->next=p;
      }
    }
    
    void addAt(int r)
    {
     q=h;
     while(q->rn!=r || q==NULL)
      q=q->next;
     if(q->rn==r)
     {
        p =(struct stud *)malloc(sizeof(stud));
      printf("\nEnter the Initials of Student : ");
      scanf("%s",p->in);
      printf("\nEnter the Last Name of Student : ");
      scanf("%s",p->na);
      printf("\nEnter the ID of Student : ");
      scanf("%d",&p->id);
      printf("\nEnter the Roll No. of Student : ");
      scanf("%d",&p->rn);
      printf("\nEnter the Ph No. of Student : ");
      scanf("%d",&p->ph);
      printf("\nEnter the Address of Student : ");
      scanf("%s",p->add);
      printf("\nEnter the D.O.B. of Student(dd/mm/yyyy) : ");
      scanf("%s",p->d);
      p->next=q->next;
      q->next=p;
     
     }
     else
     {
      printf("\n\nRecord Not Found.");
     }
    }
    
    
    void delAt(int r)
    {
     q=h;
     while((q->next)->rn!=r || q==NULL)
      q=q->next;
     if((q->next)->rn==r)
     {
      q->next=(q->next)->next;
      printf("\n\nRecord Deleted.");
      
     }
     else
      printf("\n\nRecord Not Found.");
    }
    
    void modAt(int id)
    {
     q=h;
     int ch;
     while(q->id!=id && q!=NULL)
      q=q->next;
     if(q->id==id)
     {
      
      printf("******* MODIFY *******\n1.Name Initials\n2.Last Name");
      printf("\n3.Roll No.\n4.Phone\n5.Add\n6.D.O.B.:\nEnter choice: ");
      scanf("%d",&ch);
      switch(ch)
      {
      case 1 :  printf("\n\nEnter the Initials of Student : ");
              scanf("%s",q->in);break;
      case 2 :  printf("\nEnter the Last Name of Student : ");
              scanf("%s",q->na);break;
      case 3 :  printf("\nEnter the Roll No. of Student : ");
              scanf("%d",&q->rn);break;
      case 4 :  printf("\nEnter the Ph No. of Student : ");
              scanf("%d",&q->ph);break;
      case 5 :  printf("\nEnter the Address of Student : ");
              scanf("%s",q->add);break;
      case 6 :  printf("\nEnter the D.O.B. of Student(dd/mm/yyyy) : ");
              scanf("%s",q->d);break;
      }
     }
     else
      printf("\nRecord not Found.");
    }
    
    void search(char ni[5])
    {
     int flag=0;
     q=h;
     
     while(q!=NULL)
     {
      if(stricmp(q->in,ni)==0)
      {
      flag=1;
      printf("\n\nInitals of Student :   %s ",q->in);
      printf("\n\nLast Name of Student : %s ",q->na);
      printf("\n\nID of Student :        %d ",q->id);
      printf("\n\nRoll No. of Student :  %d",q->rn);
      printf("\n\nPh No. of Student :    %d",q->ph);
      printf("\n\nAddress of Student :   %s",q->add);
      printf("\n\nD.O.B. of Student :    %s",q->d);
      printf("\n\n\n");
      }
      q=q->next;
     }
     if(flag==0)
      printf("\n\nNo Match Found.");
    }
    
    void sort()
    {
     p=h;
     while(p!=NULL)
     {
      q=h;
      while(q!=NULL)
      {
       if(stricmp(q->in,(q->next)->in)>0)
       {
        strcpy(t->in,q->in);
        strcpy(t->na,q->na);
        t->id=q->id;
        t->rn=q->rn;
        t->ph=q->ph;
        strcpy(t->add,q->add);
        strcpy(t->d,q->d);
    
    
        strcpy(q->in,(q->next)->in);
        strcpy(q->na,(q->next)->na);
        q->id=  (q->next)->id;
        q->rn=  (q->next)->rn;
        q->ph=  (q->next)->ph;
        strcpy(q->add,(q->next)->add);
        strcpy(q->d,(q->next)->d);
    
        strcpy((q->next)->in,t->in);
        strcpy((q->next)->na,t->na);
        (q->next)->id = t->id;
        (q->next)->rn = t->rn;
        (q->next)->ph = t->ph;
        strcpy((q->next)->add,t->add);
        strcpy((q->next)->d,t->d);
    
       }
       q=q->next;
      }
      p=p->next;
     }
    }
    void disp()
    {
     p=h;
     
     while(p!=NULL)
     {
      printf("\nInitals of Student :   %s ",p->in);
      printf("\nName of Student :      %s ",p->na);
      printf("\nID of Student :        %d ",p->id);
      printf("\nRoll No. of Student :  %d",p->rn);
      printf("\nPh No. of Student :    %d",p->ph);
      printf("\nAddress of Student :   %s",p->add);
      printf("\nD.O.B. of Student :    %s",p->d);
      printf("\n\n");
      p=p->next;
     }
    }
    
     int main(void)
    {
     int ch=0,r;
     char ni[5];
     
     while(ch!=8)
     {
      
      printf("1.Add the Record.\n\n2.Add Record at Locn.\n\n3.Delete Record.");
      printf("\n\n4.Modify Record.\n\n5.Search Record.\n\n6.Sort Records.");
      printf("\n\n7.Display\n\n8.Exit");
      printf("\n\nEnter the Choice: ");
      scanf("%d",&ch);
       switch(ch)
       {
        case 1:
        add();
        break;
    
        case 2:
        printf("\nEnter the Roll No. : ");
        scanf("%d",&r);
        addAt(r);
        break;
    
        case 3:
        printf("\nEnter the Roll No. : ");
        scanf("%d",&r);
        delAt(r);
        break;
    
        case 4:
        printf("\nEnter the ID : ");
        scanf("%d",&r);
        modAt(r);
        break;
    
        case 5:
        printf("\nEnter the Initials : ");
        scanf("%s",ni);
        search(ni);
        break;
    
        case 6:
        sort( );
        printf("\n\nSorted");
        break;
        case 7:
        disp();
        break;
       }
     getchar();
     }
     getchar();
     return 0;
    }

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    7
    i enter data as following
    m
    s
    1
    1
    124
    4214
    29/12/1983
    then
    a
    s
    2
    2
    234
    3245
    2/8/1980
    then choose choice no. 6 sort records and the error appears

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,555
    Okay, you fixed most of the problems I mentioned, but your code still doesn't compile. If it can't compile, then you can't have the same problem (seg fault when running it). Here's the error messages I get when compiling:
    Code:
    $ make stud
    gcc -Wall -Werror -ggdb3 -std=c99 -pedantic  -lm -lpthread -lefence  stud.c   -o stud
    stud.c: In function ‘add’:
    stud.c:16:37: error: ‘stud’ undeclared (first use in this function)
    stud.c:16:37: note: each undeclared identifier is reported only once for each function it appears in
    stud.c: In function ‘addAt’:
    stud.c:54:41: error: ‘stud’ undeclared (first use in this function)
    stud.c: In function ‘search’:
    stud.c:134:9: error: implicit declaration of function ‘stricmp’ [-Werror=implicit-function-declaration]
    cc1: all warnings being treated as errors
    make: *** [stud] Error 1
    Your malloc calls are still broken. The compiler doesn't know what 'stud' is. It knows what 'struct stud' is. Also, you shouldn't be casting malloc (read this link). There is a better way:
    Code:
    p = malloc(sizeof(*p));
    That way, whatever type of pointer p is, this malloc call will always allocate the correct size, the size of *p, which is the size of whatever type of object p points to.

    Now, you need to tell me exactly what input you give the program to cause this seg fault. The best way is to copy-paste what shows up on your terminal or in Dev-C++ when you run the program. Like this (I removed unnecessary blank lines to keep this short):
    Code:
    $ ./stud
    1.Add the Record.
    2.Add Record at Locn.
    3.Delete Record.
    4.Modify Record.
    5.Search Record.
    6.Sort Records.
    7.Display
    8.Exit
    
    
    Enter the Choice: 1
    
    
    Enter the Initials of Student : A
    Enter the Last Name of Student : Anderson
    Enter the ID of Student : 111
    Enter the Roll No. of Student : 42
    Enter the Ph No. of Student : 1234567890
    Enter the Address of Student : 987 any st
    Enter the D.O.B. of Student(dd/mm/yyyy) : 1.Add the Record.
    
    
    2.Add Record at Locn.
    3.Delete Record.
    4.Modify Record.
    5.Search Record.
    6.Sort Records.
    7.Display
    8.Exit
    
    
    Enter the Choice:
    Enter the Initials of Student :
    Enter the Last Name of Student : Balmer
    Enter the ID of Student : 222
    Notice the lines in red, how the program continued without me actually entering input. You have problems with how you are reading input, you have "dangling newlines". The user presses enter at the end of a line of input, and this puts a '\n' into the input stream, which is being picked up by your input routines. This is usually due to mixing character (getchar, scanf("%c")), line (fgets) and formatted input (scanf other than "%c"). Read this link for more info: http://cboard.cprogramming.com/c-pro...ml#post1126782.

    My recommendation is to read every single piece of input as a line with fgets, then parse out the number/DOB/name as the appropriate type with sscanf. Note, that first 's' means it scans a string (the string fgets read into) for the formatted input. This link shows the basic way to read a line with fgets: FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    7
    anyone can help me the code compile ok but when run and enter data then choose sort records the error appears
    Last edited by Mohamed Sadek; 11-08-2012 at 12:50 PM.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    2,568
    It appears that you've gone full circle here.
    anduril462 gave you some more fixes, and asked some questions. Start with that.

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    7
    Code:
    1.Add the Record.
    
    2.Add Record at Locn.
    
    3.Delete Record.
    
    4.Modify Record.
    
    5.Search Record.
    
    6.Sort Records.
    
    7.Display
    
    8.Exit
    
    Enter the Choice: 1
    
    Enter the Initials of Student : m
    
    Enter the Last Name of Student : s
    
    Enter the ID of Student : 1
    
    Enter the Roll No. of Student : 1
    
    Enter the Ph No. of Student : 21321
    
    Enter the Address of Student : 213
    
    Enter the D.O.B. of Student(dd/mm/yyyy) : 29/12/1983
    1.Add the Record.
    
    2.Add Record at Locn.
    
    3.Delete Record.
    
    4.Modify Record.
    
    5.Search Record.
    
    6.Sort Records.
    
    7.Display
    
    8.Exit
    
    Enter the Choice: 1
    
    Enter the Initials of Student : a
    
    Enter the Last Name of Student : s
    
    Enter the ID of Student : 2
    
    Enter the Roll No. of Student : 2
    
    Enter the Ph No. of Student : 23
    
    Enter the Address of Student : 432
    
    Enter the D.O.B. of Student(dd/mm/yyyy) : 2/8/1980
    1.Add the Record.
    
    2.Add Record at Locn.
    
    3.Delete Record.
    
    4.Modify Record.
    
    5.Search Record.
    
    6.Sort Records.
    
    7.Display
    
    8.Exit
    
    Enter the Choice: 6
    then the segmentation error appears anyone can help me

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,555
    Has your code changed from what you provided in post #6?
    - If so, please provide new code so I can actually debug the same code as you have. Don't make me ask this again.
    - If not, why have you not incorporated the changes I suggested? Some of them are "mandatory", some are merely "strong suggestions". Either way, you should implement them all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-08-2012, 01:05 AM
  2. Access Violation: Segmentation Fault help
    By Isaiah in forum C Programming
    Replies: 4
    Last Post: 02-09-2012, 06:53 AM
  3. Access Violation(Segmentation Fault) error
    By nirvana619 in forum C Programming
    Replies: 5
    Last Post: 08-27-2010, 08:43 AM
  4. Getting Access Violation (Segmentation Fault)
    By Brownie in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2008, 11:43 AM
  5. access violation (segmentation fault)
    By MarlonDean in forum C++ Programming
    Replies: 7
    Last Post: 05-16-2008, 05:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21