switch case problem with characters

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2008
Posts: 38
Reputation: miskeen is an unknown quantity at this point 
Solved Threads: 0
miskeen miskeen is offline Offline
Light Poster

switch case problem with characters

 
0
  #1
Nov 3rd, 2009
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!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic
 
1
  #2
Nov 3rd, 2009
The controlling expression of a switch shall have integer type.
An array of chars will not do.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 38
Reputation: miskeen is an unknown quantity at this point 
Solved Threads: 0
miskeen miskeen is offline Offline
Light Poster
 
0
  #3
Nov 3rd, 2009
But, is there any proposed solution to what I'm looking for?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 424
Reputation: gerard4143 is on a distinguished road 
Solved Threads: 49
gerard4143's Avatar
gerard4143 gerard4143 is online now Online
Posting Pro in Training
 
0
  #4
Nov 3rd, 2009
Originally Posted by miskeen View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 67
Reputation: Iam3R is an unknown quantity at this point 
Solved Threads: 3
Iam3R Iam3R is offline Offline
Junior Poster in Training
 
-1
  #5
Nov 4th, 2009
Originally Posted by miskeen View Post
But, is there any proposed solution to what I'm looking for?
you can also use function pointers to do same thing.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 358
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 56
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz
 
0
  #6
Nov 4th, 2009
Originally Posted by miskeen View Post
But, is there any proposed solution to what I'm looking for?
use if() statement and strcmp() function for your purpose.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 38
Reputation: miskeen is an unknown quantity at this point 
Solved Threads: 0
miskeen miskeen is offline Offline
Light Poster
 
0
  #7
Nov 4th, 2009
Thank you! I'm going to use strcmp.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC