Is it possible to use characters in a switch statement instead of int's? If so how would I go about doing it?
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-
Like this
case 'a': case 'A':
The keyboard is the standard device used to cause computer errors!
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; }
Guess I should have been more specific. What I meant was character strings. Would this work:
Maybe this will make sense to you guys.Code:switch(type[20]) { case (!strcmp("daily",type); cout <<value; . . .
Knowledge is power and I want it all
-0RealityFusion0-
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..Originally posted by RealityFusion
Guess I should have been more specific. What I meant was character strings. Would this work:
Maybe this will make sense to you guys.Code:switch(type[20]) { case (!strcmp("daily",type); cout <<value; . . .
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.
No, I wanted to compare it to the entire array.
Knowledge is power and I want it all
-0RealityFusion0-
Originally posted by RealityFusion
No, I wanted to compare it to the entire array.that's the test code I ran. it didn't compile based on the case statement not being an integer.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; }
in any case, that's the type of condition that is best used in an if statement.
I think I should just use enumurations. lol.....I feel stupid for fogetting that. Ha!
Knowledge is power and I want it all
-0RealityFusion0-
Would that work though? Could I use enumerations like this?
would that be possible?Code:enum["daily","weekly", "monthly", "yearly"] { case 0; .......... break;
Knowledge is power and I want it all
-0RealityFusion0-
>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.
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-
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-
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.
>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.
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?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.