student data base program having segmentation fault error

omprakashcn's Avatar, Join Date: Feb 2010
Light Poster
//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 shabbir; 17Feb2010 at 09:55.. Reason: Code blocks
virxen's Avatar, Join Date: Nov 2009
Pro contributor
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");
}
omprakashcn like this
omprakashcn's Avatar, Join Date: Feb 2010
Light Poster
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
omprakashcn's Avatar, Join Date: Feb 2010
Light Poster
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
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 shabbir; 17Feb2010 at 23:36.. Reason: Code blocks
virxen's Avatar, Join Date: Nov 2009
Pro contributor
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);;//i think one is enough !!!
        }
        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();//this is never executed !!!
        }
    }
    }
    //getch();
    return 0;
}
void student_db()//*** why did you use the limit of 1,2, or 3 students info entered per time?
{
    int no=0;
    int i=1;
    int j=0;
    while(no<1 || no>3){//*** use this instead of calling again the function
        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();//*** this is also big mistake ****
        }
    }//end while
    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);//***INSTEAD OF USING J use i-1,no need for extra variable***
        fflush(stdin);
        gets(database[j].s_ID_number);
       // fflush(stdin);
        --no;// do you know the difference between   i++;  and ++i; ???
        ++i;
        ++j;
    }
    printf("Student Info saved \n");
    printf("--------------------------------------------------------------------------------\n");
    //getch();

    //student_db();
}

void disp_info()
{
    int no;
    for(no=0;no<10;no++)//*** this is also big mistake ****
    {// *** by using 10 (maximum capacity of the array) instead of the real number of students saved in it
    //**** will result showing even the empty ones!!!
    // *** see my previous post where i did it with a global variable.
    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;// *** use this trick in order to know if you found the name you were looking for or not
    printf("enter the students name to edit his database\n");
    //getch();
    fflush(stdin);
    gets(name);

    for(no=0;no<10;no++)//*** this is also big mistake ****
    {
    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");
        found=1;//***congratulations you found the name
        break;//*** when you look for a name consider that you may have more than one hits!!!
        //*** i use break in order to change only the first instance of the name.
    }
    //else //*** this is called 10 times
    //{
        //printf("entered name is not in the student database list\n");
    //}
       }
    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.
}

i think that the comments inside the code are more than enough.
for more questions you can post again.
omprakashcn like this
omprakashcn's Avatar, Join Date: Feb 2010
Light Poster
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...