Thread: Can switches be nested?

  1. #1
    Registered User JM1082's Avatar
    Join Date
    Mar 2011
    Posts
    51

    Unhappy Can switches be nested?

    Hi,
    I'm trying to get a program started but seem to have fallen at the first hurdle. :-(

    Please find my code below. When executed it gets as far as the second switch statement then stops running, asking me to press any key to continue and closing the terminal window. Does anybody know if it's possible to nest switches in this way? Can you see where I'm going wrong?

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int month, first, i = 0;
    
        printf("Which month would you like to view?");
        scanf("%d", &month);
    
        switch (month) {
            case 1: printf("On which day does this month begin? ");
                    scanf("%d", &first);
    
                    printf("\n\nJanuary\n\nSun\tMon\tTue\tWed\tThu\tFri\tSat\n\n");
    
                    switch (first) {
                        case 1: printf("");
                                break;
                        case 2: printf("\t");
                                i = 1;
                                break;
                        case 3: printf("\t\t");
                                i = 2;
                                break;
                        case 4: printf("\t\t\t");
                                i = 3;
                                break;
                        case 5: printf("\t\t\t\t");
                                i = 4;
                                break;
                        case 6: printf("\t\t\t\t\t");
                                i = 5;
                                break;
                        case 7: printf("\t\t\t\t\t\t");
                                i = 6;
                                break;
                        default: printf("An error has occured, please restart the program!");
                                break;
                    }
                    for (; i > 31; i++) {
                        printf("%d\t", i);
    
                        if (i > 6) {
                        printf("\n\n");
                        }
                    }
        }
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps you meant
    i < 31
    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.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Don't see anything wrong with it unless the month to view is a no. not equal to 1.

  4. #4
    Registered User JM1082's Avatar
    Join Date
    Mar 2011
    Posts
    51
    Yes! i < 31! How embarassing.....

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    And to answer your main question... Yes, switch trees can be nested to almost any level.

  6. #6
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Quote Originally Posted by CommonTater View Post
    And to answer your main question... Yes, switch trees can be nested to almost any level.
    127 nested blocks and 1023 cases per switch are the limits described by C99 (5.2.4.1)

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by adeyblue View Post
    127 nested blocks and 1023 cases per switch are the limits described by C99 (5.2.4.1)
    Well... I did say "almost"...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with Nested Structures and Arrays of Structures
    By Ignoramus in forum C Programming
    Replies: 4
    Last Post: 03-02-2010, 01:24 AM
  2. Nested array vs. tree
    By KONI in forum Tech Board
    Replies: 1
    Last Post: 06-07-2007, 04:43 AM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  4. nested switch issue
    By fsu_altek in forum C Programming
    Replies: 3
    Last Post: 02-15-2006, 10:29 PM
  5. Nested Classes
    By manofsteel972 in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2004, 11:57 AM

Tags for this Thread