Thread: Student database

  1. #1
    Registered User
    Join Date
    Feb 2021
    Posts
    17

    Student database

    HELLO!

    I am creating a student database with structs in C and I want to search for students by their names in the structure. I have tried strcmp function to compare the input name with student name in structs but its not working! My delete function is not working either! Can someone PLEASE HELP ME!
    here is my code:

    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    typedef struct personal_data{
        int personal_num;
        char name[20];
        char gender[20];
        char study_prg[20];
        int age;
        char email[20];
    
    
    }personaData;
    
    
    
    
    void add_student(personaData list[50], int x)
    {
        int i;
        for(i = 0; i < x; i++)
        {
            printf("\nEnter data for student %d", i + 1);
            fflush(stdin);
            printf("\nName:");
            gets(list[i].name);
            printf("Gender:");
            gets(list[i].gender);
            printf("Study program:");
            gets(list[i].study_prg);
            printf("Email:");
            gets(list[i].email);
            printf("Personal number:");
            scanf("%d", &list[i].personal_num);
            printf("Age:");
            scanf("%d", &list[i].age);
        }
    }
    
    
    void modify_student(personaData list[50],int x, int pnum)
    {
        int i;
        for(i = 0; i < x; i++)
        {
            if(list[i].personal_num == pnum)
            {
                printf("\nEnter data for student %d", i + 1);
                fflush(stdin);
                printf("\nName:");
                gets(list[i].name);
                printf("Gender:");
                gets(list[i].gender);
                printf("Study program:");
                gets(list[i].study_prg);
                printf("Email:");
                gets(list[i].email);
                printf("Age:");
                scanf("%d", &list[i].age);
                break;
            }
            else
                printf("Student not found!");
        }
    }
    
    
    
    
    void search_by_personalNum(personaData list[50], int x)
    {
        int pnum;
        printf("Enter a personal number:");
        scanf("%d", &pnum);
        int i;
        for(i = 0; i < x; i++)
        {
            if(list[i].personal_num == pnum)
            {
                printf("\nName: %s", list[i].name);
                printf("\nGender: %s", list[i].gender);
                printf("\nStudy program: %s", list[i].study_prg);
                printf("\nEmail: %s", list[i].email);
                printf("\nAge: %d", list[i].age);
                break;
            }
            else
                printf("Student not found!");
        }
    }
    
    
    void search_by_Name(personaData list[50], int x)
    {
        char temp_name[20];
        printf("\nEnter name: ");
        fflush(stdin);
        fgets(temp_name, sizeof(temp_name), stdin);
    
    
        int i;
        int isFound = 0;
        for(i = 0; i < x; i++)
        {
    //        if(list[i].name == temp_name)
    //        {
    //            isFound = 1;
    //        }
    
    
            if(strcmp(list[i].name, temp_name) == 0)
            {
                isFound = 1;
                break;
            }
        }
        if(isFound == 1)
        {
            printf("\nPersonal number: %d", list[i].personal_num);
            printf("\nGender: %s", list[i].gender);
            printf("\nStudy program: %s", list[i].study_prg);
            printf("\nEmail: %s", list[i].email);
            printf("\nAge: %d", list[i].age);
        }
        else
                printf("Student not found!");
    }
    
    
    
    
    
    
    void Delete(personaData list[50], int x)
    {
        int pnum;
        printf("Enter a personal number:");
        scanf("%d", pnum);
        char emptyStr[20] = {"\0"};
        int emptyStr1[20] = {0};
        int i;
        for(i = 0; i < x; i++)
        {
            if(list[i].personal_num == pnum)
            {
                strcpy(emptyStr, list[i].name);
                strcpy(emptyStr, list[i].gender);
                strcpy(emptyStr, list[i].email);
                strcpy(emptyStr, list[i].study_prg);
                strcpy(emptyStr1, list[i].age);
                strcpy( emptyStr1, list[i].personal_num);
            }
            printf("Not found");
    
    
        }
    }
    
    
    int main()
    {
        personaData students[20];
        int n;
        char Name[20];
    
    
        printf("Number of records you want to enter? : ");
        scanf("%d", &n);
        add_student(students, n);
        Delete(students, n);
        search_by_Name(students, n);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,977
    IIRC, fgets leaves the newline on the end.

    Yep, this link agrees with my memory, fgets(3): input of char/strings - Linux man page

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    3,977
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,535
    Regarding delete
    Code:
    void Delete(personaData list[50], int x)
    {
        int pnum;
        printf("Enter a personal number:");
        scanf("%d", pnum);
        char emptyStr[20] = {"\0"};
        int emptyStr1[20] = {0};
        int i;
        for(i = 0; i < x; i++)
        {
            if(list[i].personal_num == pnum)
            {
                strcpy(emptyStr, list[i].name);
                strcpy(emptyStr, list[i].gender);
                strcpy(emptyStr, list[i].email);
                strcpy(emptyStr, list[i].study_prg);
                strcpy(emptyStr1, list[i].age);
                strcpy( emptyStr1, list[i].personal_num);
            }
            printf("Not found");
     
     
        }
    }
    1. You forgot the & when reading pnum
    2. All your strcpy's are the wrong way round.
    3. There's no such thing as strcpy to an integer.
    4. You should only print "Not found" when you've searched the entire array and found nothing.

    Code:
    void Delete(personaData list[50], int x)
    {
        personaData empty = { 0 };
        int pnum;
        printf("Enter a personal number:");
        scanf("%d", &pnum);
        int i;
        for(i = 0; i < x; i++)
        {
            if(list[i].personal_num == pnum)
            {
                list[i] = empty;
                return;
            }
        }
        printf("Not found");
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2021
    Posts
    17
    It worked!!! Your idea was way better than mine, to put the whole list into an empty list istead of putting every member of it into an empty list!
    Next i want to find sum of all male and female students and it is returning strange integer. I also wanna add up ages of students to find average age of students but it is not going well. Could you take a look at it. I think the problem is with sum_age.

    insert
    Code:
    void num_of_male_female(personaData list[50], int x)
    {
        int i, m, f;
        char male[20] = "male";
        char female[20] = "female";
        for(i = 0; i < x; i++)
        {
            if(list[i].gender == male)
            {
                m = m + 1;
            }
            else if(list[i].gender == female)
            {
                f = f + 1;
            }
    
    
        }
        printf("Number of male students: %d ", m);
        printf("Number of female students: %d", f);
    }
    
    
    void Average_age(personaData list[50], int x)
    {
        int i;
        int sum_age;
        int average_age;
        for(i = 0; i < x; i++)
        {
            sum_age = sum + list[i].age;
    
    
        }
        average_age = sum_age/x;
        printf("Average age of students %d \n", average_age);
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,535
    > if(list[i].gender == male)
    Use strcmp() to compare strings.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Feb 2021
    Posts
    17
    ok, did you see any problem with sum_age? it is not adding up ages of all students properly

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,535
    > int sum_age;
    What is the initial value of sum_age?

    > sum_age = sum + list[i].age;
    What is sum here?
    Where did you declare it?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Feb 2021
    Posts
    17
    Sum doesn't have an initial value, it is given a value in the for loop where I add up ages of all students. The problem here is that list[i].age gets only age of the first student and doesn't continue to next student. But if I write list[i + 1].age it goes to next students age. So should I do another for loop for this part or do you have any suggestion?

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,535
    > sum_age = sum + list[i].age;
    Where is this declared in your code?
    At best, it's some messy global variable.
    At worst, it's undeclared and your code doesn't even compile.

    Code:
    void Average_age(personaData list[50], int x)
    {
        int i;
        int sum_age = 0; //!! initialise your variables
        int average_age = 0;
        for(i = 0; i < x; i++)
        {
            //!! compare with the m = m + 1; you did in the previous function
            sum_age = sum_age + list[i].age;
        }
        average_age = sum_age/x;
        printf("Average age of students %d \n", average_age);
    }
    To save typing, you can also do this
    Code:
    sum_age += list[i].age;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Feb 2021
    Posts
    17
    Yeah the variable declaration was the problem. It worked after I just set it to zero, int sum_age = 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Student Grade Database with C
    By manOtto in forum C Programming
    Replies: 1
    Last Post: 12-25-2016, 10:16 AM
  2. Student Grade Database with C
    By manOtto in forum C++ Programming
    Replies: 2
    Last Post: 12-25-2016, 09:01 AM
  3. how to create student database in c programming.???
    By nadiamirzaaaaa in forum C Programming
    Replies: 19
    Last Post: 05-27-2014, 03:31 PM
  4. student database
    By ak47 in forum C Programming
    Replies: 10
    Last Post: 11-10-2012, 10:58 PM
  5. A Student's Mark Database
    By playstationman1 in forum C++ Programming
    Replies: 1
    Last Post: 03-01-2006, 01:26 PM

Tags for this Thread