switch()

This is a discussion on switch() within the C Programming forums, part of the General Programming Boards category; I am a just learning c, and i was wondering whether i could create a switch without user input. example: ...

  1. #1
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110

    Exclamation switch()

    I am a just learning c, and i was wondering whether i could create a switch without user input.

    example:

    I have 7 numbers (k) 1 to 7

    I want the program to print the coresponding word for each k
    such as 1 = a, 2=b, 3=c etc.....

    This is what I came up so far but it is not working....I know how to do it with user input but not when the program generates the numbers!!!!!!

    for (k=1;k<8;k++);
    {
    switch(k)
    {
    case 1 : printf("a");
    }


  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    > for (k=1;k<8;k++);

    Removing the trailing ; will be a revelation then

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    Ha! there's a bracket missing at the bottom too.

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    I guess you want to know how to use the switch-statement? The switch-statement can be used like this:

    Code:
    switch (variable)
    {
        case VALUE_1:
            /* Actions */
            break;
        case VALUE_2:
            /* Actions */
            break;
        ....
        case VALUE_N:
            /* Actions */
            break;
        default:
            /* In case variable is not one of the values VALUE_1 to VALUE_N */
            /* Actions */
            break;
    }

  5. #5
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    It does work but i get a funny result

    Rather than printing abcdefg

    It is printing: abcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefg

    I have a feeling it has to do with the for loop but if I take it out the program will not run.

    Any suggestions!!!!!!!

  6. #6
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    Never mind I got it

    I just forgot to put break;

    Thanks all......

    But I need more help know that i know how to use a switch

    rather than printing each case how can I store the result and print at the end of the program with one single printf statement

    do I have to do something like this??

    char alpha[10];
    ...
    case 1: alpha='a';
    ...
    printf("%s\n", alpha );

  7. #7
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    That's partly correct, you forgot the index.

    Code:
    char string [N];
    
    ....
        string [index] = character;
    ....
    
    printf ("%s\n", string);
    In the code, the maximum size of the string is N, make sure that your code doesn't write beyond the end of the string.

  8. #8
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    thanks shiro


    What what goes into the index.

    I have never seen that before!!!!

    and what do I write after case 1: ......

  9. #9
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    If you have an array, then you can run to it using an index, like:

    Code:
    int array [N];
    int index;
    
    for (index = 0; index < N; index++)
    {
        /* Every element of the array is set to 1. */
        array [index] = 1;
    }
    An array is a block of memory which is divided in blocks of certain size, the size of the blocks depends on the type of the array. In the example, the type is int, so each block has size sizeof(int). There are N blocks, the first block has index 0, the last block has index N-1.

    >and what do I write after case 1: ......

    After case 1, you want to store the character 'a' in the array. You can do it like this.

    Code:
    case 1:
        string [index] = 'a';
        /* Now increase index so that the next character will be
            placed in the element next to where 'a' is stored. */
        index++;
        break;

  10. #10
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    wow..I am just getting a bit frustrated.......

    This is what I wrote so far: does it make any sence???

    int main(void)
    {
    int k;
    char letter[10];

    for(k=1;k<7;k++)
    {
    switch(k)
    {
    case 1 : letter[k]='a';
    case 2 : letter[k]='b';
    ...
    k++;
    break;
    }
    }
    printf("%s\n",letter);

    return 0;
    }

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Max

    This is what I wrote so far: does it make any sence???

    <snip>
    You're getting there.
    Here's a working version, based on yours.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int     k;
        char    letter[10];
    
        for (k = 0; k < 7; k++)
        {
            switch (k)
            {
    	        case 1: letter[k] = 'a'; break;
    	        case 2: letter[k] = 'b'; break;
    	        case 3: letter[k] = 'c'; break;
    	        case 4: letter[k] = 'd'; break;
    	        case 5: letter[k] = 'e'; break;
    	        case 6: letter[k] = 'f'; break;
    	        default: letter[k] = ' '; break; 
            }
        }
    
        for (k = 0; k < 7; k++)
        {
        	putchar(letter[k]);
        }
    
        return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I'm not sure if this code you have been using is just an example or whether you are planning on using it for some program but I wouldn't suggest using a switch statement like you are doing here.

    Code:
    int main(void)
    {
        int     k;
        char    letter[10];
        const char better_than_switch[] = "abcdef ";
    
     
        for (k = 0; k < 7; k++)
        {
              letter[k] = better_than_switch[k];
        }
    
        for (k = 0; k < 7; k++)
        {
        	putchar(letter[k]);
        }
    
        return(0);
    }
    Using switches like that is not only more expensive but it takes just a little longer to program.

    You could also have the for loop look like this:
    Code:
        for (k = 0; k < 7; k++)
        {
              if(k == 7)
                    letter[k] = ' ';
              else
                    letter[k] = k;
        }

  13. #13
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    thank you hammer your code works

    but what I am trying to do now is replace abcdef with names
    the compiler hate long words it won't allow me to compile, so I defined all letters with names

    I also want to replace putchar with printf

    the program will not execute....what could be wrong?????

    this is what I have:

    #include <stdio.h>
    #define a Jeff
    #define b Bob
    #define c Catherine
    #define d David
    #define e Emilie
    #define f Francis

    int main(void)
    {
    int k;
    char letter[130];

    for (k = 0; k < 7; k++)
    {
    switch(k)
    {
    case 1: letter[k] = 'a'; break;
    case 2: letter[k] = 'b'; break;
    case 3: letter[k] = 'c'; break;
    case 4: letter[k] = 'd'; break;
    case 5: letter[k] = 'e'; break;
    case 6: letter[k] = 'f'; break;
    default: letter[k] = ' '; break;
    }
    }
    printf("%s\n",letter);

    return(0);
    }

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) You never null terminate you string, so printing it with 'printf' will give you wierd behavior.

    2) Your #defines are not set right:

    #define VALUEA "MyNameString"

    Then, any place in your code where you use VALUEA it will replace it with "MyNameString" -- unless VALUEA is in quotes of any kind.

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    thanks quzah.....

    but how do you place a null terminator in a string

    I am asumming I should do it only once in letter array

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  3. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 05:40 PM
  4. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21