0

Our professor asked us to make a program to determine if the inputted character is a symbol,digit,or letter. Is there any way to turn the if-else statement into a PURE switch statement. I've been wondering how.

#include <iostream>

using namespace std;
main()
{
    char a;
    cout << "Enter a single character: " ;
    cin >> a;
    switch ((a >= 65 && a <= 90) || (a >= 97 && a <= 122)) //ASCII Value 65-90 (capital letters), 97-122 (small letters)
    {
        case 1:
            cout << "You entered a letter!";
            break;
        case 0:
            if (a >= 48 && a <= 57 )    //ASCII Value 48-57 (num 0-9)
            {
                cout << "You entered a number!" << endl;
            }
            else
            {
                cout << "You entered a symbol!" << endl;
            }
            break;
    }
    return 0;
}
muXXmit2X
  • 2,745
  • 3
  • 17
  • 34
  • Do you want to eliminate all the if-else statements in your code and use only switch case statements? I found this question https://stackoverflow.com/questions/9432226/how-do-i-select-a-range-of-values-in-a-switch-statement some what related. – Ravi Chandra Jul 26 '17 at 09:56
  • 1
    Why would you use a switch statement at all? A sequence of if-else blocks is way more readable. – muXXmit2X Jul 26 '17 at 09:57
  • Having the cases of a switch being 1 and 0 for a boolean expression over some numbers? I hope that this was not given to you by your professor. A switch is for a small number of discrete values. Use several ifs. Since you can name this (print_character_type, for eample), this should be a function. Also, you should use indentation. – Aziuth Jul 26 '17 at 10:23

4 Answers4

1

May be like that? You may add compile time reflection as exercise =)

#include <iostream>
enum class CType
{
    Letter,
    Digit,
    Symbol
};

CType getType(char a)
{
    if( (a >= 65 && a <= 90) || (a >= 97 && a <= 122) )
        return CType::Letter;
    if( a >= 48 && a <= 57 )
        return CType::Digit;
    return CType::Symbol;
}
CType getType2(char a) // more explicit 
{
    if( (a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z') )
        return CType::Letter;
    if( a >= '0' && a <= '9' )
        return CType::Digit;
    return CType::Symbol;
}
#include <cctype>
CType getType3(char a) // warning: that 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 a single character: " ;
    std::cin >> a;
    switch(getType(a)) {
        case CType::Letter:
            std::cout << "You entered a letter!";
            break;
        case CType::Digit:
            std::cout << "You entered a number!";
            break;
        case CType::Symbol:
            std::cout << "You entered a symbol!";
            break;
    }

    return 0;
}

UPD: add other solutions from comments

user5821508
  • 332
  • 2
  • 10
  • 1
    Really like your solution, uses symbolism as it should. That said, a switch over a enum class does not need a default case if every valid value is handled. I usually don't do the default case because that means that if I ever add something to my enum class, not all cases would be covered anymore, which means that I'd get a warning, reminding me of extending the switch. I'd rather include something that checks if the input is different from a single character. – Aziuth Jul 26 '17 at 10:56
  • Nice separation of processing from input and output. – Tom Blodget Jul 26 '17 at 12:42
0

switch-statements are used for constants not for conditions. So if you really want to use only a switch-case then you have to cover every possible case like so:

switch (a) 
{
  case 65:
  case 66:
  case 67:
  ...
  case 90:
  case 97:
  ...
  case 122:
    cout << "You entered a letter!";
    break;
  case 48:
  ...
}

Without a break inside a case the program will "fall through" the cases up to the first one containing a break. This however is very tedious to write and not very readable. The preferable way would be to use if-else-statements like so:

if if (a >= 48 && a <= 57)
{
  // number
} 
else ((a >= 65 && a <=90) || (a >= 97 && a <= 122)) 
{ 
  // letter
}
else
{
  // other symbol
}
muXXmit2X
  • 2,745
  • 3
  • 17
  • 34
0

Disclaimer

I completely agree with @muXXmit2X comment, but just for fun I have wrote a script which generates switch-only check (never code like this, it's terrible)

Sometimes generators are useful in real world, but be caution.


#include <iostream>

using namespace std;

int main(int argc, char const *argv[])
{
    char a;
    cout << "Enter a single character\n";
    cin >> a;

    switch (a)
    {
        case 48: 
        case 49: 
        case 50: 
        case 51: 
        case 52: 
        case 53: 
        case 54: 
        case 55: 
        case 56: 
        case 57: cout << "You entered a number!"; break;
        case 65: 
        case 66: 
        case 67: 
        case 68: 
        case 69: 
        case 70: 
        case 71: 
        case 72: 
        case 73: 
        case 74: 
        case 75: 
        case 76: 
        case 77: 
        case 78: 
        case 79: 
        case 80: 
        case 81: 
        case 82: 
        case 83: 
        case 84: 
        case 85: 
        case 86: 
        case 87: 
        case 88: 
        case 89: 
        case 90: cout << "You entered a low-case letter!"; break;
        case 97: 
        case 98: 
        case 99: 
        case 100: 
        case 101: 
        case 102: 
        case 103: 
        case 104: 
        case 105: 
        case 106: 
        case 107: 
        case 108: 
        case 109: 
        case 110: 
        case 111: 
        case 112: 
        case 113: 
        case 114: 
        case 115: 
        case 116: 
        case 117: 
        case 118: 
        case 119: 
        case 120: 
        case 121: 
        case 122: cout << "You entered a capital letter!"; break;
        default: cout << "You entered a symbol!"; break;
    }
    return 0;
}

PS: Your professor is sadist.

PPS: You should get acquainted with common code styles and choose one you like, it's very hard to read such code as yours.

0

You can avoid switch case and can do it as below

main()
{
char a;
cout << "Enter a single character: " ;
cin >> a;
if(isdigit(a))
cout<<"You entered a number!"<<endl;
else{
cout << "You entered a symbol!" << endl;
}
}
leuage
  • 566
  • 3
  • 17