C++ Code Example: advanced switch-case statement

This example checks whether a character entered is a vowel or a consonant. The user is first prompted to enter a letter. The input variable is passed to the checkCharacter() function to check the type of the letter.

#include <iostream>
using namespace std;

void checkCharacter(char input) {
    bool isVowel = false;
    switch (input) {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
        case 'A':
        case 'E':
        case 'I':
        case 'O':
        case 'U':
            isVowel = true;
    }
    if (isVowel) {
        cout << input << " is a vowel" << endl;
    } else {
        if ((input >= 'a' && input <= 'z') || (input >= 'A' && input <= 'Z'))
            cout << input << " is a consonant" << endl;
        else
            cout << "error" << endl;
    }
}

int main() {
    char input;

    cout << "Please enter a character: ";
    cin >> input;

    checkCharacter(input);

    return 0;
}
Output 1
Please enter a character: a
a is a vowel
Output 2
Please enter a character: D
D is a consonant