954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Switch case with String not working!

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?

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");

}
razsadeq
Newbie Poster
3 posts since May 2009
Reputation Points: 19
Solved Threads: 0
 

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!

wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
 

Oh and while you're at it! Fix your case statement!

Each case condition is an integer value (enum) only!

wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
 

It would be more correct to say that switch case only works for integer constants :P

To the OP:
Read this before posting!

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

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:

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.

.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

switch(expression) - where expression must be of int or char data type.

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

Hey jephthah, since when can we use single quotes for c-strings?

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;



:P

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

Hey jephthah, since when can we use single quotes for c-strings?

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;
:P

oh, hell, i just cut and pasted his code. i didn't even notice that

:)

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

Hey Guys, Thanks alot for all your replies, here is a corrected version of the code.

#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");

}
razsadeq
Newbie Poster
3 posts since May 2009
Reputation Points: 19
Solved Threads: 0
 

Don't use void main() :angry: !!!!!!!!
Read this if you don't understand why :)

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 
Don't use void main() :angry: !!!!!!!! Read this if you don't understand why :)

LOL@ "The return type of main function has to be int. Stop and repeat the last sentence 10 times if you are using void main(). "

Ok, there you go with INT :) the code is also modified to accept the first three letters, not only the first one.

#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.
//Modified by Raz

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)
{
  

	if (!strcmp(snm,"spa"))
		printf("\n%s\n", "Spanish");
	else if (!strcmp(snm,"eng"))
		printf("\n%s\n", "English");
	else if (!strcmp(snm,"bio"))
		printf("\n%s\n", "Biology");
	else if (!strcmp(snm,"ara"))
		printf("\n%s\n", "Arabic");
	else if (!strcmp(snm,"ger"))
		printf("\n%s\n", "German");
     
     

}

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);

}



int main()
{

  look_up("spa02");
  look_up("eng11");
  look_up("ger05");
  return 0;

}
razsadeq
Newbie Poster
3 posts since May 2009
Reputation Points: 19
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You