Mutli Switch statement help

This is a discussion on Mutli Switch statement help within the C Programming forums, part of the General Programming Boards category; i m trying to code a case within a case in a switch statement if thats possible but i keep ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    187

    Mutli Switch statement help

    i m trying to code a case within a case in a switch statement if thats possible
    but i keep getting all result and if i put a break within them it will only show first statement only
    Code:
    #include <stdio.h>
    int question_him(char *question)
    {
        int result;
        puts(question);
        scanf("%d",&result);
        return result;
    }
    char *returner(char *returner)
    {
         return returner;
    }
    int main(void)
    {
        int result;
        char *buffer;
        for(;;) {
            result=ask_it("Hello here is your menu\n"
                          "1)FILE functions\n");
            switch(result)
            {
                case 1:
                     result=ask_it("Youve choosen FILE functions\n"
                                   "1) write it down\n"
                                   "2) read it\n"
                                   "3) append it\n");
                case 'w':
                     puts("write it down\n");
                     buffer=returner("Youve choosen write it down option");
                case 'r':
                     puts("you have choosen read it option \n");
                     buffer=returner("Youve choosen read it option");
                case 'a':
                     puts("You have choosen append it\n");
                     buffer=returner("Youve choosen append it option\n");
                     break;
            }
            break;
        }
        puts(buffer);
        return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're not explaining what you actually want, or rather, your code doesn't match up with what you're implying you want. Anyway...
    Code:
    while( choice != 'N' && choice != 'n' )
    {
    switch( (choice = getchoice()) )
    {
        case 'Y': case 'y':
        {
            printf( "You made your choice, now live with it.\n" );
            while( choice2 != 'o' )
            {
                switch( (choice2 = getchoice()) )
                {
                    case 'x':
                        printf( "You die.\n" );
                        choice = 'n'; /* make it drop from the outer loop too... */
                    break;
                    default: /* whatever */ break;
                }
            }
        }
        break;
    
        default: /* do nothing */ break;
    }
    I don't know what it is you're trying to do, but you can pretty much nest anything (not functions) if you have enough braces.


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

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    i tried but program crashed
    Code:
    #include <stdio.h>
    int ask_it(char *question)
    {
        int result;
        puts(question);
        scanf("%d",&result);
        return result;
    }
    char *func(char *lol)
    {
         return lol;
    }
    int main(void)
    {
        int result;
        char *buffer;
        for(;;) {
            result=ask_it("Hello here is your menu\n"
                          "1)FILE functions\n");
            switch(result)
            {
                case 1:
                     result=ask_it("Youve choosen FILE functions\n"
                                   "1) write it down\n"
                                   "2) read it\n"
                                   "3) append it\n");
                     switch(result){
                         case 'w':
                              puts("Youve choosen write it down");
                              buffer=func("Youve choosen write it down");
                              break;
                         case 'r':
                              puts("Youve choosen read it option");
                              buffer=func("Youve chossen read it option");
                              break;
                         case 'a':
                              puts("Youve choosen append it option");
                              buffer=func("youve choosen append it option");
                              break;
                     }
            }
            break;
        }
        puts(buffer);
        return 0;
    }
    it crashed i think because buffer doesnt got anything but why shouldnt that statement scanf result then reswitch the statement again ?

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by elwad View Post
    i tried but program crashed
    Code:
    #include <stdio.h>
    int ask_it(char *question)
    {
        int result;
        puts(question);
        scanf("%d",&result);
        return result;
    }
    char *func(char *lol)
    {
         return lol;
    }
    int main(void)
    {
        int result;
        char *buffer;
        for(;;) {
            result=ask_it("Hello here is your menu\n"
                          "1)FILE functions\n");
            switch(result)
            {
                case 1:
                     result=ask_it("Youve choosen FILE functions\n"
                                   "1) write it down\n"
                                   "2) read it\n"
                                   "3) append it\n");
                     switch(result){
                         case 1:
                              puts("Youve choosen write it down");
                              buffer=func("Youve choosen write it down");
                              break;
                         case 2:
                              puts("Youve choosen read it option");
                              buffer=func("Youve chossen read it option");
                              break;
                         case 3:
                              puts("Youve choosen append it option");
                              buffer=func("youve choosen append it option");
                              break;
                     }
            }
            break;
        }
        puts(buffer);
        return 0;
    }
    it crashed i think because buffer doesnt got anything but why shouldnt that statement scanf result then reswitch the statement again ?
    I've modified your code. It'll work fine.
    In your function you were reading the cases as 1,2,3 while you were taking the case labels to be 'w','r','a'.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    i thought i can casted it inside the switch

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    He means this:
    Code:
                    result=ask_it("Youve choosen FILE functions\n"
                                   "1) write it down\n"
                                   "2) read it\n"
                                   "3) append it\n");
                     switch(result){
                         case 'w':
    Why ask them to enter 1 if you are going to check for W?


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

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    anyways i fixed it
    Code:
    #include <stdio.h>
    int ask_it(int bol,char *question)
    {
        int result;
        puts(question);
        scanf("%d",&result);
        if(bol){
            scanf("%c",&result);
            return (char)result;
            }
        return result;
    }
    char *func(char *lol)
    {
         return lol;
    }
    int main(void)
    {
        int result;
        char *buffer;
        for(;;) {
            result=ask_it(0,"Hello here is your menu\n"
                          "1)FILE functions\n");
            switch(result)
            {
                case 1:
                     result=ask_it(1,"Youve choosen FILE functions\n"
                                   "1) write it down\n"
                                   "2) read it\n"
                                   "3) append it\n");
                     switch(result){
                         case 'w':
                              puts("Youve choosen write it down");
                              buffer=func("Youve choosen write it down");
                              break;
                         case 'r':
                              puts("Youve choosen read it option");
                              buffer=func("Youve chossen read it option");
                              break;
                         case 'a':
                              puts("Youve choosen append it option");
                              buffer=func("youve choosen append it option");
                              break;
                         default:
                                 puts("Sorry I dont understand your choice please retry :P");
                                 continue;
                     }
            }
            break;
        }
        puts(buffer);
        return 0;
    }

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    oh yah that was misspelling in writing stupid me lol

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    anyway thanks alot for the help mate

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It is OFTEN a good idea to split nested switch statements into their own functions - that way, it's easier to follow the code, and it's much less likely to get the overall switch enormous. There are times when this is NOT a good plan, but that's the exception rather than the rule.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 02:14 PM
  2. switch statement
    By guillermoh in forum C Programming
    Replies: 5
    Last Post: 03-10-2008, 02:17 PM
  3. switch statement issues...(undeclared identifiers)
    By mero24 in forum C++ Programming
    Replies: 2
    Last Post: 02-19-2005, 07:05 PM
  4. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 03:49 PM
  5. Efficiency with the switch() statement...
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2001, 02:47 PM

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