Thread: Structures Help!!

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    3

    Structures Help!!

    hi maybe someone can please help me basically trying to complete an assignment for college basically the code runs fine until i try to search for a student by their surname(section down near the end) and i cant figure out why maybe someone of you guys might be able to help. Here is the code sorry its a bit long :
    insert
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #define SIZE 4
    
    
    struct Student
    {
        long StudentID;
        char fname[21];
        char sname[21];
        int year;
        char course[51];
        float semester_1_results[6];
        float semester_2_results[6];
        int free;  // 1 means its free, 0 means its not
    };
    
    
    struct Student BENG[SIZE];
    
    
    int menu();   // function prototype
    int linear_search(long); // function prototype
    int linear_search_sname(char*); // function prototype
    void add_student();  // function prototype
    void display_students(); // function prototype
    void display_results(); // function prototype
    void search_for_student_studentID(); // function prototype
    void search_for_student_by_surname(); // function prototype
    void delete_student(); // function prototype
    void initialise_database(); // function prototype
    void Run_statistics_for_individual_student(); // function prototype
    void Run_statistics_for_all_student(); // function prototype
    
    
    
    
    int main()
    {
    
    
        initialise_database();  // call the function to set all free positions to 1
    
    
        for(;;)  // infinite loop
        {
            switch(menu())   // calling function menu within switch
            {
                case 1:
                    add_student();  // calling function add_student
                    break;
                case 2:
                    delete_student();  // calling function delete_student
                    break;
                case 3:
                    display_students(); // calling function display_students
                    break;
                case 4:
                    search_for_student_studentID();  // calling function display_students
                    break;
                case 5:
                    search_for_student_by_surname();  // calling function display_students
                    break;
                case 6:
                    Run_statistics_for_individual_student();  // calling function display_students
                    break;
                case 7:
                    Run_statistics_for_all_student();  // calling function display_students
                    break;
                case 8:
                    display_results();  // calling function display_students
                    break;
                case 9:
                    printf("Quitting Program\n");
                    exit(1);
                default:
                    printf("Invalid option chosen\n\n");
            }
        }  // end of infinite loop
    
    
        return 0;
    }
    
    
    void add_student()
    {
        int freepos = -1;
        int i = 0;
        int j = 0;
        int k = 0;
    
    
        for(i = 0; i < SIZE; i++)
        {
            if(BENG[i].free == 1)
            {
                freepos = i;
                break;
            }
        }
    
    
        if(freepos != -1)
        {
            do
            {
                printf("Enter student ID (8-digits): ");
                scanf("%ld%*c", &BENG[freepos].StudentID); // %ld%*c" used to prevent a new line from remaining in the input buffer
            }
            while(BENG[freepos].StudentID<10000000 || BENG[freepos].StudentID>99999999);
    
    
            printf("Enter firstname: ");
            scanf("%[^\n]%*c", BENG[freepos].fname);
    
    
            printf("Enter surname: ");
            scanf("%[^\n]%*c", BENG[freepos].sname);
    
    
            do
            {
                printf("Enter year of course (1-4): ");
                scanf("%d%*c", &BENG[freepos].year);
            }
            while(BENG[freepos].year < 1 || BENG[freepos].year > 4);
    
    
            printf("Enter course name: ");
            scanf("%[^\n]%*c", BENG[freepos].course);
    
    
            for(j = 0; j < 6; j++)
            {
                printf("Enter result of semester 1  Module %i: ", j+1);
                scanf("%f%*c", &BENG[freepos].semester_1_results[j]);
            }
            for(k = 0; k < 6; k++)
            {
                printf("Enter result of semester 2  Module %i: ", k+1);
                scanf("%f%*c", &BENG[freepos].semester_2_results[k]);
            }
    
    
            BENG[freepos].free = 0;  // mark record as taken
        }
        else
            printf("No position free at present\n");
    }
    
    
    void display_students()
    {
        // output the values just entered
        int i, j;
        for(i = 0; i < SIZE; i++)
        {
            if(BENG[i].free == 0) // only print taken records
            {
                printf("Student ID: %ld:\n", BENG[i].StudentID);
                printf("Firstname: %s\n", BENG[i].fname);
                printf("Surname: %s\n", BENG[i].sname);
                printf("Year: %i\n", BENG[i].year);
                printf("Course: %s\n", BENG[i].course);
                for(j = 0; j < 6; j++)
                {
                    printf("Result semester 1 Module %i: %0.1f\n", j+1, BENG[i].semester_1_results[j]);
                }  // end of for
                for(j = 0; j < 6; j++)
                {
                    printf("Result semester 2 Module %i: %0.1f\n", j+1, BENG[i].semester_2_results[j]);
                }  // end of for
            }  // end of if
        }  // end of for
    }
    void display_results()
    {
        // output the values just entered
        int i, j;
        for(i = 0; i < SIZE; i++)
        {
            if(BENG[i].free == 0) // only print taken records
            {
                for(j = 0; j < 6; j++)
                {
                    printf("Result semester 1 Module %i: %0.1f\n", j+1, BENG[i].semester_1_results[j]);
                }  // end of for
                for(j = 0; j < 6; j++)
                {
                    printf("Result semester 2 Module %i: %0.1f\n", j+1, BENG[i].semester_2_results[j]);
                }  // end of for
            }  // end of if
        }  // end of for
    }
    int menu()
    {
        int choice;
        printf("1. To add a student\n");
        printf("2. To delete a student\n");
        printf("3. To display all students\n");
        printf("4. Find a student using studentID\n");
        printf("5. Find a student by their Surname\n");
        printf("6. Find the student you like to run statistics for\n");
        printf("7. Find statistics for all students\n");
        printf("8. Display results of all students\n");
        printf("9. Exit Program\n");
    
    
        do
        {
            scanf("%i", &choice);
        }
        while(choice < 1 || choice > 9);
    
    
        return choice;
    }
    
    
    void initialise_database()
    {
        int i;
        for(i = 0; i < SIZE; i++) // set all structure variables free to 1
            BENG[i].free = 1;
    }
    
    
    void delete_student()
    {
        long search = 0;
        int position = 0;
    
    
        printf("Enter the number of student to delete\n");
        scanf("%ld%*c",&search);
    
    
        position = linear_search(search);
    
    
        if ( position == -1 )
            printf("%ld is not present in array.\n", search);
        else
            printf("%ld is present at location %i.\n", search, position+1);
        BENG[position].free = 1;
        return;
    }
    
    
    void search_for_student_studentID()
    {
        long search = 0;
        int position = 0;
        int j = 0;
    
    
        printf ("Enter the number to search: ");
        scanf ("%ld%*c",&search);
    
    
        position = linear_search (search);
    
    
        if ( position == -1 )
            printf("%ld is not present in array.\n", search);
        else
            printf("%ld is present at location %i.\n", search, position+1);
        printf("Student ID: %ld:\n", BENG[position].StudentID);
        printf("Firstname: %s\n", BENG[position].fname);
        printf("Surname: %s\n", BENG[position].sname);
        printf("Year: %d\n", BENG[position].year);
        printf("Course: %s\n", BENG[position].course);
    
    
        for(j = 0; j < 6; j++)
        {
            printf("Result semester 1 Module %i: %0.2f\n", j+1, BENG[position].semester_1_results[j]);
        }
    
    
        for(j = 0; j < 6; j++)
        {
            printf("Result semester 2 Module %i: %0.2f\n", j+1, BENG[position].semester_2_results[j]);
        }
    
    
        return;
    }
    
    
    void search_for_student_by_surname (char* sname)
    {
        int position = 0;
        int j = 0;
    
    
        printf("Enter the surname of student to search\n");
        scanf("%[^\n]%*c", sname);
    
    
        position = linear_search_sname(sname);
    
    
        if ( position == 1 )
            printf("%s is not present in array.\n", sname);
        else
            printf("%s is present at location %i.\n", sname, position+1);
        printf("Student ID: %ld:\n", BENG[position].StudentID);
        printf("Firstname: %s\n", BENG[position].fname);
        printf("Surname: %s\n", BENG[position].sname);
        printf("Year: %d\n", BENG[position].year);
        printf("Course: %s\n", BENG[position].course);
        for(j = 0; j < 6; j++)
        {
            printf("Result semester 1 Module %i: %0.1f\n", j+1, BENG[position].semester_1_results[j]);
        }
        for(j = 0; j < 6; j++)
        {
            printf("Result semester 2 Module %i: %0.1f\n", j+1, BENG[position].semester_2_results[j]);
        }
    
    
        return;
    }
    int linear_search(long find)
    {
        int c = 0;
    
    
        for ( c = 0 ; c < SIZE ; c++ )
            if (BENG[c].StudentID == find )
                return c;
    
    
        return -1;
    }
    int linear_search_sname(char *find)
    {
        int c = 0;
    
    
        for ( c = 0 ; c < SIZE ; c++ )
            if (strcmp (BENG[c].sname, find) == 0 )
                return c;
    
    
        return -1;
    }
    void Run_statistics_for_individual_student()
    {
        long search = 0;
        int position = 0;
        int i = 0;
        int k = 0;
        int max = 0;
        int min = 0;
        float sums = 0,maxsums=0;
    
    
        printf("Enter the number to search\n");
        scanf("%ld",&search);
        printf("The statistics for student ID (%ld):\n",search);
    
    
        position = linear_search(search);
    
    
        if ( position == -1 )
            printf("%ld is not present in array.\n", search);
        else
    
    
            for(i = 0; i < 6; i++)
            {
                sums += BENG[position].semester_1_results[i];
            }
        printf("Average grade over 6 subjects for this student in semester 1 is %0.1f marks\n",    sums/6);
    
    
        for(i = 0; i < 6; i++)
        {
            maxsums = BENG[position].semester_1_results[i];
        }
        if(maxsums>max)
        {
            max=maxsums;
        }   // end of if
    
    
        printf("Max grade for this student in semester 1 is %i\n", max);
        {
    
    
            for(k = 0; k < 6; k++)
            {
                if(BENG[position].semester_1_results[i]<min)    
                {
                    min=BENG[position].semester_1_results[i];
                }   // end of if    
            }// end of for
            printf("Min grade for this student in semester 2 is %i\n", min);    
        }
    
    
        for(i = 0; i < 6; i++)
        {
            sums += BENG[position].semester_2_results[i];           
        }
        printf("Average grade over 6 subjects for this student in semester 2 is %0.1f marks\n",  sums/6);
    
    
        for(i = 0; i < 6; i++)
        {
            maxsums = BENG[position].semester_2_results[i];  
        }   
        if(maxsums>max)     
        {
            max=maxsums;
        }   // end of if        
    
    
        printf("Max grade for this student in semester 2 is %i\n", max);    
        {
    
    
            for(k = 0; k < 6; k++)
            {
                if(BENG[position].semester_2_results[i]<min)    
                {
                    min=BENG[position].semester_2_results[i];
                }   // end of if    
            }// end of for
            printf("Min grade for this student in semester 2 is %i\n", min);    
        }
        return ;
    }
    
    
    void Run_statistics_for_all_student()
    {
    
    
        // output the values just entered
        int i,j,k, max,min=100;
        float sums,maxsums=0;
        {
            for(i = 0; i < SIZE; i++)
    
    
                if(BENG[i].free == 0) // only print taken records
                {
                    for(j = 0; j < 6; j++)
                    {
                        for(i = 0; i < SIZE; i++)
                        {
                            sums += BENG[i].semester_1_results[i];              
                        } // end of for
    
    
                    }  // end of for
    
    
                }  // end of if
    
    
            printf("Average grade for all students in semester 1 is %0.1f \n", sums/(6)); 
        }               
        {
            for(k = 0; k < SIZE; k++)
    
    
                if(BENG[i].free == 0) // only print taken records
                {
                    for(j = 0; j < 6; j++)
                    {
                        for(i = 0; i < SIZE; i++)
                        {
                            maxsums = BENG[i].semester_1_results[i];  
                        }   
                        if(maxsums>max)     
                        {
                            max=maxsums;
                        }       
                    }// end of for
                }// end of if
        }
        printf("Max grade for all student in semester 1 is %i\n", max);  
    
    
        {
            for(k = 0; k < SIZE; k++)
    
    
                if(BENG[k].free == 0) // only print taken records
                {
                    for(j = 0; j < 6; j++)
                    {
                        for(i = 0; i < 6; i++)
                        {
                            if(BENG[i].semester_1_results[i]<min)   
                            {
                                min=BENG[i].semester_1_results[i];
                            }       
                        }// end of for
                    }// end of for
                }// end of if
        }
        printf("Min grade for all student in semester 1 is %i\n", min);
    
    
    
    
        // output the values just entered
        int q,w,e, maximum,minimum=100;
        float sum,maxsum=0;
        {
            for(q = 0; q < SIZE; q++)
    
    
                if(BENG[q].free == 0) // only print taken records
                {
                    for(w = 0; w < 6; w++)
                    {
                        for(q = 0; q < SIZE; q++)
                        {
                            sum += BENG[q].semester_2_results[q];              
                        } // end of for
    
    
                    }  // end of for
    
    
                }  // end of if
    
    
            printf("Average grade for all students in semester 2 is %0.1f \n", sum/(6)); 
        }               
        {
            for(e = 0; e < SIZE; e++)
    
    
                if(BENG[q].free == 0) // only print taken records
                {
                    for(w = 0; w < 6; w++)
                    {
                        for(q = 0; q < SIZE; q++)
                        {
                            maxsum = BENG[q].semester_2_results[q];  
                        }   
                        if(maxsum>maximum)     
                        {
                            maximum=maxsum;
                        }       
                    }// end of for
                }// end of if
        }
        printf("Max grade for all student in semester 2 is %i\n", maximum);  
    
    
        {
            for(e = 0; e < SIZE; e++)
    
    
                if(BENG[e].free == 0) // only print taken records
                {
                    for(w = 0; w < 6; w++)
                    {
                        for(w = 0; w < 6; w++)
                        {
                            if(BENG[q].semester_2_results[q]<minimum)   
                            {
                                minimum=BENG[q].semester_2_results[q];
                            }       
                        }// end of for
                    }// end of for
                }// end of if
        }
        printf("minimum grade for all student in semester 2 is %i\n", minimum);
    
    
    
    
        return ;
    }
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    When asking for help, I'd suggest you create a new project, copy your code into it, and start trimming it down until you get the simplest complete (i.e. can be compiled) program that illustrates the problem.

    It would also help to say what input you're giving the program, what output you're getting, and what output you're expecting.

    At a quick glance, I noticed that your function declaration does not match your function definition:

    Code:
    // line 30
    void search_for_student_by_surname(); // function prototype
    
    // line 293
    void search_for_student_by_surname (char* sname)
    Furthermore, you're not passing a character pointer to the function when you call it:

    Code:
    // line 63
    search_for_student_by_surname();  // calling function display_students
    But you're trying to use that (non-existent) variable to store a string read from the input.

    It doesn't appear that you need to pass a character pointer to that function. I'd suggest instead creating a character array locally in your "search_for_student_by_surname()" function. There are also easier ways to read a string from the user.

    FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com
    FAQ > How do I avoid a "dangling" newline when reading single character user input? - Cprogramming.com

    A few more suggestions:

    • I'd suggest explicitly declaring a function parameter list as "void" if the function isn't expecting any arguments
    • Avoid global variables - instead, pass your struct to other functions as needed
    • Though not wrong, I'd suggest not using an infinite loop - a "do-while" loop is a good choice for a menu, and a simple variable can be used to determine whether or not to continue (this variable can be updated when the appropriate command is selected by the user)
    • I would avoid using "free" as a variable name, since this is the name of a function in "stdlib.h"

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    3
    Cheers for the tips Matticus. ive only started learning C a few weeks ago (exams next week) and im still at a pretty basic level. anyway the point of the program is to take inputs from a user on student details and output them based on ID Name Grade etc.. before performing statistics such as average max/min.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You seem to be making good progress. And your coding style looks pretty good, too - neat formatting and consistent indentation are very important for anyone reading your code (including yourself).

    I like the fact you #define a constant for your structure array size. I'd also suggest doing this for other constants in your program (e.g. your character arrays and float arrays) rather than relying on magic numbers.

    There's also a thread here that illustrates how to build up a program methodically, testing as you go. It's a good read for a beginner: A development process

  5. #5
    Registered User
    Join Date
    Dec 2014
    Posts
    3
    cheers Matticus Ill have a read

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tree Structures Best Structures!!! Discuss.
    By MutantJohn in forum General Discussions
    Replies: 20
    Last Post: 09-07-2013, 01:17 AM
  2. Link Lists of Structures within Structures...
    By Zocheyado in forum C Programming
    Replies: 3
    Last Post: 04-17-2012, 04:21 PM
  3. Problems with Nested Structures and Arrays of Structures
    By Ignoramus in forum C Programming
    Replies: 4
    Last Post: 03-02-2010, 01:24 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

Tags for this Thread