卡在一个do while循环中,我无法下车

时间:2019-02-26 19:29:34

标签: c

此代码无法通过第二个dowhile循环实现。它似乎在遍历默认语句并循环回到选项循环。我不确定是什么问题,但是我至少重写了十二次,但仍然遇到问题。我遗漏了什么?我很肯定这只是我所忽略的简单事情。有没有一种简单的方法来识别这些类型的问题?

#include <stdio.h>

int main(void)
{
    int j = 0;
    int i = 0;
    int classSize = 0;
    int position = 0;
    int studentID = 0;
    float average = 0;
    char option = ' ';


    do
    {
        printf("How many students are in the class?\n ");
        scanf("%d", &classSize);
    }while (classSize == 0);

    float class[4][classSize];
    float final[classSize];


    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < classSize; j++)
            class[i][j] = 0;
    }

    for(j = 0; j < classSize; j++)
        final[j] = 0;
    do
    {

        printf ("Please select an option:\n");
        printf ("'A'to add student info one student at a time \n");
        printf ("'D' to display student’s info \n");
        printf ("'T' to display class average for homework\n");
        printf ("'S' to display class average for quizzes\n");
        printf ("'B' to display class average for final exams\n");
        printf ("'W' to display class average for student’s final 
                 grade\n");
        printf ("'Z' to exit program\n");
        scanf("%c\n", &option);

        switch (option)
        {
            case 'a': case 'A':
                if (position < classSize)
                {

                    printf ("Please enter student's number:\n");
                    scanf("%f", &class[0][position] );

                    printf ("Please enter student's homework grade:\n");
                    scanf("%f", &class[1][position] );

                    printf ("Please enter student's quiz grade:\n");
                    scanf("%f", &class[2][position] );

                    printf ("Please enter student's final exam grade:\n");
                    scanf("%f", &class[3][position] );

                    position++;

                }
                else
                    puts("The table is full.\n");

                break;

            case 'd': case 'D':

                printf("Enter student number:\n");
                scanf("%d", &studentID);

                for(j = 0; j < classSize; j++)
                {
                    if (class[0][j] == studentID)
                    {
                        printf ("The student's number is %d. \n", studentID);
                        printf ("The student's homework grade is %.2f.\n", class[1][j]);
                        printf ("The Student's quiz score is %.2f. \n", class[2][j]);
                        printf ("The student's final exam score is %.2f.\n", class[3][j]);
                        printf ("The student's total grade is %.2f. \n",
                            ((class[1].[j]*.5)+(class[2][j]*.1)+(class[3][j]*.4)));

                        break;
                    }
                    else
                        continue;
                    }

                    if (class[0][j] == studentID)
                    {
                        for(j = 0; j < classSize; j++)
                            break;
                    }
                    else
                    {
                        printf("No match\n");
                        break;
                    }
                    break;

            case 't': case 'T':

                average = 0;
                for(j = 0; j < classSize; j++)
                    average = average + class[1][j];

                    average = average / classSize;
                printf("The average homework grade is %.2f\n", average);

                break;

            case 's': case 'S':

                average = 0;

                for(j = 0; j < classSize; j++)
                    average = average + class[2][j];
                    average = average / classSize;
                printf ("The average quiz grade is %.2f\n", average);

                break;

            case 'b': case 'B':

                average = 0;

                for(j = 0; j < classSize; j++)
                    average = average + class[3][j];
                average = average / classSize;
                printf("The average class final grade is %.2f\n", average);
                break;

            case 'w': case 'W':

                average = 0;

                for( j = 0; j < classSize; j++)
                    final[j] = ((class[1][j] * .5) + (class[2][j] * .1) +
                                (class[3][j] * .4));

                for(j = 0; j < classSize; j++)
                    average = average + final[j];
                average = average / classSize;

                printf("The class average is %.2f\n", average);

                break;

            case 'z': case 'Z':

                return 0;

                break;


            default:
            printf("Error...try again\n");
        }
    }
    while(1);

}

0 个答案:

没有答案