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; }
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; }
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.Originally Posted by Ancient Dragon
And yes I know the compiler change the 'a' to the right integer in ASCII.Code:char input; cin >> input; switch (input) { case 'a': case 'b': ...ect... }
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.Originally Posted by Anddos
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.Originally Posted by MacNilly
you could switch on the integer value of the pointerOriginally Posted by Quantum1024
use if statements with the cstring function strcmp
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Ž
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; }
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; }
...and goto's are for people that don't know how to program. Your program can be rewritten in a much clearer fashion:If statements are for looooossssseeeerrrs
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.
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; }
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Ž
I really like Salem's solution the best -- I'll have to keep it in mind.