Is it possible to use characters in a switch statement instead of int's? If so how would I go about doing it?
Printable View
Is it possible to use characters in a switch statement instead of int's? If so how would I go about doing it?
Like this
case 'a': case 'A':
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;
. . .
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..Quote:
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.
No, I wanted to compare it to the entire array.
Quote:
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!
Would that work though? Could I use enumerations like this?
would that be possible?Code:enum["daily","weekly", "monthly", "yearly"]
{
case 0;
..........
break;
>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?
I know that would probably be easier but if I did everything the easy way I don't think I would learn much. :D
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.
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]();
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?Quote:
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.