switch case problem with characters

Thread Solved

Join Date: Feb 2008
Posts: 34
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
20 Days Ago
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,029
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: 176
Aia's Avatar
Aia Aia is offline Offline
Postaholic
 
1
  #2
20 Days Ago
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: 34
Reputation: miskeen is an unknown quantity at this point 
Solved Threads: 0
miskeen miskeen is offline Offline
Light Poster
 
0
  #3
20 Days Ago
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: 344
Reputation: gerard4143 is on a distinguished road 
Solved Threads: 44
gerard4143's Avatar
gerard4143 gerard4143 is offline Offline
Posting Whiz
 
0
  #4
20 Days Ago
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; 20 Days Ago at 5:30 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 39
Reputation: Iam3R is an unknown quantity at this point 
Solved Threads: 2
Iam3R Iam3R is offline Offline
Light Poster
 
-1
  #5
20 Days Ago
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: 332
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 46
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz
 
0
  #6
19 Days Ago
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: 34
Reputation: miskeen is an unknown quantity at this point 
Solved Threads: 0
miskeen miskeen is offline Offline
Light Poster
 
0
  #7
19 Days Ago
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



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC