Jump to content
  • Advertisement

Archived

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

unrealfragmaster

Switch Case using strings

This topic is 5159 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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!