6 Replies - 690 Views - Last Post: 31 May 2009 - 06:57 AM Rate Topic: -----

#1 dartoscoder  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 313
  • Joined: 15-May 09

Switch with strings

Posted 30 May 2009 - 06:28 PM

I am try to make a game and i would like to know if there is a way to make a switch loop using strings as cases insted of integers.
Is This A Good Question/Topic? 0
  • +

Replies To: Switch with strings

#2 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5191
  • View blog
  • Posts: 26,903
  • Joined: 10-May 07

Re: Switch with strings

Posted 30 May 2009 - 06:33 PM

Single quotes will check characters. String checking in C/C++ is best left to string functions. If the string is an array, then the case probably wouldn't work anyhow, because of the extra space.

But my answer would be no, I checked with gcc © & switch will only check integers, or single characters.
Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Switch with strings

Posted 30 May 2009 - 06:42 PM

switch only work with integer types. For strings you would want to use if-else-if etc. There are other options but those generally require a bit more work and are not as direct.

For example you can use OOP and create objects representing each condition and then scroll though a collection (or even use more advanced searches) and then call a function on the object.

Basically -- you need to find another route.
Was This Post Helpful? 0
  • +
  • -

#4 Dantheman  Icon User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • Posts: 445
  • Joined: 27-May 09

Re: Switch with strings

Posted 30 May 2009 - 10:17 PM

View Postdartoscoder, on 30 May, 2009 - 05:28 PM, said:

I am try to make a game and i would like to know if there is a way to make a switch loop using strings as cases insted of integers.


No, Switch only works with integral types (char, int, long) and enums.
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Switch with strings

Posted 30 May 2009 - 10:18 PM

:) maybe it will be come clear if a few more people mention it...
Was This Post Helpful? 0
  • +
  • -

#6 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Switch with strings

Posted 30 May 2009 - 11:03 PM

Could you do a strcmp() type of thing for this particular question?
Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Switch with strings

Posted 31 May 2009 - 06:57 AM

not really strcmp() works against 2 strings, and so it would only help you determine 1 case. You can make function calls in the case statements.

Basically the easiest solution is to use if-else blocks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1