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