switch expression not integral ???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bayxarea-usenet@yahoo.com

    #1

    switch expression not integral ???

    I am getting the following error during compiling:

    'switch expression not integral'

    I am new to the switch command - here is a snip of my code where I have
    used this.. .. what is the problem here? Thanks!

    ------------- snip --------------------------

    for (ii = 0; ii < n; ii++)
    {
    if (!UF_OBJ_ask_na me(members[ii], obj_name))
    {
    sprintf(msg,"\n Object #%d has an object tag = %d and is named
    %s,",ii,member s[ii],obj_name);
    WRITE(msg);

    UF_CALL(UF_DRF_ ask_draft_aid_t ext_info(member s[ii], &nn,
    &text_info)) ;

    for (iii=0; iii<nn; iii++)
    {
    WRITE3F(text_in fo[iii].origin);
    for (jj=0; jj<text_info[iii].num_lines; jj++)
    {
    sprintf(msg, " %s\n", text_info[iii].text[jj].string);
    UF_UI_write_lis ting_window( msg );
    }
    }
    UF_CALL(UF_DRF_ free_text(nn, &text_info)) ;
    switch (obj_name)
    {
    case "NOTE TEXT CP SL":
    uc5566(members[ii], 1, 6, "J");
    break;
    case "NOTE TEXT CP COC":
    uc5566(members[ii], 1, 6, "O");
    break;
    case "NOTE TEXT CP PQP":
    uc5566(members[ii], 1, 6, "H");
    break;
    case "NOTE TEXT CP FRD":
    uc5566(members[ii], 1, 6, "N);
    break;
    case "NOTE TEXT CP SC":
    uc5566(members[ii], 1, 6, "P);
    break;
    default:
    break;
    }

    }
    }

  • Ulrich Eckhardt

    #2
    Re: switch expression not integral ???

    bayxarea-usenet@yahoo.co m wrote:[color=blue]
    > I am getting the following error during compiling:
    >
    > 'switch expression not integral'[/color]

    The argument to switch must be integral. It can't be a floating point
    value, a structure or a pointer (including char pointers aka strings).
    [color=blue]
    > I am new to the switch command - here is a snip of my code where I have
    > used this.. .. what is the problem here? Thanks![/color]

    Please reduce your examples to the smallest piece of code that demonstrates
    your problem.

    Uli

    Comment

    • Michael Mair

      #3
      Re: switch expression not integral ???

      bayxarea-usenet@yahoo.co m wrote:[color=blue]
      > I am getting the following error during compiling:
      >
      > 'switch expression not integral'
      >
      > I am new to the switch command - here is a snip of my code where I have
      > used this.. .. what is the problem here? Thanks![/color]

      Well, switch can only be applied to integer data types, i.e
      char, (un)signed char, (un)signed short, (un)signed int,
      (un)signed long (, size_t, ..., C99: (un)signed long long,
      the int*_t types, ptrdiff_t, ...) but not to a pointer
      -- at least I guess that obj_name is either of type char *
      or of type array of char.

      [color=blue]
      > ------------- snip --------------------------
      >
      > for (ii = 0; ii < n; ii++)
      > {
      > if (!UF_OBJ_ask_na me(members[ii], obj_name))
      > {
      > sprintf(msg,"\n Object #%d has an object tag = %d and is named
      > %s,",ii,member s[ii],obj_name);
      > WRITE(msg);
      >
      > UF_CALL(UF_DRF_ ask_draft_aid_t ext_info(member s[ii], &nn,
      > &text_info)) ;
      >
      > for (iii=0; iii<nn; iii++)
      > {
      > WRITE3F(text_in fo[iii].origin);
      > for (jj=0; jj<text_info[iii].num_lines; jj++)
      > {
      > sprintf(msg, " %s\n", text_info[iii].text[jj].string);
      > UF_UI_write_lis ting_window( msg );
      > }
      > }
      > UF_CALL(UF_DRF_ free_text(nn, &text_info)) ;
      > switch (obj_name)
      > {
      > case "NOTE TEXT CP SL":
      > uc5566(members[ii], 1, 6, "J");
      > break;
      > case "NOTE TEXT CP COC":
      > uc5566(members[ii], 1, 6, "O");
      > break;
      > case "NOTE TEXT CP PQP":
      > uc5566(members[ii], 1, 6, "H");
      > break;
      > case "NOTE TEXT CP FRD":
      > uc5566(members[ii], 1, 6, "N);
      > break;
      > case "NOTE TEXT CP SC":
      > uc5566(members[ii], 1, 6, "P);
      > break;
      > default:
      > break;
      > }
      >
      > }
      > }[/color]

      A workaround: Apply switch to the first significant
      character...
      switch (obj_name[13]) {
      case 'S':
      /* Either SC or SL */
      break;
      case 'C':
      ....
      }

      Two things for future requests:
      * Please give us a minimal example which we can directly
      feed to a compiler.
      * Indent your code. Not with tabs but with spaces.


      Cheers
      Michael
      --
      E-Mail: Mine is an /at/ gmx /dot/ de address.

      Comment

      • Andrey Tarasevich

        #4
        Re: switch expression not integral ???

        bayxarea-usenet@yahoo.co m wrote:[color=blue]
        > I am getting the following error during compiling:
        >
        > 'switch expression not integral'
        >
        > I am new to the switch command - here is a snip of my code where I have
        > used this.. .. what is the problem here? Thanks![/color]

        The problem here is just what the compiler said: 'switch expression not
        integral'. In C language switch argument as well as case labels must be
        integral expressions. You seem to be trying to use pointer expressions
        ('char*') instead. This will not work. If you want to build a multiway
        dispatcher based on string argument, one way to go is to use 'if' ladder

        if (strcmp(obj_nam e, "NOTE TEXT CP SL") == 0)
        uc5566(members[ii], 1, 6, "J");
        else if (strcmp(obj_nam e, "NOTE TEXT CP COC") == 0)
        uc5566(members[ii], 1, 6, "O");
        else if (strcmp(obj_nam e, "NOTE TEXT CP PQP") == 0)
        uc5566(members[ii], 1, 6, "H");
        /* ... and so on */

        Switch is not applicable here. At least, not immediately applicable.

        --
        Best regards,
        Andrey Tarasevich

        Comment

        • bayxarea-usenet@yahoo.com

          #5
          Re: switch expression not integral ???

          Thanks for all the replies - I did not realize it was only a test for
          integers - didn't realize that integral meant integer actually.

          I will post a more usable snippet in the future...

          Thanks again..

          John

          Comment

          • Mike Wahler

            #6
            Re: switch expression not integral ???


            <bayxarea-usenet@yahoo.co m> wrote in message
            news:1102974969 .522711.34080@f 14g2000cwb.goog legroups.com...[color=blue]
            > Thanks for all the replies - I did not realize it was only a test for
            > integers[/color]

            Actually, the type of the 'case' expression is even more restricted
            than that. The expression must be a constant integer expression.

            int main()
            {
            int x = 42;
            int y = 99;

            switch(x)
            {
            case y: /* ERROR: not constant expression */
            break;
            case 99: /* OK: constant expression */
            break;
            default:
            break;
            }

            return 0;
            }
            [color=blue]
            >- didn't realize that integral meant integer actually.[/color]

            Now you know. :-)

            -Mike


            Comment

            • Sniper1

              #7
              Re: switch expression not integral ???

              In article <1102974969.522 711.34080@f14g2 000cwb.googlegr oups.com>,
              bayxarea-usenet@yahoo.co m says...[color=blue]
              > Thanks for all the replies - I did not realize it was only a test for
              > integers - didn't realize that integral meant integer actually.[/color]

              Where you thinking it meant the area under the switch curve? :-)

              Comment

              Working...