Thread: what types can you do switch on

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    319

    what types can you do switch on

    just wondering what data types can you use the switch on
    i was thinking if it could do strings

    Code:
    cin >> stringname;
    switch(stringname)
    {
    case "random"
    break;
    case "random2"
    break;
    default:
    break;
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    switch is only for integers. Use a series of if statements for all other data types, such as strings.

  3. #3
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Quote Originally Posted by Ancient Dragon
    switch is only for integers. Use a series of if statements for all other data types, such as strings.
    Actually you can use chars also.
    Code:
    char input;
    cin >> input;
    
    switch (input)
    {
        case 'a':
        case 'b':
        ...ect...
    }
    And yes I know the compiler change the 'a' to the right integer in ASCII.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    well my compiler dosent let me do strings with switch
    what about char array with switch , then you can do "blah"

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by Anddos
    well my compiler dosent let me do strings with switch
    what about char array with switch , then you can do "blah"
    That won't work, you can only use a switch with a single char. As mentioned above use if statements.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by MacNilly
    Actually you can use chars also.
    char is a one-byte integer. There really is no such thing as "chacter" in C or C++ -- what we call character is really just an integer.

  7. #7
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    Quote Originally Posted by Quantum1024
    That won't work, you can only use a switch with a single char. As mentioned above use if statements.
    you could switch on the integer value of the pointer

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    use if statements with the cstring function strcmp

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    If statements are for looooossssseeeerrrs! Always use switches for everything!

    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
                   
    int main() {
        char *swtchCase[5] = {"Hello","World","How","Are","You"};
        char word[] = "You";
        int i = 0;
        
        cmpNoMtch:
          switch (i) {
            case 0: case 1: case 2: case 3: case 4:
              switch (strcmp(word,swtchCase[i])) {
                case 0:
                  cout << "Found match!";
                  goto cmpMatch;
                default:
                  i++;
                  goto cmpNoMtch;
              }
           default:
               cout << "No Match!";
               break;      
           }  
           
        cmpMatch:
        
        return 0;
    }
    Sent from my iPadŽ

  10. #10
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    You can switch on any integer type which include bool,short, char, int,long, and long long, or any type that has an implicit conversion to an int type, so what you can do is create your own string class that someone hashes out a unique integer for a string representation.

    However, the case statements only take const expression values, so you would not be able to use a string as the case or even a converted string. Here is the best alternative I can offer:

    Code:
    #include <iostream>
    class mystring
    {
    public:
    	mystring(char* cstring)
    	{
    		mString = cstring;
    	}
    	operator const int()	
    	{
    		// since this is only to test theory
    		// my "hash" only returns int value of 1st char
    		// but in practice you should try to create 
    		//a int value unique to the string
    
    		return (const int)mString[0];
    	}
    private:
    	char* mString;
    
    
    };
    int main()
    {
    	mystring name("Darryl");
    
    	switch (name)
    	{
    	case 68:
    		std::cout << "Darry\nl";
    		break;
    	default:
    		std::cout << "Not Darryl\n";
    	}
    
        return 0;
    }

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Use a map
    Code:
    #include <iostream>
    #include <map>
    #include <string>
    using namespace std;
    
    enum cmdIdents {
      I_FILE,
      I_EDIT,
      I_PRINT,
    };
    
    int main ( ) {
      map<string,cmdIdents> table;
    
      table["file"] = I_FILE;
      table["edit"] = I_EDIT;
      table["print"]= I_PRINT;
    
      string cmd = "file";
      switch ( table[cmd] ) {
        case I_FILE:
          cout << "case file" << endl;
          break;
        break;
        case I_EDIT:
          cout << "case edit" << endl;
          break;
        break;
        case I_PRINT:
          cout << "case print" << endl;
          break;
        break;
        default:
          cout << "Huh?" << endl;
          break;
      }
      return 0;
    }

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    If statements are for looooossssseeeerrrs
    ...and goto's are for people that don't know how to program. Your program can be rewritten in a much clearer fashion:
    Code:
    #include <iostream> 
    #include <cstring> //strcmp()
    using namespace std;
    
    int main()
    {
    	const int size = 5;
    	char* words[size] = {"Hello","World","How","Are","You"};
            char searchWord[] = "You";
    	int i = 0;
        
    	for(; i < size; i++)
    	{
    		if(strcmp(searchWord, words[i]) == 0)
    			break;
    	}
    	
    	if(i < size)
    		cout<<"Found match!"<<endl;
    	else
    		cout<<"No match!"<<endl;
    
    	return 0;
    }
    Last edited by 7stud; 02-15-2006 at 11:25 AM.

  13. #13
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Well, since this is c++ board, not C
    Code:
    #include <iostream> 
    #include <string>
    using namespace std;
    
    int main()
    {
    	std::string words[] = {"Hello","World","How","Are","You"};
    	std::string searchWord("You");
    	bool found = false;
    	for(int i = 0; i < sizeof(words)/sizeof(words[0]); i++)
    	{
    		if(searchWord == words[i])
    		{
    			found = true;
    			break;
    		}
    	}
    	
    	if(found)
    		cout<<"Found match!"<<endl;
    	else
    		cout<<"No match!"<<endl;
    
    	return 0;
    }

  14. #14
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    My Humor


    Ancient's and 7stud's Head




    And for anyone else that didn't get it, if you ever see me spelling a woooorrrrddddd liiiiikkkkeeee thhiiiiiissss. I'm probably being sarcastic.
    Last edited by SlyMaelstrom; 02-15-2006 at 11:30 AM.
    Sent from my iPadŽ

  15. #15
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267

    Thumbs up

    I really like Salem's solution the best -- I'll have to keep it in mind.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. invalid types 'int[int]' for array subscript
    By kolistivra in forum C++ Programming
    Replies: 6
    Last Post: 12-11-2010, 12:57 PM
  2. Replies: 6
    Last Post: 08-23-2008, 01:16 PM
  3. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM