c++ - Need help to determine if the inputted character is a symbol,digit or letter -


our professor asked make program determine if inputted character symbol,digit,or letter. there way turn if-else statement pure switch statement. i've been wondering how.

#include <iostream>  using namespace std; main() {     char a;     cout << "enter single character: " ;     cin >> a;     switch ((a >= 65 && <= 90) || (a >= 97 && <= 122)) //ascii value 65-90 (capital letters), 97-122 (small letters)     {         case 1:             cout << "you entered letter!";             break;         case 0:             if (a >= 48 && <= 57 )    //ascii value 48-57 (num 0-9)             {                 cout << "you entered number!" << endl;             }             else             {                 cout << "you entered symbol!" << endl;             }             break;     }     return 0; } 

may that? may add compile time reflection exercise =)

#include <iostream> enum class ctype {     letter,     digit,     symbol };  ctype gettype(char a) {     if( (a >= 65 && <= 90) || (a >= 97 && <= 122) )         return ctype::letter;     if( >= 48 && <= 57 )         return ctype::digit;     return ctype::symbol; } ctype gettype2(char a) // more explicit  {     if( (a >= 'a' && <= 'z') || (a >= 'a' && <= 'z') )         return ctype::letter;     if( >= '0' && <= '9' )         return ctype::digit;     return ctype::symbol; } #include <cctype> ctype gettype3(char a) // warning: functions uses locale inside {     if( isalpha(a) )         return ctype::letter;     if( isdigit(a) )         return ctype::digit;     return ctype::symbol; }  int main(...) {     char a;     std::cout << "enter single character: " ;     std::cin >> a;     switch(gettype(a)) {         case ctype::letter:             std::cout << "you entered letter!";             break;         case ctype::digit:             std::cout << "you entered number!";             break;         case ctype::symbol:             std::cout << "you entered symbol!";             break;     }      return 0; } 

upd: add other solutions comments


Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

angular - Copying node modules to wwwroot AspNetCore -