Thread: Switch statement and returning the correct output

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    49

    Question 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

  2. #2
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    The output in may machine is xx\555\x, which is the expected output after examining the code.

    Just use 8 instead of "x" in the the_switch() function.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    What in the world... ? Well here's my idea:
    Code:
    #include <stdio.h>
    
    char separator (int *v)
    {
        int *next = v;
        if (next && *++next)
            return '\\';
        else
            return '\0';
    }
    
    int main(void)
    {
        int v[] = { 8, 444, 555, 333, };
        size_t size = sizeof v / sizeof v[0];
        int i;
        for (i = 0; i < size; ++i)
        {
            printf("%d%c", v[i], separator(&v[i]));
        }
        putchar('\n');
        return 0;
    }
    Owner@pavilion ~
    $ ./a
    8\444\555\333

    Will something like that work for you?

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Or just something like:
    Code:
    printf("\\%d\\%d\\%d\n", v[1], v[2], v[3]);
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    49

    Unhappy

    fnoyan - yes, i do see that my example would produce the correct output if the x in the switch statement was an 8.

    However, how would I do "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" with the switch statement that I have in place??? The problem with changing things around as citizen and itsme86 suggested is the fact that I have pre-existing code that has MANY other cases besides mine. I only have to add these 3 cases to the switch statement, but I'm having a really hard time figuring out how to get the output look as I need it for case 333. Your help is always appreciated.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    Why do your cases ever return a trailing \
    It looks like you always want \#

    I would think your cases should be more like...
    Code:
    "\\8"
    "\\333"
    "\\555"
    ...
    Though I don't seen any sense in using that method to print a \ in front of a number.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    This is your code, but with some modification

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    void the_switch(int cw);
    char *txt_ptr;
    
    int main()
    {
    	int cw;
    	int v[3];
    	int i;
    	int j;
    	txt_ptr = malloc(80);
    	strcpy(txt_ptr,"");
    	
    	v[0]=8;
    	v[1]=8;
    	v[2]=555;
    	v[3]=8;
    	for (i = 0; i < 4; i++)
    	{
    		cw = v[i];
    		the_switch(cw);
    	  	printf("%s\n",txt_ptr);
    	}
    	
    	getchar();
    	return 0;
    }
    
    void the_switch(int cw)
    {
    	switch(cw)
    		{
    		case 8:
    			strcat(txt_ptr,"\\x");
    			break;
    		case 555:
    			strcat(txt_ptr,"\\555");
    			break;
    		case 444:
    			strcat(txt_ptr,"\\444");
    			break;
    		case 333:
    			strcat(txt_ptr,"\\333");
    			break;
    		default:
    			strcat(txt_ptr, "\0");
    		}
    }  	
    
    /*my output
    \x
    \x\x
    \x\x\555
    \x\x\555\x
    */
    I guess this will do what u want. u need to just change X to 8 to display like \333\8\8 for 333,8,8

    ssharish2005
    Last edited by ssharish2005; 09-28-2006 at 04:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-30-2008, 02:48 PM
  2. Recursion: base case returning 1, function returning 0
    By yougene in forum C Programming
    Replies: 5
    Last Post: 09-07-2007, 05:38 PM