Switch case with String not working!

Thread Solved

Join Date: May 2009
Posts: 3
Reputation: razsadeq is an unknown quantity at this point 
Solved Threads: 0
razsadeq razsadeq is offline Offline
Newbie Poster

Switch case with String not working!

 
0
  #1
Jun 19th, 2009
the following is a simple program that fragments standardized name consisting of 3 letters and 2 numbers then prints the full word, for instance spa11 print spanish eleven.

Why is it not working?


  1. char sname[4];
  2. char snum[3];
  3.  
  4. // void strnumCpy(char *dest, char *source) { // NOT ACCURATE
  5. void strNumCpy(char *dest, const char *source) { // You are passing a const char in the source parameter, not a regular char.
  6. int i=0;
  7. do { // Isn't it easier to use a for loop here ? :)
  8. *source++;
  9. i += 1;
  10. } while (i < 3);
  11. while (*source)
  12. *dest++ = *source++ ;
  13. *dest = 0; // This adds the null terminator
  14. }
  15.  
  16. void look_up(char *fname)
  17. {
  18.  
  19. strncpy(sname, fname, 3);
  20. sname[3] = 0;
  21. strNumCpy(snum, fname);
  22. read_sname(sname);
  23. read_snum(snum);
  24.  
  25. }
  26.  
  27. void read_sname(char *snm)
  28. {
  29. switch(snm) {
  30.  
  31. case "ara": printf("\n%s\n", 'arabic'); break;
  32. case "spa": printf("\n%s\n", 'spanish'); break;
  33. case "eng": printf("\n%s\n", 'english'); break;
  34. case "bio": printf("\n%s\n", 'biology'); break;
  35. case "ger": printf("\n%s\n", 'german'); break;
  36.  
  37. }
  38.  
  39. }
  40.  
  41. void read_snum(char *sno)
  42. {
  43.  
  44. switch(sno) {
  45.  
  46. case "01": printf("\n%s\n", 'one'); break;
  47. case "02": printf("\n%s\n", 'two'); break;
  48. case "03": printf("\n%s\n", 'three'); break;
  49. case "05": printf("\n%s\n", 'five'); break;
  50. case "11": printf("\n%s\n", 'eleven'); break;
  51.  
  52. }
  53.  
  54. }
  55.  
  56. void main()
  57. {
  58.  
  59. look_up("spa02");
  60. look_up("eng11");
  61. look_up("ger05");
  62.  
  63. }
Last edited by Tekmaven; Jun 19th, 2009 at 7:19 pm. Reason: Code Tags
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Switch case with String not working!

 
0
  #2
Jun 19th, 2009
There are several problems but first of all your source strings are how long? And you're stuffing them into a buffer how long?

5 + 1 chars in 4 char buffer. What about the last character and the terminator?

Do you mean to intentioning destroying the 1st digt of the source data by setting the terminator after the first three letters of ASCII.

Don't forget ASCIIz strings have a hidden ('\0') terminator.

Correct and repost!


Note: Use stack buffers and make them much larger then needed. Won't cost you anything!
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Switch case with String not working!

 
0
  #3
Jun 19th, 2009
Oh and while you're at it! Fix your case statement!

Each case condition is an integer value (enum) only!
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Switch case with String not working!

 
0
  #4
Jun 19th, 2009
It would be more correct to say that switch case only works for integer constants

To the OP:
Read this before posting!
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Switch case with String not working!

 
0
  #5
Jun 19th, 2009
as already stated, never try to use strings as cases for a switch. Some languages (like Perl) will allow this. C will not.

you could, for instance, try something like this:

  1. void read_snum(char *sno)
  2. {
  3. int courseNum;
  4. courseNum = atol(sno);
  5.  
  6. switch(courseNum)
  7. {
  8. case 1: printf("\n%s\n", 'one'); break;
  9. case 2: printf("\n%s\n", 'two'); break;
  10. case 3: printf("\n%s\n", 'three'); break;
  11. case 5: printf("\n%s\n", 'five'); break;
  12. case 11: printf("\n%s\n", 'eleven'); break;
  13.  
  14. default: printf("\nInvalid Course Number\n");
  15. }
  16. }

