Switch statement and returning the correct output
I have a large application that I tried to simplify into a small program in order to ask for help on this question.
Code:
static char *the_switch(int cw);
char *txt_ptr = "";
main()
{
int cw;
int v[3];
int i;
int j;
v[0]=8;
v[1]=8;
v[2]=555;
v[3]=8;
for (i = 0; i < 4; i++)
{
cw = v[i];
txt_ptr = the_switch(cw);
printf("%s", txt_ptr);
}
}
static char *the_switch(int cw)
{
switch(cw)
{
case 8:
txt_ptr = "x";
return txt_ptr;
case 555:
txt_ptr = "\\555\\";
return txt_ptr;
case 444:
txt_ptr = "\\444\\";
return txt_ptr;
case 333:
txt_ptr = "\\333\\";
return txt_ptr;
default:
txt_ptr = "not a case";
return txt_ptr;
}
}
If I had v[2] = 555 & v[3] = 8, then I need the output to be: \555\8
which is basically just adding a backslash before cw and then adding a backslash after cw and then printing the next number in the array which is 8. If v[2] was 444 and v[3] was 8, then I basically need the output to be the same: \444\8
And finally if v[1] was 333, v[2] was 8, and v[3] was 8, then I would need the output to look like this: \333\8\8
The code that I have above only gives me \555\ instead of \555\8 and obviously the other cases only give me that type of output too. What can I add to fix this in order to get the output that I'm looking for? I'm working with someone else's code, so I'm kind of restricted on how much I can change things. Any help is greatly appreciated. Thanks