I've been working on this exercise in the book "C: How to Program (Global 9th Edition)" where you create a program that accepts a pass and fail grade and then tells you how many passed/failed at the end along with "giving you a bonus" if you have 9 or more passes as the teacher. It's the same exercise that I posted in my last thread about unassigned integers:
Code:
#include <stdio.h>
int main ()
{
    int passes = 0;
    int failures = 0;
    int student = 1;

    while (student <= 10) 
    {
        printf ("Enter 1 for pass, 2 for fail: ");

        int result;

        scanf ("%d", &result);

        while (result != 1 && result != 2)
        {
            printf ("Oops! Enter 1 or 2 or exit with ctrl + c: ");

            scanf ("%d", &result);
        }

        if (result == 1)
        {
            ++passes;
        }
        else 
        {
            ++failures;
        }
        
        ++student;
    }
    
    printf ("%d students passed,", passes);
    printf (" %d students failed.\n", failures);

    if (passes > 8)
    {    
        puts("Congratulations! Instructor gets a bonus!");
    }

    return 0;
}
I added a mechanism for checking whether the user entered just 1 or 2 like they are supposed to, and tell them how to just exit the program. It works fine, HOWEVER, if you enter a character instead of an integer, the program spits out my error message in an infinite loop.

Is there a way to repeat the error loop the way it's supposed to be repeated if someone were to enter a letter/char instead of a number? I'd think that in a real world scenario, this would be a pretty grave security vulnerability...that's interesting, but it would also be interesting to test for the wrong data type in the input and output the same message that the user would get with a 0 or 3.