请帮助真正坚持结构

时间:2014-12-05 21:17:24

标签: c

<罢工>嗨,大家一直在使用这个网站一段时间用于提示但从未发布过(我认为的第一次)无论如何做大学的任务(制造学位),所以可能是你们中的一些人的基础。基本上

问题是在学生名称的搜索中,程序运行的底部附近是空的,但是一旦我输入名称就会崩溃任何帮助都会非常感激

#include <stdlib.h>
#include <stdio.h>
#define SIZE 2

struct Student
{
    long StudentID;//works
    char fname[21];//works
    char sname[21];//does not work
    int year;//works
    char course[51];//works
    float results_semester_1[6];//works
    float results_semester_2[6];//works
    int free;  // 1 means its free, 0 means its not
};

struct Student BENG[SIZE];

int menu();   // 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_name(); // 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 linear_search(long); // function prototype
int linear_search_sname(char); // 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_name();  // 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, i, j,k,year;

    for(i = 0; i < SIZE; i++)
    {
        if(BENG[i].free == 1)
        {
            freepos = i;
            break;
        }
    }

    if(freepos != -1)
    {
        do
        {
            printf("Enter student ID:\n");
            scanf("%ld", &BENG[freepos].StudentID);
        }
        while(BENG[freepos].StudentID<10000000 ||BENG[freepos].StudentID>99999999);

        printf("Enter firstname:\n");
        scanf("%s", BENG[freepos].fname);

        printf("Enter surname:\n");
        scanf("%s", BENG[freepos].sname);

        do
        {
            printf("Enter year of course:\n");
            scanf("%d", &BENG[freepos].year);
        }
        while(BENG[freepos].year < 1 || BENG[freepos].year > 4);

        printf("Enter course name:\n");
        scanf("%s", BENG[freepos].course);

        for(j = 0; j < 6; j++)
        {
            printf("Enter result of semester 1  Module %d:\n", j+1);
            scanf("%f", &BENG[freepos].results_semester_1[j]);
        }
        for(k = 0; k < 6; k++)
        {
            printf("Enter result of semester 2  Module %d:\n", k+1);
            scanf("%f", &BENG[freepos].results_semester_2[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: %d\n", BENG[i].year);
            printf("Course: %s\n", BENG[i].course);
            for(j = 0; j < 6; j++)
            {
                printf("Result semester 1 Module %d: %0.2f\n", j+1, BENG[i].results_semester_1[j]);
            }  // end of for
            for(j = 0; j < 6; j++)
            {
                printf("Result semester 2 Module %d: %0.2f\n", j+1, BENG[i].results_semester_2[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 %d: %0.2f\n", j+1, BENG[i].results_semester_1[j]);
            }  // end of for
            for(j = 0; j < 6; j++)
            {
                printf("Result semester 2 Module %d: %0.2f\n", j+1, BENG[i].results_semester_2[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 student Surname\n");
    printf("6. Find student would 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("%d", &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;
    int position,freepos;

    printf("Enter the number of student to delete\n");
    scanf("%ld",&search);

    position = linear_search(search);

    if ( position == -1 )
        printf("%d is not present in array.\n", search);
    else
        printf("%d is present at location %d.\n", search, position+1);
    BENG[position].free = 1;
    return;
}

void search_for_student_studentID()
{
    long search;
    int position,j,i;

    printf("Enter the number to search\n");
    scanf("%ld",&search);

    position = linear_search(search);

    if ( position == -1 )
        printf("%d is not present in array.\n", search);
    else
        printf("%d is present at location %d.\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 %d: %0.2f\n", j+1,         BENG[i].results_semester_1[j]);
    }
    for(j = 0; j < 6; j++)
    {
        printf("Result semester 2 Module %d: %0.2f\n", j+1, BENG[i].results_semester_2[j]);
    }
    return;
}

void search_for_student_by_name(sname)
{
    int position,j,i;

    printf("Enter the surname of student to search\n");
    scanf("%s",&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 %d.\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 %d: %0.2f\n", j+1, BENG[i].results_semester_1[j]);
    }
    for(j = 0; j < 6; j++)
    {
        printf("Result semester 2 Module %d: %0.2f\n", j+1, BENG[i].results_semester_2[j]);
    }

    return;
}
int linear_search(long find)
{
    int c;

    for ( c = 0 ; c < SIZE ; c++ )
    {
        if (BENG[c].StudentID == find )
        {
            return c;
            break;
        }
    }
    return -1;
}
int linear_search_sname(char find)
{
    int c;

    for ( c = 0 ; c < SIZE ; c++ )
    {
        if (BENG[c].sname[21] == find )
        {
            return c;
            break;
        }
    }
    return -1;
}
void Run_statistics_for_individual_student()
{
    long search;
    int position,j,i,k,r,max,minsums,min=100;
    float sums = 0,maxsums=0;

    printf("Enter the number to search\n");
    scanf("%ld",&search);
    printf("The statistics for student ID:\n",search);

    position = linear_search(search);

    if ( position == -1 )
        printf("%d is not present in array.\n", search);
    else

        for(i = 0; i < 6; i++)
        {
            sums += BENG[position].results_semester_1[i];
        }
    printf("Average grade over 6 subjects for this student in semester 1 is %0.2f marks\n",    sums/6);

    for(i = 0; i < 6; i++)
    {
        maxsums = BENG[position].results_semester_1[i];
    }
    if(maxsums>max)
    {
        max=maxsums;
    }   // end of if

    printf("Max grade for this student in semester 1 is %d\n", max);
    {

        for(k = 0; k < 6; k++)
        {
            if(BENG[position].results_semester_1[k]<min)    
            {
                min=BENG[position].results_semester_1[k];
            }   // end of if    
        }// end of for
        printf("Min grade for this student in semester 2 is %d\n", min);    
    }

    for(i = 0; i < 6; i++)
    {
        sums += BENG[position].results_semester_2[i];           
    }
    printf("Average grade over 6 subjects for this student in semester 2 is %0.2f marks\n",  sums/6);

    for(i = 0; i < 6; i++)
    {
        maxsums = BENG[position].results_semester_2[i];  
    }   
    if(maxsums>max)     
    {
        max=maxsums;
    }   // end of if        

    printf("Max grade for this student in semester 2 is %d\n", max);    
    {

        for(k = 0; k < 6; k++)
        {
            if(BENG[position].results_semester_2[k]<min)    
            {
                min=BENG[position].results_semester_2[k];
            }   // end of if    
        }// end of for
        printf("Min grade for this student in semester 2 is %d\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].results_semester_1[i];              
                    } // end of for

                }  // end of for

            }  // end of if

        printf("Average grade for all students in semester 1 is %0.2f \n", sums/(SIZE*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].results_semester_1[i];  
                    }   
                    if(maxsums>max)     
                    {
                        max=maxsums;
                    }       
                }// end of for
            }// end of if
    }
    printf("Max grade for all student in semester 1 is%d\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].results_semester_1[i]<min)   
                        {
                            min=BENG[i].results_semester_1[i];
                        }       
                    }// end of for
                }// end of for
            }// end of if
    }
    printf("Min grade for all student in semester 1 is %d\n", min);


    return ;
}

2 个答案:

答案 0 :(得分:1)

整个代码中存在大量错误。所有这些都是由于没有放松和关注每一行(以及每行的一部分)。 C是完全语言。这是它的优势之一。语法或逻辑中没有足够接近这样的东西。找到大多数 ...疏忽...... 的一种方法是在警告开启的情况下进行编译。程序的编译字符串至少应包含-Wall -Wextra。例如:

gcc -Wall -Wextra -o student_database student_database.c

这将捕获大部分基本语法和变量类型不匹配问题。消除每个警告。编译器没有意外抛出它们。他们的意思是。

话虽如此,我已经删除了代码中的警告。我已经通过add_student函数,它接受输入并阻止newlines在下一个条目之前保留在输入缓冲区中。我已经纠正了其他方面,只是为了解决编译器警告。您的代码仍需要大量工作。这将给你一个良好的开端,消除那种不堪重负的感觉(暂时)。完成您的代码,查看我所做的更正。慢下来 - 你会做得很好。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define SIZE 4

struct Student
{
    long StudentID;//works
    char fname[21];//works
    char sname[21];//does not work
    int year;//works
    char course[51];//works
    float results_semester_1[6];//works
    float results_semester_2[6];//works
    int free;  // 1 means its free, 0 means its not
};

struct Student BENG[SIZE];

int menu();   // 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_name(); // 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 linear_search(long); // function prototype
int linear_search_sname(char*); // 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_name();  // 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);
        }
        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 %d: ", j+1);
            scanf("%f%*c", &BENG[freepos].results_semester_1[j]);
        }
        for(k = 0; k < 6; k++)
        {
            printf("Enter result of semester 2  Module %d: ", k+1);
            scanf("%f%*c", &BENG[freepos].results_semester_2[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: %d\n", BENG[i].year);
            printf("Course: %s\n", BENG[i].course);
            for(j = 0; j < 6; j++)
            {
                printf("Result semester 1 Module %d: %0.2f\n", j+1, BENG[i].results_semester_1[j]);
            }  // end of for
            for(j = 0; j < 6; j++)
            {
                printf("Result semester 2 Module %d: %0.2f\n", j+1, BENG[i].results_semester_2[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 %d: %0.2f\n", j+1, BENG[i].results_semester_1[j]);
            }  // end of for
            for(j = 0; j < 6; j++)
            {
                printf("Result semester 2 Module %d: %0.2f\n", j+1, BENG[i].results_semester_2[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 student Surname\n");
    printf("6. Find student would 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("%d", &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 %d.\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 %d.\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 %d: %0.2f\n", j+1,         BENG[position].results_semester_1[j]);
    }

    for(j = 0; j < 6; j++)
    {
        printf("Result semester 2 Module %d: %0.2f\n", j+1, BENG[position].results_semester_2[j]);
    }

    return;
}

void search_for_student_by_name (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 %d.\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 %d: %0.2f\n", j+1, BENG[position].results_semester_1[j]);
    }
    for(j = 0; j < 6; j++)
    {
        printf("Result semester 2 Module %d: %0.2f\n", j+1, BENG[position].results_semester_2[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 = 100;
    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].results_semester_1[i];
        }
    printf("Average grade over 6 subjects for this student in semester 1 is %0.2f marks\n",    sums/6);

    for(i = 0; i < 6; i++)
    {
        maxsums = BENG[position].results_semester_1[i];
    }
    if(maxsums>max)
    {
        max=maxsums;
    }   // end of if

    printf("Max grade for this student in semester 1 is %d\n", max);
    {

        for(k = 0; k < 6; k++)
        {
            if(BENG[position].results_semester_1[k]<min)    
            {
                min=BENG[position].results_semester_1[k];
            }   // end of if    
        }// end of for
        printf("Min grade for this student in semester 2 is %d\n", min);    
    }

    for(i = 0; i < 6; i++)
    {
        sums += BENG[position].results_semester_2[i];           
    }
    printf("Average grade over 6 subjects for this student in semester 2 is %0.2f marks\n",  sums/6);

    for(i = 0; i < 6; i++)
    {
        maxsums = BENG[position].results_semester_2[i];  
    }   
    if(maxsums>max)     
    {
        max=maxsums;
    }   // end of if        

    printf("Max grade for this student in semester 2 is %d\n", max);    
    {

        for(k = 0; k < 6; k++)
        {
            if(BENG[position].results_semester_2[k]<min)    
            {
                min=BENG[position].results_semester_2[k];
            }   // end of if    
        }// end of for
        printf("Min grade for this student in semester 2 is %d\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].results_semester_1[i];              
                    } // end of for

                }  // end of for

            }  // end of if

        printf("Average grade for all students in semester 1 is %0.2f \n", sums/(SIZE*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].results_semester_1[i];  
                    }   
                    if(maxsums>max)     
                    {
                        max=maxsums;
                    }       
                }// end of for
            }// end of if
    }
    printf("Max grade for all student in semester 1 is%d\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].results_semester_1[i]<min)   
                        {
                            min=BENG[i].results_semester_1[i];
                        }       
                    }// end of for
                }// end of for
            }// end of if
    }
    printf("Min grade for all student in semester 1 is %d\n", min);


    return ;
}

