Hi i made a program with switch, if i input a character to show me if is vowel or a consonant if is letter and to show me if is odd or even if it's number
i don't understand where is the mistake..i need obligatory to use switch
#include <iostream>
#include <cctype> // ::isalpha, ::tolower, ::isdigit
// http://www.cplusplus.com/reference/cctype/int main()
{
std::cout << "Input a single character: ";
char n;
std::cin >> n;
std::cout << '\n';
// let's check if the input is a letter or a numeric digit
if (::isalpha(n)) // is it a letter?
{
switch (::tolower(n)) // temp convert to lower case
{
case'a': case'e': case'i': case'o': case'u': // is it a vowel?
std::cout << n << " is a vowel.\n"; break;
default: // if not, consonant
std::cout << n << " is a consonant.\n"; break;
}
}
elseif (::isdigit(n)) // is it a numeric digit?
{
switch ((int)(n - '0') % 2) // temp convert to integer
{
case 0: std::cout << n << " is a number and is even.\n"; break;
case 1: std::cout << n << " is a number and is odd.\n"; break;
}
}
else // not a letter or numeric digit
{
std::cout << n << " is symbol\n";
}
}
When the input is a character converting to lower (or upper) case reduces the number of case statements required.
Also, when working with a letter if it isn't a vowel by default it is a consonant.
The is-a-number switch is not really needed since all you are looking for is whether a number is odd or even. It could be rewritten:
27 28 29 30 31 32 33 34
if ((int) (n - '0') % 2) // temp convert to integer
{
std::cout << n << " is a number and is odd.\n";
}
else
{
std::cout << n << " is a number and is even.\n";
}