note, that my use of atol employs no error checking. I'm just using this as an example for switch/case statement. the preferred method would be strtol() and verify the string contains a valid number before moving to the switch.

.
Last edited by jephthah; Jun 19th, 2009 at 5:48 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 463
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Switch case with String not working!

 
0
  #6
Jun 20th, 2009
switch(expression) - where expression must be of int or char data type.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Switch case with String not working!

 
0
  #7
Jun 20th, 2009
Hey jephthah, since when can we use single quotes for c-strings?
  1. case 1: printf("\n%s\n", 'one'); break;
  2. case 2: printf("\n%s\n", 'two'); break;
  3. case 3: printf("\n%s\n", 'three'); break;
  4. case 5: printf("\n%s\n", 'five'); break;
  5. case 11: printf("\n%s\n", 'eleven'); break;
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Switch case with String not working!

 
0
  #8
Jun 20th, 2009
Originally Posted by tux4life View Post
Hey jephthah, since when can we use single quotes for c-strings?
  1. case 1: printf("\n%s\n", 'one'); break;
  2. case 2: printf("\n%s\n", 'two'); break;
  3. case 3: printf("\n%s\n", 'three'); break;
  4. case 5: printf("\n%s\n", 'five'); break;
  5. case 11: printf("\n%s\n", 'eleven'); break;
oh, hell, i just cut and pasted his code. i didn't even notice that

Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 3
Reputation: razsadeq is an unknown quantity at this point 
Solved Threads: 0
razsadeq razsadeq is offline Offline
Newbie Poster

Re: Switch case with String not working!

 
0
  #9
Jun 20th, 2009
Hey Guys, Thanks alot for all your replies, here is a corrected version of the code.
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. //Razan Sadeq
  5. //Blind Student Study Helper Project
  6. //PIC16F877A is used with 4 MHz oscillator
  7. //vmusic2 is used and connected to USART module.
  8. //Modified by N.A.
  9.  
  10. char sname[4];
  11. char snum[3];
  12.  
  13.  
  14. void strNumCpy(char *dest, const char *source) {
  15. int i=0;
  16. do {
  17. *source++;
  18. i += 1;
  19. } while (i < 3);
  20. while (*source)
  21. *dest++ = *source++ ;
  22. *dest = 0; // This adds the null terminator
  23. }
  24.  
  25. void read_sname(char *snm)
  26. {
  27. char s = *snm;
  28. switch(s) {
  29.  
  30. case 'a': printf("\n%s\n", "arabic"); break;
  31. case 's': printf("\n%s\n", "spanish"); break;
  32. case 'e': printf("\n%s\n", "english"); break;
  33. case 'b': printf("\n%s\n", "biology"); break;
  34. case 'g': printf("\n%s\n", "german"); break;
  35.  
  36. }
  37.  
  38. }
  39.  
  40. void read_snum(char *sno)
  41. {
  42.  
  43. if (!strcmp(sno,"01"))
  44. printf("\n%s\n", "one");
  45. else if (!strcmp(sno,"02"))
  46. printf("\n%s\n", "two");
  47. else if (!strcmp(sno,"03"))
  48. printf("\n%s\n", "three");
  49. else if (!strcmp(sno,"05"))
  50. printf("\n%s\n", "five");
  51. else if (!strcmp(sno,"11"))
  52. printf("\n%s\n", "eleven");
  53.  
  54. }
  55.  
  56. void look_up(char *fname)
  57. {
  58.  
  59. strncpy(sname, fname, 3);
  60. sname[3] = 0;
  61. strNumCpy(snum, fname);
  62. read_sname(sname);
  63. read_snum(snum);
  64.  
  65. }
  66.  
  67.  
  68.  
  69. void main()
  70. {
  71.  
  72. look_up("spa02");
  73. look_up("eng11");
  74. look_up("ger05");
  75.  
  76. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Switch case with String not working!

 
0
  #10
Jun 20th, 2009
Don't use void main() !!!!!!!!
Read this if you don't understand why
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
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