1

I wrote a small program that checks if the input is a char between a-z and A-Z, I wanted to practice functions so I made a function called int check; which should return a value for the while loop (if the input is wrong it's suppose to ask to ask for another input again)

    #include <stdio.h>
int check(char a, char b);
int main()
{
    char a, b;
    int flag = 1;
    printf("enter two characters\n");
    scanf("%c%c", &a, &b);
    flag = check(a, b);
    if (flag != 0)
    {
        while (flag)
        {
            printf("enter two characters\n");
            scanf("%c%c", &a, &b);
            flag = check(a, b);
        }
    }
    printf("worked");
}
int check(char a, char b)
{
    int counter=0;
    if ((a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z')) counter++;
    if ((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z')) counter++;
    if (counter == 2) return 0;
    else return 1;
}

now when I run it it works fine, but for some reason when I input a wrong number twice the first time it asks me to "enter two characters" and on the second time it just skips the scanf and prints twice " enter two characters" does anybody knows why? thanks!

Alex Kreinis
  • 147
  • 7

0 Answers0