• Advertisement

Archived

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

Switch Case using strings

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im trying to make a switch statement. I have one working fine with integers, but how do I do it with text? Im trying to get this to work:
char alignchoice;
cin >> alignchoice;
switch (alignchoice){
  case "a":
    charalign = "lawful good";
    break;
  case "b":
    charalign = "lawful neutral";
    break;
  case "c":
    charalign = "lawful evil";
    break;
  case "d":
    charalign = "neutral good";
    break;
  case "e":
    charalign = "true neutral";
    break;
  case "f":
    charalign = "neutral evil";
    break;
  case "g":
    charalign = "chaotic good";
    break;
  case "h":
    charalign = "chaotic neutral";
    break;
  case "i":
    charalign = "chaotic evil";
    break;
  default:
    charalign = "true neutral";
}
What should I have charalign declared as? A char or a string?

Share this post


Link to post
Share on other sites
Advertisement
You can''t switch on strings because they''re not integral types in C. You can switch on characters, though:

char ch;
...
switch(ch)
{
case ''a'':
case ''A'':
// do a stuff

break;

case ''b'':
case ''B'':
// do b stuff

break;

// etc...

}

Share this post


Link to post
Share on other sites
Why doesnt this work?

char charalign [20];
char alignchoice;
cin >> alignchoice;
switch (alignchoice){
case ''a'':
charalign = "lawful good";
break;
case ''b'':
charalign = "lawful neutral";
break;
case ''c'':
charalign = "lawful evil";
break;
case ''d'':
charalign = "neutral good";
break;
case ''e'':
charalign = "true neutral";
break;
case ''f'':
charalign = "neutral evil";
break;
case ''g'':
charalign = "chaotic good";
break;
case ''h'':
charalign = "chaotic neutral";
break;
case ''i'':
charalign = "chaotic evil";
break;
default:
charalign = "true neutral";
}

Share this post


Link to post
Share on other sites
Because charalign is a char of size 20 (an array). For it to work you need to use switch (charalign[0]) so you only check the first item in the array.

Worship the holy trinity that is Blitz, Babes and Beers!

Share this post


Link to post
Share on other sites
Almost - use strncpy() instead of that bizarre (blah[0])= expression you''ve got there, and it should work.

Share this post


Link to post
Share on other sites
quote:
Original post by Neo Genesis10
Because charalign is a char of size 20 (an array). For it to work you need to use switch (charalign[0]) so you only check the first item in the array.
Pay closer attention. He''s switching on alignchoice, which is perfectly valid.

@unrealfragmaster:
You can''t "assign" C-style strings. They''re not singular entities, but rather are null-terminated character arrays. You have to copy data into them using special functions. It''s tricky stuff, so I''ll recommend the superior alternative instead.

#include <iostream>
#include <string>

int main()
{
using namespace std;

string charalign;
char alignchoice;
cin >> alignchoice;

switch(alignchoice)
{
case ''a'':
charalign = "lawful good";
break;

case ''b'':
charalign = "lawful neutral";
break;

case ''c'':
charalign = "lawful evil";
break;

// etc...

}

...

return 0;
}


Now someone is going to come along and extol the virtues of C-style strings. Ignore them. C-style strings are useful in very specific contexts, but for general-purpose string manipulation std::string is far superior.

Share this post


Link to post
Share on other sites
try this:

#include <iostream.h>
#include <string.h>

int main()
{
char alignchoice;
cin >> alignchoice;
switch (alignchoice)
{ case ''a'':
strcpy(charalign,"lawful good");
case ''b'':
//b stuff
//etc.
}
return 0;
}

you need to use strcpy instead of charalign="lawful good", etc.

Share this post


Link to post
Share on other sites
@Bovine13:
Please cease and desist using and recommending the use of <iostream.h>. It is pre-standard, meaning that there are no guarantees about what exactly is in it - or even that it will exist. Use, and recommend, <iostream> instead, noting that entities within it are defined as being in the std namespace.

Share this post


Link to post
Share on other sites

// No, I'm not really serious about this, I did some ugly hacking

// here just for recreation's sake. Oluseyi (and other

// knowledgeable types), please don't hurt me :D But as a general

// principle, case statements are kind of ugly, and would like to

// be replaced by a calculation, or else a polymorphic dispatch.

// If you really need to be able to "switch on strings" (i.e.

// more than one character long), look up std::map. You can make

// a map from std::string to function objects, and then do a

// lookup in the map and invoke operator() on the result.


std::string ethics[4] { "lawful ", "neutral ", "chaotic ", "true "};
std::string morals[3] { "good", "neutral", "evil" };

std::string charalign;

cin >> alignchoice;
alignchoice -= 'a';
// implement "default" case of true neutral

if (alignchoice < 0 || alignchoice > 8) alignchoice = 4;

int charethic = alignchoice / 3;
int charmoral = alignchoice % 3;

// fix "neutral neutral" to say "true neutral" instead.

// Deliberately obfuscated to provide an exercise for the reader.

// ;)

if (charethic & charmoral & 1) charethic |= 2;

charalign += charethic;
// er, addition *is* overloaded for concatenation in std::string

// isn't it? I learned C++ in the Old Days(TM) and kinda missed

// out on the details of this stuff...

charalign += charmoral;
// you could do the concatenation instead using std::stringstream

// if you have to, I guess. Or just use a single lookup table

// instead of decomposing the alignments like I have :)



(Edit: wow, maybe I should line-wrap my comments...)

[edited by - Zahlman on April 11, 2004 8:58:03 PM]

Share this post


Link to post
Share on other sites
@Zahlman:

Interesting approach, but you should have gone all the way and changed the user prompts (subtracting the integer value of ''a'' from charalign is less than intuitive). Indexing the options by number (and reading the inputs into an integer) instead would have eliminated all that logic.

There''s nothing wrong with or ugly about switch statements, btw. Don''t be unnecessarily prejudiced against language features (including goto, when appropriate).

Oh, an deliberate obfuscation is evil.

Share this post


Link to post
Share on other sites

  • Advertisement