This program is designed to take a single character as input from the user and determine if it is a vowel or consonant.
#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;
}
Please enter a character: a
a is a vowel
Please enter a character: D
D is a consonant
checkCharacter
function is defined with a single parameter input
, which is a char
data type.bool
variable isVowel
is declared and initialized to false
.switch
statement is used to determine if the input
character is a vowel (upper- or lowercase ‘a’, ‘e’, ‘i’, ‘o’, ‘u’). If any of these cases are matched, the isVowel
variable is set to true
.switch
statement, an if
statement is used to determine if the isVowel
variable is true
. If it is, the program outputs the message input
is a vowel. If not, a nested if
statement checks if the input is a letter between a
and z
or A
and Z
. If the input is a letter, the program outputs the message input
is a consonant. If not, the program outputs the message error
.main
function, the user is prompted to enter a character. The entered character is stored in the input
variable.checkCharacter
function is called with input
as an argument.