Switch-case problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chih-Hsu Yen

    #1

    Switch-case problem

    I encountered a strange problem about switch-case statement.
    switch(cmd)
    {
    case 1: statements; break;
    case 2: statements; break;
    ... ....
    case 11: S1;
    S2;
    S3;
    statements;
    break;
    ... ...
    case xx
    }

    When the cmd is above 11, the first two statements are skipped. Take case 11
    as example
    the first executing statement is S3, not S1.
    What the problem is?


  • Richard Bos

    #2
    Re: Switch-case problem

    "Chih-Hsu Yen" <zsyian@ms14.hi net.net> wrote:
    [color=blue]
    > I encountered a strange problem about switch-case statement.
    > switch(cmd)
    > {
    > case 1: statements; break;
    > case 2: statements; break;
    > ... ....
    > case 11: S1;
    > S2;
    > S3;
    > statements;
    > break;
    > ... ...
    > case xx
    > }
    >
    > When the cmd is above 11, the first two statements are skipped. Take case 11
    > as example
    > the first executing statement is S3, not S1.
    > What the problem is?[/color]

    I've no idea, and it would be very hard to tell without seeing your
    actual code. For example, what _are_ S1, S2, and S3? How is cmd defined?
    What is the case just before case 11? Show your real code, and we may be
    able to help.

    Richard

    Comment

    • Chih-Hsu Yen

      #3
      Re: Switch-case problem

      Most cases have analogous format as case 10, so I only pasted case 10.
      It seems that the first two statements are skipped, no matter what they are,
      while the case number is above 10.
      Therefore, I thought that the problem may be caused by the codes in case 10,
      but I can not figure out how could this happen.

      -------------
      scanf("%d",&cmd );

      switch(cmd){
      case 1: similar as case 10

      case 2: similar as case 10

      case 3: similar as case 10

      case 10: /*insert inbound SPD*/
      spd_udp.opcode = SPD_IN_INSERT;
      printf("please input the addr1 & addr2 (XX XX, XX=00-3F): ");
      scanf("%x %x",&addr1,&add r2);
      printf("please select an action : \n");
      printf("1: bypass \n");
      printf("2: IPsec \n");
      printf("3: disard \n");
      scanf("%d",&sel );
      if(sel == 1)
      {
      entry_spd.ctrl. action_index = 0;
      entry_spd.ctrl. next_index = 0;
      }
      else if(sel == 2)
      {
      entry_spd.ctrl. action_index = 64;
      entry_spd.ctrl. next_index = 0;
      }
      else if(sel == 3)
      {
      entry_spd.ctrl. action_index = 128;
      entry_spd.ctrl. next_index = 0;
      }
      else
      {
      printf("error selection!! \n");
      break;
      }
      spd_udp.oper.op erand_addr1 = addr1;
      spd_udp.oper.op erand_addr2 = addr2;
      sendstring[0] = spd_udp.opcode;
      sendstring[1] = spd_udp.oper.op erand_addr1;
      sendstring[2] = spd_udp.oper.op erand_addr2;
      sendstring[3] = entry_spd.ctrl. action_index;
      sendstring[4] = entry_spd.ctrl. next_index;
      sendstring[5] = 0;
      sendstring[6] = entry_spd.proto col;
      printf("please input sip value (XX XX XX XX, XX=00-FF): ");
      scanf("%x %x %x %x", &b[0], &b[1], &b[2], &b[3]);
      entry_spd.sip = word_build(b[0],b[1],b[2],b[3]);
      for (i=7; i<11; i++)
      {
      sendstring[i] = b[i-7];
      }
      printf("please input sip Mask value (XX XX XX XX, XX=00-FF): ");
      scanf("%x %x %x %x", &b[0], &b[1], &b[2], &b[3]);
      entry_spd.sipMa sk = word_build(b[0],b[1],b[2],b[3]);
      for (i=11; i<15; i++)
      {
      sendstring[i] = b[i-11];
      }
      printf("please input dip value (XX XX XX XX, XX=00-FF): ");
      scanf("%x %x %x %x", &b[0], &b[1], &b[2], &b[3]);
      entry_spd.dip = word_build(b[0],b[1],b[2],b[3]);
      for (i=15; i<19; i++)
      {
      sendstring[i] = b[i-16];
      }
      printf("please input dip Mask value (XX XX XX XX, XX=00-FF): ");
      scanf("%x %x %x %x", &b[0], &b[1], &b[2], &b[3]);
      entry_spd.dipMa sk = word_build(b[0],b[1],b[2],b[3]);
      for (i=19; i<23; i++)
      {
      sendstring[i] = b[i-19];
      }
      printf("please input spMaxMin value (XX XX XX XX, XX=00-FF): ");
      scanf("%x %x %x %x", &b[0], &b[1], &b[2], &b[3]);;
      entry_spd.spMax Min = word_build(b[0],b[1],b[2],b[3]);
      for (i=23; i<27; i++)
      {
      sendstring[i] = b[i-23];
      }
      printf("please input dpMaxMin value (XX XX XX XX, XX=00-FF): ");
      scanf("%x %x %x %x", &b[0], &b[1], &b[2], &b[3]);
      entry_spd.dpMax Min = word_build(b[0],b[1],b[2],b[3]);
      for (i=27; i<31; i++)
      {
      sendstring[i] = b[i-27];
      }
      insert_isp(spd_ udp.oper,entry_ spd);
      sendsocket(send string,SPD_INTP AC_LEN);
      c=getchar();
      break;
      }


      "Richard Bos" <rlb@hoekstra-uitgeverij.nl> ¼¶¼g©ó¶l¥ó·s»D: 425b6d32.772019 58@news.xs4all. nl...[color=blue]
      > "Chih-Hsu Yen" <zsyian@ms14.hi net.net> wrote:
      >[color=green]
      >> I encountered a strange problem about switch-case statement.
      >> switch(cmd)
      >> {
      >> case 1: statements; break;
      >> case 2: statements; break;
      >> ... ....
      >> case 11: S1;
      >> S2;
      >> S3;
      >> statements;
      >> break;
      >> ... ...
      >> case xx
      >> }
      >>
      >> When the cmd is above 11, the first two statements are skipped. Take case
      >> 11
      >> as example
      >> the first executing statement is S3, not S1.
      >> What the problem is?[/color]
      >
      > I've no idea, and it would be very hard to tell without seeing your
      > actual code. For example, what _are_ S1, S2, and S3? How is cmd defined?
      > What is the case just before case 11? Show your real code, and we may be
      > able to help.
      >
      > Richard[/color]


      Comment

      • Minti

        #4
        Re: Switch-case problem


        Chih-Hsu Yen wrote:[color=blue]
        > Most cases have analogous format as case 10, so I only pasted case[/color]
        10.[color=blue]
        > It seems that the first two statements are skipped, no matter what[/color]
        they are,[color=blue]
        > while the case number is above 10.
        > Therefore, I thought that the problem may be caused by the codes in[/color]
        case 10,[color=blue]
        > but I can not figure out how could this happen.[/color]

        Could you by any means step through your code and see what is going on?


        <snip code>

        --
        Imanpreet Singh Arora

        Comment

        • Chih-Hsu Yen

          #5
          Re: Switch-case problem

          It's really strange. I marked all statements as comments and left "break"
          statement only in case 10.
          However, the first two statements of case 11 are still skipped.
          But, if I remove all statements and left "break" statement in case 10.
          The execution of case 11 is correct.
          Do there have restrictions on the number of lines in switch-case statement,
          no matter what they are statements or comments?

          "Minti" <imanpreet@gmai l.com>
          ???????:1113289 298.345400.9342 0@z14g2000cwz.g ooglegroups.com ...[color=blue]
          >
          > Chih-Hsu Yen wrote:[color=green]
          >> Most cases have analogous format as case 10, so I only pasted case[/color]
          > 10.[color=green]
          >> It seems that the first two statements are skipped, no matter what[/color]
          > they are,[color=green]
          >> while the case number is above 10.
          >> Therefore, I thought that the problem may be caused by the codes in[/color]
          > case 10,[color=green]
          >> but I can not figure out how could this happen.[/color]
          >
          > Could you by any means step through your code and see what is going on?
          >
          >
          > <snip code>
          >
          > --
          > Imanpreet Singh Arora
          >[/color]


          Comment

          • CBFalconer

            #6
            Re: Switch-case problem

            *** top-posting corrected ***
            Chih-Hsu Yen wrote:[color=blue]
            > "Minti" <imanpreet@gmai l.com>[color=green]
            >> Chih-Hsu Yen wrote:[/color]
            >[color=green][color=darkred]
            >>> Most cases have analogous format as case 10, so I only pasted
            >>> case 10. It seems that the first two statements are skipped, no
            >>> matter what they are, while the case number is above 10.
            >>> Therefore, I thought that the problem may be caused by the codes
            >>> in case 10, but I can not figure out how could this happen.[/color]
            >>
            >> Could you by any means step through your code and see what is
            >> going on?[/color]
            >
            > It's really strange. I marked all statements as comments and left
            > "break" statement only in case 10. However, the first two
            > statements of case 11 are still skipped. But, if I remove all
            > statements and left "break" statement in case 10. The execution
            > of case 11 is correct. Do there have restrictions on the number
            > of lines in switch-case statement, no matter what they are
            > statements or comments?[/color]

            You almost certainly have undefined behaviour somewhere, which
            might even be writing into a jump table. Simplify your code so
            that errors stand out. That usually means splitting off
            subroutines to do jobs.

            Please do not top-post. Your answer belongs after, or intermixed
            with, the material to which you reply, with non-germane portions
            snipped out. Top-posting is generally considered rude, boorish and
            inconsiderate.

            --
            "If you want to post a followup via groups.google.c om, don't use
            the broken "Reply" link at the bottom of the article. Click on
            "show options" at the top of the article, then click on the
            "Reply" at the bottom of the article headers." - Keith Thompson

            Comment

            • Bjoern Pedersen

              #7
              Re: Switch-case problem

              "Chih-Hsu Yen" <zsyian@ms14.hi net.net> writes:
              [color=blue]
              > Most cases have analogous format as case 10, so I only pasted case 10.
              > It seems that the first two statements are skipped, no matter what they are,
              > while the case number is above 10.
              > Therefore, I thought that the problem may be caused by the codes in case 10,
              > but I can not figure out how could this happen.
              >
              > -------------
              > scanf("%d",&cmd );[/color]

              now a "\n" is still in the input stream...
              [color=blue]
              > switch(cmd){
              > case 1: similar as case 10
              >
              > case 2: similar as case 10
              >
              > case 3: similar as case 10
              >
              > case 10: /*insert inbound SPD*/
              > spd_udp.opcode = SPD_IN_INSERT;
              > printf("please input the addr1 & addr2 (XX XX, XX=00-3F): ");
              > scanf("%x %x",&addr1,&add r2);[/color]

              this scanf is "skipped" due to the "\n" in the input stream...

              .... an so on

              Björn
              --
              Bjoern Pedersen

              Comment

              • Minti

                #8
                Re: Switch-case problem


                Bjoern Pedersen wrote:[color=blue]
                > "Chih-Hsu Yen" <zsyian@ms14.hi net.net> writes:
                >[color=green]
                > > Most cases have analogous format as case 10, so I only pasted case[/color][/color]
                10.[color=blue][color=green]
                > > It seems that the first two statements are skipped, no matter what[/color][/color]
                they are,[color=blue][color=green]
                > > while the case number is above 10.
                > > Therefore, I thought that the problem may be caused by the codes in[/color][/color]
                case 10,[color=blue][color=green]
                > > but I can not figure out how could this happen.
                > >
                > > -------------
                > > scanf("%d",&cmd );[/color]
                >
                > now a "\n" is still in the input stream...
                >[color=green]
                > > switch(cmd){
                > > case 1: similar as case 10
                > >
                > > case 2: similar as case 10
                > >
                > > case 3: similar as case 10
                > >
                > > case 10: /*insert inbound SPD*/
                > > spd_udp.opcode = SPD_IN_INSERT;
                > > printf("please input the addr1 & addr2 (XX XX, XX=00-3F): ");
                > > scanf("%x %x",&addr1,&add r2);[/color]
                >
                > this scanf is "skipped" due to the "\n" in the input stream...[/color]

                Nopes, the scanf here won't be skipped. It would be skipped only if we
                were taking a character which it isn't taking.

                It seems to be different problem altogether. I believe that the OP is,
                just doing something else that makes him believe that the the
                statements are skipped.

                --
                Imanpreet Singh Arora

                Comment

                • Carlos

                  #9
                  Re: Switch-case problem

                  Chih-Hsu Yen wrote:[color=blue]
                  > Most cases have analogous format as case 10, so I only pasted case 10.
                  > It seems that the first two statements are skipped, no matter what they are,
                  > while the case number is above 10.
                  > Therefore, I thought that the problem may be caused by the codes in case 10,
                  > but I can not figure out how could this happen.[/color]

                  [...][color=blue]
                  > switch(cmd){
                  > case 1: similar as case 10
                  >
                  > case 2: similar as case 10
                  >
                  > case 3: similar as case 10
                  >
                  > case 10: /*insert inbound SPD*/
                  > spd_udp.opcode = SPD_IN_INSERT;[/color]
                  [...][color=blue]
                  > sendsocket(send string,SPD_INTP AC_LEN);
                  > c=getchar();
                  > break;
                  > }[/color]

                  Did you really close your switch after case 10, or can we assume that
                  case 11 starts after the break and before the brace?

                  Comment

                  • Michael Wojcik

                    #10
                    Re: Switch-case problem


                    In article <m3u0mc5ow3.fsf @resi2.office.f rm2>, Bjoern Pedersen <Bjoern.Pederse n@frm2.tum.de> writes:[color=blue]
                    > "Chih-Hsu Yen" <zsyian@ms14.hi net.net> writes:
                    >[color=green]
                    > > case 10: /*insert inbound SPD*/
                    > > spd_udp.opcode = SPD_IN_INSERT;
                    > > printf("please input the addr1 & addr2 (XX XX, XX=00-3F): ");
                    > > scanf("%x %x",&addr1,&add r2);[/color]
                    >
                    > this scanf is "skipped" due to the "\n" in the input stream...[/color]

                    And the output from the printf will likely not have appeared yet,
                    because stdout is probably in line-buffering mode, no newline has
                    been written, and stdout has not been flushed.

                    We might have more confidence in claims about statements being
                    "skipped" if they were written to have visible effects, and if you
                    checked whether they failed. (scanf returns a value for a reason.)

                    C seems to attract the sort of programmer who throws code at a
                    problem and hopes it will all work correctly. This approach does not
                    always produce ideal results.

                    --
                    Michael Wojcik michael.wojcik@ microfocus.com

                    The antics which have been drawn together in this book are huddled here
                    for mutual protection like sheep. If they had half a wit apiece each
                    would bound off in many directions, to unsimplify the target. -- Walt Kelly

                    Comment

                    • Chih-Hsu Yen

                      #11
                      Re: Switch-case problem

                      "Carlos" <angus@quovadis .com.ar> ???????:d3hdih$ b30$1@domitilla .aioe.org...[color=blue]
                      > Chih-Hsu Yen wrote:[color=green]
                      >> Most cases have analogous format as case 10, so I only pasted case 10.
                      >> It seems that the first two statements are skipped, no matter what they
                      >> are, while the case number is above 10.
                      >> Therefore, I thought that the problem may be caused by the codes in case
                      >> 10,
                      >> but I can not figure out how could this happen.[/color]
                      >
                      > [...][color=green]
                      >> switch(cmd){
                      >> case 1: similar as case 10
                      >>
                      >> case 2: similar as case 10
                      >>
                      >> case 3: similar as case 10
                      >>
                      >> case 10: /*insert inbound SPD*/
                      >> spd_udp.opcode = SPD_IN_INSERT;[/color]
                      > [...][color=green]
                      >> sendsocket(send string,SPD_INTP AC_LEN);
                      >> c=getchar();
                      >> break;
                      >> }[/color]
                      >
                      > Did you really close your switch after case 10, or can we assume that case
                      > 11 starts after the break and before the brace?[/color]

                      I solved the problem by deleting "sendsocket(sen dstring, SPD_INTPAC_LEN) ;"
                      and retyping it again.
                      I still can not figure out what happened, but it is fixed.


                      Comment

                      Working...