使用/输出:

./bin/student_database
1. To add a student
2. To delete a student
3. To display all students
4. Find a student using studentID
5. Find a student by student Surname
6. Find student would you like to run statistics for
7. Find statistics for all students
8. Display results of all students
9. Exit Program
1
Enter student ID (8-digits): 12345678
Enter firstname: Johnny
Enter surname: Walker
Enter year of course (1-4): 1
Enter course name: UnderwaterMusic
Enter result of semester 1  Module 1: 88.8
Enter result of semester 1  Module 2: 89.8
Enter result of semester 1  Module 3: 90.8
Enter result of semester 1  Module 4: 91.0
Enter result of semester 1  Module 5: 68.7
Enter result of semester 1  Module 6: 66.7
Enter result of semester 2  Module 1: 92.4
Enter result of semester 2  Module 2: 88.4
Enter result of semester 2  Module 3: 82.3
Enter result of semester 2  Module 4: 95.3
Enter result of semester 2  Module 5: 91.2
Enter result of semester 2  Module 6: 88.5
1. To add a student
2. To delete a student
3. To display all students
4. Find a student using studentID
5. Find a student by student Surname
6. Find student would you like to run statistics for
7. Find statistics for all students
8. Display results of all students
9. Exit Program
3
Student ID: 12345678:
Firstname: Johnny
Surname: Walker
Year: 1
Course: UnderwaterMusic
Result semester 1 Module 1: 88.80
Result semester 1 Module 2: 89.80
Result semester 1 Module 3: 90.80
Result semester 1 Module 4: 91.00
Result semester 1 Module 5: 68.70
Result semester 1 Module 6: 66.70
Result semester 2 Module 1: 92.40
Result semester 2 Module 2: 88.40
Result semester 2 Module 3: 82.30
Result semester 2 Module 4: 95.30
Result semester 2 Module 5: 91.20
Result semester 2 Module 6: 88.50
1. To add a student
2. To delete a student
3. To display all students
4. Find a student using studentID
5. Find a student by student Surname
6. Find student would you like to run statistics for
7. Find statistics for all students
8. Display results of all students
9. Exit Program
9
Quitting Program

其他建议

我给你的一个建议是不要在函数中静态声明字符串大小,只需在add_student函数中根据需要进行分配。例如:

struct Student
{
    long StudentID;//works
    char *fname;//works
    char *sname;//does not work
    int year;//works
    char *course;//works
    float results_semester_1[6];//works
    float results_semester_2[6];//works
    int free;  // 1 means its free, 0 means its not
};

然后你可以简单地允许scanf根据需要动态分配内存,例如:

scanf("%m[^\n]%*c", &BENG[freepos].fname);

此外,您将看到我必须更改scanf中的所有add_student格式字符串,以防止newline保留在输入缓冲区中(按[enter]的结果)导致您的程序在下一个输入上跳过。控制输入​​缓冲区的状态至关重要。有很多方法可以做到这一点,但仔细看看man scanf是一个很好的起点。

答案 1 :(得分:0)

这里有一个错误

void search_for_student_by_name(sname)//只写sname的类型