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

}

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!

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

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

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.

.

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

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

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

:)

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

}

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

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;

}
commented: Yes :) +9
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.