Archived

This topic is now archived and is closed to further replies.

Switch statement with char*?

This topic is 5652 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Im trying to use a switch statement to determ what action the user wants by commands, however, The user text commands are stored as char* and I apparently can''t use that in a Switch statement. Any suggestions to allow me to use my char* in a switch statement or somehow convert it to just char?

Share on other sites
switch statements only work with integral types. You''ll have to use if/else statements.

If I had my way, I''d have all of you shot!

codeka.com - Just click it.

Share on other sites
you can use a command list array and search the command in the array ,then you get a integer you can use it in your switch statement.

/*- John Dragon-*/

Share on other sites
I tried if/else but it seems to run both IF statements instead of just one or the other.

if(TempArray == "Computer1")
{
Computer1.Time += time;
return;
}
if(TempArray == "Computer2")
{
Computer2.Time += time;
return;
}

If the Array reads Computer1 Its suppose to just add that time variable to the Computer1 class and that works fine,but If I try Computer2 it doesnt add anything and exits.

Any Suggestions?

[edited by - ChaosPhoenix on September 3, 2002 5:38:56 AM]

Share on other sites
You can''t use == to compare c-style strings. Use strcmp, or use the c++ string class.

Share on other sites
Never do string comparison this way! Instead, use strcmp().

Share on other sites
using elseif might be a little nicer too.. and probably much faster if you want to execute code behind the if statements
(this way it checks every if.. even if we already found the thing we wanted)

[edited by - The Eternal on September 3, 2002 6:13:20 AM]

Share on other sites
They are right, use strcmp, but it is even easier to overload the == operator like this:

bool operator==(const char *c1, const char *c2)
{
return (strcmp(c1, c2) < 0)
}

That way you simplify your code a lot and don''t have to remind yourself of how strcmp works every time.

Share on other sites
Overloading is a possibility, but it does add another layer to the functions calls. so an extra function is called which takes extra time. and remembering how stricmp works isn''t hard at all

Share on other sites
quote:
Original post by biovenger
They are right, use strcmp, but it is even easier to overload the == operator like this:

bool operator==(const char *c1, const char *c2)

That won''t work. It''s only possible to overload operators where at least one of the operands is a user-defined type.

Share on other sites
quote:
Original post by biovenger
return (strcmp(c1, c2) < 0)
In addition to SabreMan''s comment, I''d like to point out that strcmp returs zero if its paremeters are equal.

Share on other sites
Just to try and clarify what people have said so far, when comparing c-style strings with == or switch an important thing to realise is that you''re not comparing the contents of the strings, just the addresses of their first character.

This is almost certainly not what you want.

How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

Share on other sites
You could use enums... these can be used with a switch statement

switch(computer) {case COMPUTER_1:   ...   break;case COMPUTER_2:   ...   break;}

Share on other sites
I''d suggest having a look at the std::map class, you can map a unique string onto a function or map it to an enum value.

Share on other sites
there is a code solution to this problem over on www.codeguru.com

Share on other sites
if(strcmp){}
else if(strcmp){}
else if(strcmp){}
else {}

Share on other sites
quote:
I'd suggest having a look at the std::map class, you can map a unique string onto a function or map it to an enum value.
Hmmm...

  void addToComputer1(int time) { computer1.time += time; }void addToComputer2(int time) { computer2.time += time; }...map<string, void(*)(int)> dispatcher;dispatcher["Computer1"] = addToComputer1;dispatcher["Computer2"] = addToComputer2;...map<string, void(*)(int)>::const_iterator i = dispatcher.find(tempString);if (i == dispatcher.end()) throw logic_error("blah");(*i).second(time);

...that might be a bit confusing to some people

[edited by - Beer Hunter on September 3, 2002 7:09:18 PM]

Share on other sites
Probably not useful for you, but if you have single-char commands, you can do this:
    switch((int)(char)*szCommand) {    case 0x41: // A (can one use case "A": here?)    case 0x47: // G    }
You can make this more complicated if you allow 4 chars, or if only the first 4 count:
    switch(*(int *)Command) {    case 0x41424344: // ABCD    case 0x47414D45: // GAME    }
That would require that all commands are at least 4 chars, or that the following data always are the same/cleard.

EDIT: Making the int usigned would probably be a good idea

[edited by - CWizard on September 3, 2002 8:56:32 PM]