944,144 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1051
  • C RSS
Nov 3rd, 2009
0

switch case problem with characters

Expand Post »
Hello guys,

How can I make a switch case using an array of chars.
Ex:
  1. char input[50];
  2. // my code here
  3. switch ( input ) {
  4. case "abc":
  5. // do something
  6. break;
  7. case "xyz":
  8. // do otherthings
  9. break;
  10. }

Is that possible in C?

Thank you!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
miskeen is offline Offline
63 posts
since Feb 2008
Nov 3rd, 2009
1
Re: switch case problem with characters
The controlling expression of a switch shall have integer type.
An array of chars will not do.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Nov 3rd, 2009
0
Re: switch case problem with characters
But, is there any proposed solution to what I'm looking for?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
miskeen is offline Offline
63 posts
since Feb 2008
Nov 3rd, 2009
0
Re: switch case problem with characters
Click to Expand / Collapse  Quote originally posted by miskeen ...
But, is there any proposed solution to what I'm looking for?
Your comparing a array of characters(char input[50]) that is supposed to be a C style string("abc")...I would investigate string compare functions
Last edited by gerard4143; Nov 3rd, 2009 at 5:30 pm.
Reputation Points: 499
Solved Threads: 367
Postaholic
gerard4143 is offline Offline
2,198 posts
since Jan 2008
Nov 4th, 2009
-1
Re: switch case problem with characters
Click to Expand / Collapse  Quote originally posted by miskeen ...
But, is there any proposed solution to what I'm looking for?
you can also use function pointers to do same thing.
Reputation Points: 34
Solved Threads: 4
Junior Poster
Iam3R is offline Offline
110 posts
since Oct 2009
Nov 4th, 2009
0
Re: switch case problem with characters
Click to Expand / Collapse  Quote originally posted by miskeen ...
But, is there any proposed solution to what I'm looking for?
use if() statement and strcmp() function for your purpose.
Reputation Points: 121
Solved Threads: 61
Posting Pro in Training
dkalita is offline Offline
402 posts
since Sep 2009
Nov 4th, 2009
0
Re: switch case problem with characters
Thank you! I'm going to use strcmp.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
miskeen is offline Offline
63 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: read e-mail attachment with POP3
Next Thread in C Forum Timeline: passing structuce to function as an argument





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC