-1

I am a newbie to C programming. I'm practicing to read the user input through an infinite while loop, and check if the input is a vowel or constant. I use a switch to check it, however when I execute my code, the default case is always executed, and I got stucked.

Would be appreciate if anyone can give me some advice, thanks!

#include <stdio.h>

void checkIfVowel(char *c);

int main()
{
    char c;
    while (1)
    {
        printf("Please enter a character: ");
        scanf("%c", &c);
        checkIfVowel(&c);
    }
    return 0;
}

void checkIfVowel(char *c)
{
    switch (*c)
    {

    case 'A':
        printf("%c is a vowel! \n", *c);
        break;
    case 'E':
        printf("%c is a vowel! \n", *c);
        break;
    case 'I':
        printf("%c is a vowel! \n", *c);
        break;
    case 'O':
        printf("%c is a vowel! \n", *c);
        break;
    case 'U':
        printf("%c is a vowel! \n", *c);
        break;
    case 'a':
        printf("%c is a vowel! \n", *c);
        break;
    case 'e':
        printf("%c is a vowel! \n", *c);
        break;
    case 'i':
        printf("%c is a vowel! \n", *c);
        break;
    case 'o':
        printf("%c is a vowel! \n", *c);
        break;
    case 'u':
        printf("%c is a vowel! \n", *c);
        break;
    default:
        printf("%c is a constant! \n", *c);
        break;
    }
}
Currycatlover
  • 99
  • 1
  • 1
  • 7

1 Answers1

0

Simple change is enough to remove errors. But you can simply do this even with if- else statement with logical conditions for all the vowel within 1 line of code rather than this switch cases.! just try it! I'm attaching error removed code only

#include <stdio.h>

void checkIfVowel(char *c);

int main()
{
    char c;
    while (1)
    {
        printf("Please enter a character: ");
        scanf("%c", &c);
        checkIfVowel(c);
    }
    return 0;
}

void checkIfVowel(char* c)
{
    switch ((char) c)
    {

    case 'A':
        printf("%c is a vowel! \n", c);
        break;
    }
    
}

 
RusJaI
  • 666
  • 1
  • 7
  • 28