Friday, July 31, 2009

How do you do a string compare with a switch case? (a c++ question)?

This is a c++ question, is the only way to do a string compare is an if else if statement or can I do a switch case?


I want to change this following statment to a switch case, could it be possible?


if (strcmp (open,"opendoor") == 0)


{ cout%26lt;%26lt;"It's locked."%26lt;%26lt;endl;}


else if (strcmp (open,"closedoor") == 0)


{ cout%26lt;%26lt;"It's already closed") == 0)}


else if (strcmp (open,"backward") == 0)


{ lvl3();}

How do you do a string compare with a switch case? (a c++ question)?
int flag ;


String x;


.


. //somewhere in the code make x take the value of "opendoor", "closedoor", or "backward"


.


flag = strcmp(open,x);


switch x{


case "opendoor":


if (flag == 0) cout%26lt;%26lt;"its locked"%26lt;%26lt;endl;


break;


case "closedoor":


if (flag == 0) cout%26lt;%26lt;"its already closed"%26lt;%26lt;endl;


break;


case "backward":


if (flag == 0) lvl3();


break;


}


No comments:

Post a Comment