输入循环功能时崩溃

时间:2017-11-10 17:21:03

标签: c loops for-loop crash

我必须输入学生的数量然后他们的名字然后标记。但是当我输入标记时,程序崩溃了。我不知道为什么当我进入最后一个循环函数时它会崩溃我的程序。也许是因为我嵌套太多了?

    #include <stdio.h>
    #include <string.h>
    #define NAME_LEN 25

    int noOfStud(void);
    void listStudents(int noOfStud, char names[][NAME_LEN]);
    void options(int noOfStud, double marks[]);
    void switchOptions(int noOfStud, double marks[]);
    void courseWork1(int noOfStud, double marks[]);
    void emptyBuffer(void);
int main(void)
{
    int students;
    char names[students][NAME_LEN];
    double marks[students];
    students = noOfStud();

    listStudents(students, names);
    options(students, marks);


    return 0;
}

int noOfStud(void)
{
    int students;
    printf("Number of students: ");
    scanf("%d", &students);
    getchar();
    return students;
}

void listStudents(int noOfStud, char names[][NAME_LEN])
{
    int i;
    for(i=0; i<noOfStud; i++)
    {
        printf("Enter name: ");
        scanf("%[^\n]", names[i]);
        getchar();
    }
}

void options(int noOfStud, double marks[])
{
    printf("1. Enter marks for course work 1\n");
    printf("2. Enter marks for course work 2\n");
    printf("3. Enter makrs for course work 3\n");
    printf("4. Display a particular student's marks\n");
    printf("5. Supervisor mode\n");
    printf("6. Exi program\n");

    switchOptions(noOfStud, marks);
}

void switchOptions(int noOfStud, double marks[])
{
    char options;

    printf("\nPlease enter a number for which choice you want: ");
    scanf("%d", &options);
    emptyBuffer();
    switch(options)
    {
        case 1:
            printf("Thank you for chosing to enter marks for course work 1!\n");
            courseWork1(noOfStud,marks);
            break;
        case 2:
            printf("Thank you for chosing to enter marks for course work 2!\n");
            break;
        case 3:
            printf("Thank you for chosing to enter marks for course work 3!\n");
            break;
        case 4:
            printf("work 4");
            break;
        case 5:
            printf("work 5");
            break;
        case 6:
            printf("work 6");
            break;
        default:
            printf("You didn't enter anything");
    }
}

void courseWork1(int noOfStud, double marks[])
{
    int i;
    for(i=0; i<noOfStud; i++)
    {
        printf("Enter marks: ");
        scanf("%d", &marks[i]);
        getchar();
    }

}

void emptyBuffer(void) /* Empty the keyboard buffer */
{
    while(getchar() != '\n')
    {
        ;
    }

}

一切正常,直到我进入最后一个循环函数,它崩溃了。你们有什么想法吗?谢谢你试图帮助我。

 void courseWork1(int noOfStud, double marks[])
{
    int i;
    for(i=0; i<noOfStud; i++)
    {
        printf("Enter marks: ");
        scanf("%d", &marks[i]);
        getchar();
    }

}

1 个答案:

答案 0 :(得分:0)

您应该使用%lf说明符进行双输入。

scanf("%lf",&marks[i]);

另外scanf("%c",&options);因为选项的类型为char。

使用错误的格式说明符会导致未定义的行为。

在main函数中,您正在使用一些未初始化的变量初始化数组。

你应该按正确的顺序放置它。

int main(void)
{
    int students;

    students = noOfStud();
    char names[students][NAME_LEN];
    double marks[students];
    listStudents(students, names);
    options(students, marks);


    return 0;
}