Thread: char switch statement

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    12

    char switch statement

    i am trying to write a switch statement that displays whether ch is a vowel, a digit, or something else. i got it to display it as a vowel or something else but the digit part is killing me. this is what i have:

    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    
    int main() {
    
      char ch;
    
      //prompt user
      cout << "Enter an character: ";
    
      cin >> ch;
    
      //print out the common part of the message here to save typing
      cout << "You entered ";
    
      switch( ch ) {
    
          case 'A': case 'E': case 'I': case 'O': case 'U':
    	  case 'a': case 'e': case 'i': case 'o': case 'u':
    		cout << "a vowel.\n";
    	  // it's a vowel, process accordingly
    	  break;
    
    	  case ch 0 - 9:            //>= '0' && ch <= '9':
    		  cout << "a digit.\n";
    
    	default:
    		cout << "something else.\n";
    		break;
    }  
    
        
    
      return 0;
    
    }
    i am lost any help would be great thanks

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    If you are stuck on using a switch to do this you'd need to use:
    Code:
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
        cout << "a digit.\n";
        break;
    ... similar to what you did with the vowels part.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Excuse my confusion, but I don't understand how you did the vowel part properly, yet decided to try some random garbage for the digits that resembles nothing you'd see in a switch statement.

    Anyway, as mp5, said.
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    12
    there is no way of adding just one case for all of the digits? i know u can do it with a if - else statement but i have to do it as a switch statement. thanks

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    No, there is no way. Switch statements are case by case.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    12
    thanks, and i just put the cases across instead of down that is why it looks like that. either way it works.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'd recommend using an if/else statement for this instead of switch if only because there would be less typing and it wouldn't look as silly.

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    If you have a range of values that you wish to pass to a case statement, one way is to create a helper function which will boil down the user's input into a single value representing the range of input values. eg,
    Code:
    int findCategory(char c)
    { 
        vector<string> lookup;
        lookup.push_back("aeiouAEIOU");
        lookup.push_back("1234567890");
        lookup.push_back("bcdfghjklmnpqrstvwxzyBCDFGHJKLMNPQRSTVWXYZ");
    
        // Code to search through the lookup table
    }
    Your findCategory() function could contain a lookup table, such as the vector<string> in the above example, with each entry containing all valid inputs for each input range. It should be fairly trivial to search each entry for a match. (or return an "error" value if no match is found)
    Code:
    switch( findCategory(input) )
    {
    case 0:
        cout << "Vowel";
        break;
    case 1:
        cout << "Number";
        break;
    case 2:
        cout << "Consonant";
        break;
    default:
        cout << "Error";
    }
    So, all possible values at position 0 in the vector might be vowels.. then all possible values at position 1 in the vector might be numbers.. etc.

    you can improve readability of the switch statement with this method by using an enum instead of "magic" numbers, but that's not essential.
    Last edited by Bench82; 05-03-2006 at 05:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  3. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM