Question: When I enter the letter `'q'` as `grade`, it runs infinitely.

```
#include <stdio.h>
#include <stdbool.h>

int main(void) {
    int grade;
    bool flag = true;

    while (flag) {
        puts("-----------------------------"); // comment
        printf("What's your grade out of 10? ");
        scanf(" %d", &grade);

        switch (grade) {
          case 10:
          case 9:
          case 8:
          case 7:
          case 6:
            printf("Pass\n");
            break;
          case 5:
            printf("Fail\n");
            break;
          case 4:
            printf("Fail\n");
            break;
          case 3:
            printf("Fail\n");
            break;
          case 2:
            printf("Fail\n");
            break;
          case 1:
            printf("Fail\n");
            break;
          case 0:
            printf("Fail\n");
            break;
          default:
            printf("Illegal Grade\n");
            flag = false;
            break;
        }
    }
    return 0;
}

```

Answer: 
`scanf(" %d", &grade);` fails when you type something that cannot be parsed as into an integer. `grade` is not modified, so it is uninitialized if the conversion error happens immediately and the behavior is undefined, otherwise you get the same value and behavior as the previous time.

The offending input stays in the input stream, so the same thing happens when the code executes again in the `while` loop, hence the infinite loop.

You want to test if the conversion was successful and discard the input if not:

```
#include <stdio.h>
#include <stdbool.h>

int main(void) {
    int res, c, grade;
    bool flag = true;

    while (flag) {
        puts("-----------------------------"); // comment
        printf("What's your grade out of 10? ");
        res = scanf("%d", &grade);
        if (res == EOF)
            break;
        if (res == 0) {
            printf("Invalid input\n");
            /* discard the offending line of input */
            while ((c = getchar()) != EOF && c != '\n')
                continue;
            /* try again */
            continue;
        }

        switch (grade) {
          case 10:
          case 9:
          case 8:
          case 7:
          case 6:
            printf("Pass\n");
            break;
          case 5:
          case 4:
          case 3:
          case 2:
          case 1:
          case 0:
            printf("Fail\n");
            break;
          default:
            printf("Illegal Grade\n");
            flag = false;
            break;
        }
    }
    return 0;
}

```
```
scanf(" %d",&grade);

```

It scans `int` in string. "q" is not `int`. When you enter "q", value of the variable `grade` left unchanged. You must check returned value of `scanf` to validate number of filled placeholders.

```
if (scanf(" %d",&grade) != 1) {
    printf("Illegal Grade\n");
    exit(1); // or break
}

```

Other parts are ok.