Switch Statements.

This is a discussion on Switch Statements. within the C++ Programming forums, part of the General Programming Boards category; Is it possible to use characters in a switch statement instead of int's? If so how would I go about ...

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Question Switch Statements.

    Is it possible to use characters in a switch statement instead of int's? If so how would I go about doing it?
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Like this

    case 'a': case 'A':
    The keyboard is the standard device used to cause computer errors!

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    yes.
    Code:
    #include <iostream>
    #include <cctype>
    
    int main()
    {
    	char test;
    	
    	std::cin >> test;
    	
    	switch(toupper(test)) {
    		case 'A':
    			std::cout << "a";
    			break;
    		default:
    			std::cout << "letter";
    			break;
    	}
    	
    	return 0;
    }

  4. #4
    1479
    Join Date
    Aug 2003
    Posts
    253
    Guess I should have been more specific. What I meant was character strings. Would this work:
    Code:
    switch(type[20])
    {
        case (!strcmp("daily",type);
                cout <<value;
    . . .
    Maybe this will make sense to you guys.
    Knowledge is power and I want it all

    -0RealityFusion0-

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by RealityFusion
    Guess I should have been more specific. What I meant was character strings. Would this work:
    Code:
    switch(type[20])
    {
        case (!strcmp("daily",type);
                cout <<value;
    . . .
    Maybe this will make sense to you guys.
    well, in this example, you're comparing one element of the array, and since you say character strings, i'm assuming it's a char. in essence, you're saying case 0: . hmm.. have you tried it. i'm not quite sure. let me try it..


    EDIT: okay, I tried it. it doesn't compile on gcc, and I have to say I didn't really know if it would, but I also didn't expect it to.
    Last edited by alpha; 08-18-2003 at 11:06 PM.

  6. #6
    1479
    Join Date
    Aug 2003
    Posts
    253
    No, I wanted to compare it to the entire array.
    Knowledge is power and I want it all

    -0RealityFusion0-

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by RealityFusion
    No, I wanted to compare it to the entire array.
    Code:
    #include <iostream>
    #include <cstring>
    
    int main()
    {
    	char test[5];
    	
    	std::cin >> test;
    	
    	switch(test) {
    		case (!strcmp("word", test)):
    			std::cout << "a";
    			break;
    		default:
    			std::cout << "letter";
    			break;
    	}
    	
    	return 0;
    }
    that's the test code I ran. it didn't compile based on the case statement not being an integer.

    in any case, that's the type of condition that is best used in an if statement.

  8. #8
    1479
    Join Date
    Aug 2003
    Posts
    253
    I think I should just use enumurations. lol.....I feel stupid for fogetting that. Ha!
    Knowledge is power and I want it all

    -0RealityFusion0-

  9. #9
    1479
    Join Date
    Aug 2003
    Posts
    253
    Would that work though? Could I use enumerations like this?
    Code:
    enum["daily","weekly", "monthly", "yearly"]
    {
    case 0;
             ..........
             break;
    would that be possible?
    Knowledge is power and I want it all

    -0RealityFusion0-

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >would that be possible?
    No. Just out of curiosity, why can't you use a series of if..else if..else statements in place of the switch?
    My best code is written with the delete key.

  11. #11
    1479
    Join Date
    Aug 2003
    Posts
    253
    I know that would probably be easier but if I did everything the easy way I don't think I would learn much.
    Knowledge is power and I want it all

    -0RealityFusion0-

  12. #12
    1479
    Join Date
    Aug 2003
    Posts
    253
    Also, I am using the if statment when calling a funtions of a class and it would save space and time if I could do it this way.
    Knowledge is power and I want it all

    -0RealityFusion0-

  13. #13
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    You can only use switch statements with integer types (int, char, enums, etc) so its not possible to use floats, char arrays, strings or the like. Unfortunately you'll have to use if/else blocks.

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Also, I am using the if statment when calling a funtions of a class and it would save space and time if I could do it this way.
    In my experience, a switch usually takes up more space than the equivalent if series. Usually, when somebody wants this kind of functionality, they would be better off using a data structure to hold string/function pairs...a std::map for example. Then the search becomes virtually nothing:
    Code:
    if (mymap.find(test) != mymap.end())
        mymap[test]();
    My best code is written with the delete key.

  15. #15
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Originally posted by PJYelton
    You can only use switch statements with integer types (int, char, enums, etc) so its not possible to use floats, char arrays, strings or the like. Unfortunately you'll have to use if/else blocks.
    It isn't that much additional code and it is a lot easier to handle than switch. Buggy switch code sometimes behaves extremely strange and is hard to debug. Thats at least what I think. Maybe it's just because I never use any switch comands. I don't even know the word. What were we talking about?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Files Inside Switch Statements
    By arealfind08 in forum C Programming
    Replies: 11
    Last Post: 03-17-2009, 04:49 PM
  2. Switch Statements
    By lazyturtle in forum C++ Programming
    Replies: 12
    Last Post: 05-02-2007, 02:40 AM
  3. Explanation of switch statements
    By ammochck21 in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2006, 01:59 PM
  4. help with switch statements
    By Wexy in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 04:44 PM
  5. Switch Statements
    By blackgingr in forum C Programming
    Replies: 3
    Last Post: 10-07-2002, 02:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21