1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

student data base program having segmentation fault error

Discussion in 'C' started by omprakashcn, Feb 16, 2010.

  1. omprakashcn

    omprakashcn New Member

    Joined:
    Feb 16, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    //toverify:-1st enter 1 then enter1 then add the info...now enter 2 you will get the error???please ans
    Code:
    #include<stdio.h>
    #include<math.h>
    #include<conio.h>
    #include <stdlib.h>
    #include<string.h>
    
    void student_db();
    void disp_info();
    void edit_info();
    
    struct s_database
    {
        char s_name[10];
        char s_ph_number[10];
        char s_ID_number[10];
    }database[10];
    
    int main()
    {
        char num;
        while(1)
        {
    
        printf("Enter 1-> To create New Student\nEnter 2-> To Display Information of all students\nEnter 3-> To Edit Students Info\n");
        fflush(stdin);
        scanf("%d",&num);
        //num=0;          //num=0 should be initialized here because if not than switch will be executed twice.
        switch(num)
        {
            case 1:
            {
                student_db();
                break;
            }
            case 2:
            {
                disp_info();
                break;
            }
            case 3:
            {
                edit_info();
                break;
            }
            case 4:
            {
                exit(1);;
            }
            default:
            {
                printf("Invalid number entered...please select 1,2 or 3\n");
              //  num=0;          //num=0 should be initialized here because if not than switch will be executed twice.
                break;
                getch();
            }
        }
        }
        //getch();
        return 0;
    }
    void student_db()
    {
        char no=0;
        int i=1;
        printf("--------------------------------------------------------------------------------\n");
        printf("Enter the number of students information you will be adding to the database\n");
        fflush(stdin);
        scanf("%d",&no);
        printf("--------------------------------------------------------------------------------\n");
        if(no>0)
        {
            printf("Enter students name\t\tcontact number\t\tID_number\n");
        while(no!=0)
        {
            printf("Enter student[%d]  Information\n",i);
            fflush(stdin);
            gets(database[no].s_name);
            fflush(stdin);
            gets(database[no].s_ph_number);
            fflush(stdin);
            gets(database[no].s_ID_number);
           // fflush(stdin);
            no--;
            i++;
        }
        printf("Student Info saved \n");
        getch();
    
        //student_db();
        }
        else
        {
            printf("Invalid number entered...\n");
            //goto step1;
            student_db();
        }
    }
    void disp_info()
    {
        int no;
        for(no=0;no<10;no++)
        {
        puts(database[no].s_name);
        puts(database[no].s_ph_number);
        puts(database[no].s_ID_number);
        }
    }
    void edit_info()
    {
        char name[10],no;
        printf("enter the students name to edit his database\n");
        //getch();
        fflush(stdin);
        gets(name);
    
        for(no=0;no<10;no++)
        {
        if(!(strcmp(database[no].s_name,name)))
        {
            printf("Enter students name\t contact number\t and ID_number\n");
            gets(database[no].s_name);
            gets(database[no].s_ph_number);
            gets(database[no].s_ID_number);
        }
        }
        printf("entered name is not in the student database list\n");
    }
     
    Last edited by a moderator: Feb 17, 2010
  2. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    you had a lot of errors in your code,i fixed most of them.

    Code:
    #include<stdio.h>
    #include<math.h>
    #include<conio.h>
    #include <stdlib.h>
    #include<string.h>
    
    void student_db();
    void disp_info();
    void edit_info();
    
    struct s_database{
        char s_name[10];
        char s_ph_number[10];
        char s_ID_number[10];
    }database[10];
    
    int countStudents=0;
    int main(){
        char num;
        while(1){
            printf("Enter 1-> To create New Student\nEnter 2-> To Display Information of all students\nEnter 3-> To Edit Students Info\n");
            fflush(stdin);
            scanf("%d",&num);
    //num=0; //num=0 should be initialized here because if not than switch will be executed twice.
            switch(num){
                case 1:{
                    student_db();
                    break;
                }
                case 2:{
                    disp_info();
                    break;
                }
                case 3:{
                    edit_info();
                    break;
                }
                case 4:{
                    exit(1);
                }
                default:{
                    printf("Invalid number entered...please select 1,2 or 3\n");
    // num=0; //num=0 should be initialized here because if not than switch will be executed twice.
                    break;
                    getch();
                }
            }
        }
        //getch();
        return 0;
    }
    
    
    void student_db(){
        int no=0;
        countStudents=0;
        int error=1;
        int i=1;
        while(error==1){
            error=0;
            printf("--------------------------------------------------------------------------------\n");
            printf("Enter the number of students information you will be adding to the database\n");
            fflush(stdin);
            scanf("%d",&no);
            printf("--------------------------------------------------------------------------------\n");
            if(no>0){
                printf("Enter students name\t\tcontact number\t\tID_number\n");
                while(no!=0){
                    countStudents++;
                    printf("Enter student[%d] Information\n",i);
                    fflush(stdin);
                    gets(database[i-1].s_name);
                    fflush(stdin);
                    gets(database[i-1].s_ph_number);
                    fflush(stdin);
                    gets(database[i-1].s_ID_number);
                    // fflush(stdin);
                    no--;
                    i++;
                }
                printf("Student Info saved \n");
                
                getch();
                //student_db();
            }else{
                printf("Invalid number entered...\n");
                error=1;
            }
            //goto step1;
            //student_db();
        }
    }
    
    void disp_info(){
        int no;
        for(no=0;no<countStudents;no++){
            puts(database[no].s_name);
            puts(database[no].s_ph_number);
            puts(database[no].s_ID_number);
        }
    }
    
    void edit_info(){
        char name[10],no;
        int found=0;
        printf("enter the students name to edit his database\n");
        //getch();
        fflush(stdin);
        gets(name);
        for(no=0;no<countStudents;no++){
            if(strcmp(database[no].s_name,name)==0){
                printf("Enter students name\t contact number\t and ID_number\n");
                gets(database[no].s_name);
                gets(database[no].s_ph_number);
                gets(database[no].s_ID_number);
                found=1;
            }
        }
        if (found==0) printf("entered name is not in the student database list\n");
    } 
    
    
    
     
  3. omprakashcn

    omprakashcn New Member

    Joined:
    Feb 16, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    hey virxen... good one...but now check this first Enter 1-> To create New Student

    2)printf("Enter the number of students information you will be adding to the database\n");
    for this try inserting char value .u will get magical erronious output...code is not fool proof man...still thanks for your reply....ill be posting my new code by morrow...with no error :)
     
  4. omprakashcn

    omprakashcn New Member

    Joined:
    Feb 16, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    here is the code which is working fool proof...but when i try to display info saved by pressing 3 and enter wrong students name im getting
    printf("entered name is not in the student database list\n");
    it 10 times...help me in reducing it to only one time:mean:
    Code:
    #include<stdio.h>
    #include<math.h>
    #include<conio.h>
    #include <stdlib.h>
    #include<string.h>
    
    void student_db();
    void disp_info();
    void edit_info();
    
    struct s_database
    {
        char s_name[10];
        char s_ph_number[10];
        char s_ID_number[10];
    }database[10];
    
    int main()
    {
    
        while(1)
        {
        static char num;
    
        printf("Enter 1-> To create New Student\nEnter 2-> To Display Information of all students\nEnter 3-> To Edit Students Info\n");
        fflush(stdin);
        scanf("%d",&num);
        //num=0;          //num=0 should be initialized here because if not than switch will be executed twice.
        switch(num)
        {
            case 1:
            {
                student_db();
                break;
            }
            case 2:
            {
                disp_info();
                break;
            }
            case 3:
            {
                edit_info();
                break;
            }
            case 4:
            {
                exit(1);;
            }
            default:
            {
                printf("Invalid number entered...please select 1,2 or 3\n");
              //  num=0;          //num=0 should be initialized here because if not than switch will be executed twice.
                break;
                getch();
            }
        }
        }
        //getch();
        return 0;
    }
    void student_db()
    {
        int no;
        int i=1;
        int j=0;
        printf("--------------------------------------------------------------------------------\n");
        printf("Enter the number of students information you will be adding to the database\n");
        fflush(stdin);
        scanf("%d",&no);
    
        printf("--------------------------------------------------------------------------------\n");
    
        if((no!=1)&&(no!=2)&&(no!=3))
        {
            printf("Invalid number entered...\n");
            //goto step1;
            student_db();
        }
        printf("Enter students name\t\tcontact number\t\tID_number\n");
    
        while(no!=0)
        {
            printf("Enter student[%d]  Information\n",i);
            fflush(stdin);
            gets(database[j].s_name);      //insted of j if i put no..it will be an error..which leads to 2,1,0 arrayfeed
            fflush(stdin);
            gets(database[j].s_ph_number);
            fflush(stdin);
            gets(database[j].s_ID_number);
           // fflush(stdin);
            --no;
            ++i;
            ++j;
        }
        printf("Student Info saved \n");
        printf("--------------------------------------------------------------------------------\n");
        //getch();
    
        //student_db();
    }
    
    void disp_info()
    {
        int no;
        for(no=0;no<10;no++)
        {
        puts(database[no].s_name);
        puts(database[no].s_ph_number);
        puts(database[no].s_ID_number);
        }
    }
    void edit_info()
    {
        char name[10],no;
        printf("enter the students name to edit his database\n");
        //getch();
        fflush(stdin);
        gets(name);
    
        for(no=0;no<10;no++)
        {
        if(!(strcmp(database[no].s_name,name)))
        {
            printf("Enter students name\t contact number\t and ID_number\n");
            gets(database[no].s_name);
            gets(database[no].s_ph_number);
            gets(database[no].s_ID_number);
            printf("Student Info saved \n");
        }
        else
        {
            printf("entered name is not in the student database list\n");
        }
        }
    
    }
     
    Last edited by a moderator: Feb 17, 2010
  5. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    Code:
    #include<stdio.h>
    #include<math.h>
    #include<conio.h>
    #include <stdlib.h>
    #include<string.h>
    
    void student_db();
    void disp_info();
    void edit_info();
    
    struct s_database
    {
        char s_name[10];
        char s_ph_number[10];
        char s_ID_number[10];
    }database[10];
    
    int main()
    {
    
        while(1)
        {
        static char num;
    
        printf("Enter 1-> To create New Student\nEnter 2-> To Display Information of all students\nEnter 3-> To Edit Students Info\n");
        fflush(stdin);
        scanf("%d",&num);
        //num=0;          //num=0 should be initialized here because if not than switch will be executed twice.
        switch(num)
        {
            case 1:
            {
                student_db();
                break;
            }
            case 2:
            {
                disp_info();
                break;
            }
            case 3:
            {
                edit_info();
                break;
            }
            case 4:
            {
                exit(1);[COLOR=Red];//i think one is enough !!![/COLOR]
            }
            default:
            {
                printf("Invalid number entered...please select 1,2 or 3\n");
              //  num=0;          //num=0 should be initialized here because if not than switch will be executed twice.
                break;
                getch();[COLOR=Red]//this is never executed !!![/COLOR]
            }
        }
        }
        //getch();
        return 0;
    }
    void student_db()[COLOR=Red]//*** why did you use the limit of 1,2, or 3 students info entered per[/COLOR] time?
    {
        int no=0;
        int i=1;
        int j=0;
       [COLOR=Red] while(no<1 || no>3){//*** use this instead of calling again the function[/COLOR]
            printf("--------------------------------------------------------------------------------\n");
            printf("Enter the number of students information you will be adding to the database\n");
            fflush(stdin);
            scanf("%d",&no);
    
            printf("--------------------------------------------------------------------------------\n");
    
            if((no!=1)&&(no!=2)&&(no!=3))
            {
                printf("Invalid number entered...\n");
            //goto step1;
            [COLOR=Red]//student_db();//*** this is also big mistake ****[/COLOR]
            }
        [COLOR=Red]}//end while[/COLOR]
        printf("Enter students name\t\tcontact number\t\tID_number\n");
    
        while(no!=0)
        {
            printf("Enter student[%d]  Information\n",i);
            fflush(stdin);
            gets(database[j].s_name);      //insted of j if i put no..it will be an error..which leads to 2,1,0 arrayfeed
            fflush(stdin);
            gets(database[j].s_ph_number);[COLOR=Red]//***INSTEAD OF USING J use i-1,no need for extra variable***[/COLOR]
            fflush(stdin);
            gets(database[j].s_ID_number);
           // fflush(stdin);
            --no;[COLOR=Red]// do you know the difference between   i++;  and ++i; ???[/COLOR]
            ++i;
            ++j;
        }
        printf("Student Info saved \n");
        printf("--------------------------------------------------------------------------------\n");
        //getch();
    
        //student_db();
    }
    
    void disp_info()
    {
        int no;
        for(no=0;no<10;no++)[COLOR=Red]//*** this is also big mistake ****[/COLOR]
        {[COLOR=Red]// *** by using 10 (maximum capacity of the array) instead of the real number of students saved in it[/COLOR]
        [COLOR=Red]//**** will result showing even the empty ones!!!
        // *** see my previous post where i did it with a global variable.[/COLOR]
        puts(database[no].s_name);
        puts(database[no].s_ph_number);
        puts(database[no].s_ID_number);
        }
    }
    void edit_info()
    {
        char name[10],no;
        int found=0;[COLOR=Red]// *** use this trick in order to know if you found the name you were looking for or not[/COLOR]
        printf("enter the students name to edit his database\n");
        //getch();
        fflush(stdin);
        gets(name);
    
        for(no=0;no<10;no++)[COLOR=Red]//*** this is also big mistake ****[/COLOR]
        {
        if(!(strcmp(database[no].s_name,name)))
        {
            printf("Enter students name\t contact number\t and ID_number\n");
            gets(database[no].s_name);
            gets(database[no].s_ph_number);
            gets(database[no].s_ID_number);
            printf("Student Info saved \n");
            [COLOR=Red]found=1;//***congratulations you found the name
            break;//*** when you look for a name consider that you may have more than one hits!!![/COLOR]
            [COLOR=Red]//*** i use break in order to change only the first instance of the name.[/COLOR]
        }
        [COLOR=Red]//else //*** this is called 10 times
        //{
            //printf("entered name is not in the student database list\n");
        //}[/COLOR]
           }
        [COLOR=Red]if (found==0) printf("entered name is not in the student database list\n");//*** show message only if you don't find the name
         //*** also notice that this if must reside outside the for,for obvious reasons.[/COLOR]
    }
    
    
    

    i think that the comments inside the code are more than enough.
    for more questions you can post again.
     
  6. omprakashcn

    omprakashcn New Member

    Joined:
    Feb 16, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    int found=0;// *** use this trick in order to know if you found the name you were looking for or no...


    NICE one :)

    now i think some changes would have been better...im planning of using files in my program...i implement some of ur idea in my code...

    ++i This is pre increment.
    i++ this is post increment.post decrement means that the variable is decremented after the expression is evaluated
    now dont ask me the question like...how come u knew the ans...
     

Share This Page