| | |
Switch case with String not working!
Thread Solved |
•
•
Join Date: May 2009
Posts: 3
Reputation:
Solved Threads: 0
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?
Why is it not working?
c Syntax (Toggle Plain Text)
char sname[4]; char snum[3]; // void strnumCpy(char *dest, char *source) { // NOT ACCURATE void strNumCpy(char *dest, const char *source) { // You are passing a const char in the source parameter, not a regular char. int i=0; do { // Isn't it easier to use a for loop here ? :) *source++; i += 1; } while (i < 3); while (*source) *dest++ = *source++ ; *dest = 0; // This adds the null terminator } void look_up(char *fname) { strncpy(sname, fname, 3); sname[3] = 0; strNumCpy(snum, fname); read_sname(sname); read_snum(snum); } void read_sname(char *snm) { switch(snm) { case "ara": printf("\n%s\n", 'arabic'); break; case "spa": printf("\n%s\n", 'spanish'); break; case "eng": printf("\n%s\n", 'english'); break; case "bio": printf("\n%s\n", 'biology'); break; case "ger": printf("\n%s\n", 'german'); break; } } void read_snum(char *sno) { switch(sno) { case "01": printf("\n%s\n", 'one'); break; case "02": printf("\n%s\n", 'two'); break; case "03": printf("\n%s\n", 'three'); break; case "05": printf("\n%s\n", 'five'); break; case "11": printf("\n%s\n", 'eleven'); break; } } void main() { look_up("spa02"); look_up("eng11"); look_up("ger05"); }
Last edited by Tekmaven; Jun 19th, 2009 at 7:19 pm. Reason: Code Tags
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!
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!
It would be more correct to say that switch case only works for integer constants 
To the OP:
Read this before posting!

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."
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:
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.
.
you could, for instance, try something like this:
c Syntax (Toggle Plain Text)
void read_snum(char *sno) { int courseNum; courseNum = atol(sno); switch(courseNum) { case 1: printf("\n%s\n", 'one'); break; case 2: printf("\n%s\n", 'two'); break; case 3: printf("\n%s\n", 'three'); break; case 5: printf("\n%s\n", 'five'); break; case 11: printf("\n%s\n", 'eleven'); break; default: printf("\nInvalid Course Number\n"); } }
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.
Hey jephthah, since when can we use single quotes for c-strings?
C Syntax (Toggle Plain Text)
case 1: printf("\n%s\n", 'one'); break; case 2: printf("\n%s\n", 'two'); break; case 3: printf("\n%s\n", 'three'); break; case 5: printf("\n%s\n", 'five'); break; 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."
•
•
•
•
Hey jephthah, since when can we use single quotes for c-strings?
C Syntax (Toggle Plain Text)
case 1: printf("\n%s\n", 'one'); break; case 2: printf("\n%s\n", 'two'); break; case 3: printf("\n%s\n", 'three'); break; case 5: printf("\n%s\n", 'five'); break; case 11: printf("\n%s\n", 'eleven'); break;
•
•
Join Date: May 2009
Posts: 3
Reputation:
Solved Threads: 0
Hey Guys, Thanks alot for all your replies, here is a corrected version of the code.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> //Razan Sadeq //Blind Student Study Helper Project //PIC16F877A is used with 4 MHz oscillator //vmusic2 is used and connected to USART module. //Modified by N.A. char sname[4]; char snum[3]; void strNumCpy(char *dest, const char *source) { int i=0; do { *source++; i += 1; } while (i < 3); while (*source) *dest++ = *source++ ; *dest = 0; // This adds the null terminator } void read_sname(char *snm) { char s = *snm; switch(s) { case 'a': printf("\n%s\n", "arabic"); break; case 's': printf("\n%s\n", "spanish"); break; case 'e': printf("\n%s\n", "english"); break; case 'b': printf("\n%s\n", "biology"); break; case 'g': printf("\n%s\n", "german"); break; } } void read_snum(char *sno) { if (!strcmp(sno,"01")) printf("\n%s\n", "one"); else if (!strcmp(sno,"02")) printf("\n%s\n", "two"); else if (!strcmp(sno,"03")) printf("\n%s\n", "three"); else if (!strcmp(sno,"05")) printf("\n%s\n", "five"); else if (!strcmp(sno,"11")) printf("\n%s\n", "eleven"); } void look_up(char *fname) { strncpy(sname, fname, 3); sname[3] = 0; strNumCpy(snum, fname); read_sname(sname); read_snum(snum); } void main() { look_up("spa02"); look_up("eng11"); look_up("ger05"); }
![]() |
Similar Threads
- switch statement on String in Java (Java)
- Reputed number problem switch case statement (VB.NET)
- Convert calculator program to use switch case statements (C++)
- Help with switch/case statement navigation (PHP)
- why can't you use a switch statment with a string? (C++)
- Switch Case Statement (Java)
- switch/case statement (C++)
Other Threads in the C Forum
- Previous Thread: Greater Than Less Than in "C"
- Next Thread: need help please !!!!
| Thread Tools | Search this Thread |
* adobe api array arrays binarysearch calculate centimeter char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking hardware highest homework i/o ide inches incrementoperators intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation pattern pdf performance pointer posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h






!!!!!!!!