Thread: Question about switch cases

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    26

    Question about switch cases

    When you have a list of cases in a switch function can you nest ifs and things of that nature in a single case?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sure. You can have switch statements with if, while, for, do-while, function calls, local variable declarations, or any other piece of C that you could put inside a function in C in general.

    It is, however, advisable to keep the code in the case short, preferrably just calling a function that does the real work.

    If you use local variables in a specific case, make sure you put braces around it:
    Code:
     ... 
        switch(x) 
        {
           case 1:
              ...
              break;
           case 2:
              {
                  int y;
                  y = 4;
                  while (y < z) y << 2;
                  ...
               }
               break;
    ....
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    Alright, I nested some ifs else ifs into the switch, which isn't a short series like you advised but I don't know how else to do it.

    The program I'm writing allows the user to input a two digit number and it prints out the english word for the number.

    I can get all cases to work except for 11-19, which prints the work, in addition to the second digit. For example, if you input 19, it prints:

    ninteen
    nine

    I know the problem is the first switch is working fine but when the second switch isn't necessary, its printing anyway. I can't figure out how to ignore the second switch if it isn't required.

    Code:
    // This program takes a two digit number from the user and
    // prints the English word for the number
    
    #include <stdio.h>
    
    main (void)
    {
    
            int num, first, second;
    
            printf("Enter a two-digit number: ");
            scanf("%d", &num);
    
            if (num < 10 || num > 99) {
                 printf("Incorrect input: Please enter a two-digit number \n");
    
            }
            else {
            first = num / 10;
            second = num%10;
    
            switch (first) {
                 case 9: printf("ninety"); break;
                 case 8: printf("eighty"); break;
                 case 7: printf("seventy"); break;
                 case 6: printf("sixty"); break;
                 case 5: printf("fifty"); break;
                 case 4: printf("forty"); break;
                 case 3: printf("thirty"); break;
                 case 2: printf("twenty"); break;
                 case 1: if (first == 1 && second == 0) {
                                printf("ten \n");
                            }
                             else if (first == 1 && second == 1) {
                             printf("eleven \n");
                            }
                            else if (first == 1 && second == 2) {
                                 printf("twelve \n");
                            }
                            else if (first == 1 && second == 3) {
                                 printf("thirteen \n");
                            }
                            else if (first == 1 && second == 4) {
                                 printf("fourteen \n");
                            }
                            else if (first == 1 && second == 5) {
                                 printf("fifteen \n");
                            }
                            else if (first == 1 && second == 6) {
                                 printf("sixteen \n");
                            }
                             else if (first == 1 && second == 7) {
                                 printf("seventeen \n");
                            }
                            else if (first == 1 && second == 8) {
                                 printf("eighteen \n");
                            }
                            else if (first == 1 && second == 9) {
                                 printf("nineteen \n");
                            }
                            break;
            }
    
            switch (second) {
                 case 9: printf("-nine \n"); break;
                 case 8: printf("-eight \n"); break;
                 case 7: printf("-seven \n"); break;
                 case 6: printf("-six \n"); break;
                 case 5: printf("-five \n"); break;
                 case 4: printf("-four \n"); break;
                 case 3: printf("-three \n"); break;
                 case 2: printf("-two \n"); break;
                 case 1: printf("-one \n"); break;
    
            }
    
    
    
    
            }
            return 0;
    }
    Any ideas?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to have some sort of if-statement around (or inside) your second switch. I think
    Code:
    if (first != 1) ...
    would do.

    Of course, you should probably use a switch inside case 1 in the first switch statement - and you definitely don't need the
    Code:
    ... first == 1 && ...
    sections, because you are in the "1" case for "first", so no other value should end up here.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    That works perfect, thanks man. That was driving me nuts.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Looks like a long winded way of implementing an array to me.
    Unless switch/case was the point of the exercise.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using a character array in a switch question.
    By bajanElf in forum C Programming
    Replies: 10
    Last Post: 11-08-2008, 08:06 AM
  2. noob question re: switch case & loops
    By scooglygoogly in forum C++ Programming
    Replies: 13
    Last Post: 08-25-2008, 11:08 AM
  3. switch() inside while()
    By Bleech in forum C Programming
    Replies: 7
    Last Post: 04-23-2006, 02:34 AM
  4. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM
  5. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM