0

This code is not making it through the second dowhile loop. It seems to be cycling through the default statement and looping back to the option loop. I am not sure what the issue is, but I have rewritten it at least a dozen times, and still I am having trouble with it. What am I leaving out? I'm positive it's something simple that I am just overlooking. Is there an easy way to identify these types of issues?

#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);

}
dbush
  • 205,898
  • 23
  • 218
  • 273
Andromeda
  • 17
  • 2
  • 1
    Your code is huge and doesn't include any comments. For more users to be willing to answer your question, please edit your question so as to indicate what you're trying to do with your program, and perhaps also add a few more relevant tags. – Nino Filiu Feb 26 '19 at 19:31
  • 6
    It's `while(1)` without any `break` related to it. Why would it ever stop? – Eugene Sh. Feb 26 '19 at 19:31
  • You never check the return value from `scanf()` to see if it converts any input. That will infinite loop if someone answers `Q` to `How many students are in the class?` – Andrew Henle Feb 26 '19 at 19:33
  • Wha'ts `class[1].[j]` supposed to be? – Fred Larson Feb 26 '19 at 19:38
  • 5
    Please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). Change `scanf("%c\n", &option);` to `scanf(" %c", &option);` note the added space and removal of newline – Weather Vane Feb 26 '19 at 19:38
  • For those noting the while(1), case 'z' does a return so that will definitely exit the loop. – Jim Rhodes Feb 26 '19 at 19:40
  • @JimRhodes Yeah, well spotted. – Eugene Sh. Feb 26 '19 at 19:44
  • @WeatherVane Thank you. I removed the new line. That cleared it up. – Andromeda Feb 26 '19 at 19:46
  • 2
    Better add the space too: it filters out the leading whitespace which other format specifiers such as `%d` and `%f` and `%s` do automatically, but not `%c` and `%[]` and `%n`. – Weather Vane Feb 26 '19 at 19:48
  • 1
    Why are you using a Do While loop infinitely? Just use a While loop infinitely, less typing. And I don't need to scroll to the bottom for the loop to see that there's nothing tested. – Deanie Feb 26 '19 at 20:29
  • OT: regarding: `switch (option)` By changing this to `switch ( toupper(option) )` then each lower case 'case' statement can be removed. Note: `toupper()` is found in the header file: `ctype.h` – user3629249 Feb 26 '19 at 23:38
  • OT: regarding this kind of statement: `average = 0;` The variable `average` is a `float`, so any assignments should be `float` I.E. change to: `average = 0.0f;` Then instead of trying force an `int` into a `float` the code would be assigning a `float` from a `float` – user3629249 Feb 26 '19 at 23:41

0 Answers0