case statement,

Discussion in 'C++' started by vsgdp, Sep 15, 2006.

  1. vsgdp

    vsgdp Guest

    Why can't I define variables in a case statement:

    case 1:
    float u = expression;
    ...
    break;


    Also, is it bad form to cast ints to enums? For example, one might want 11
    to map to an enum element NOVEMBER.
     
    vsgdp, Sep 15, 2006
    #1
    1. Advertising

  2. vsgdp

    Jim Langston Guest

    "vsgdp" <> wrote in message
    news:u8nOg.9542$cw.2165@fed1read03...
    > Why can't I define variables in a case statement:
    >
    > case 1:
    > float u = expression;
    > ...
    > break;
    >
    >
    > Also, is it bad form to cast ints to enums? For example, one might want
    > 11 to map to an enum element NOVEMBER.


    case 1:
    {
    float u = expression;
    // ...
    break;
    }
    case 2:
    // ....
     
    Jim Langston, Sep 15, 2006
    #2
    1. Advertising

  3. vsgdp

    Jim Langston Guest

    "Jim Langston" <> wrote in message
    news:qnnOg.60$...
    > "vsgdp" <> wrote in message
    > news:u8nOg.9542$cw.2165@fed1read03...
    >> Why can't I define variables in a case statement:
    >>
    >> case 1:
    >> float u = expression;
    >> ...
    >> break;
    >>
    >>
    >> Also, is it bad form to cast ints to enums? For example, one might want
    >> 11 to map to an enum element NOVEMBER.

    >
    > case 1:
    > {
    > float u = expression;
    > // ...
    > break;
    > }
    > case 2:
    > // ....


    Sorry, missed the 2nd part of your question, re casting ints to enums.
    Casting a number as a month, would be okay *some of the time*. What
    happens, however, when the value is 13?

    enum Months
    {
    January = 1,
    February,
    March,
    // ...
    December
    }

    int Month = (something).

    Going from Months to Month wouldn't be a problem as such, as you can examine
    the value. if ( Month > 13 || Month < 1 ) { some error }.

    But when you go into a variable declared as Months
    Months EnumMonth = Month; // maybe some cast required

    What re you going to do if Month is 13? In a switch statement it shouldn't
    be that difficult as long as you remember to use default:

    I go from ints to enums all the time when serializing my classes (writing
    them to files or databases) but I ensure that the int value came from an
    enum, not from some other form of input (such as user input) without
    checking.

    I may have understood your question wrong, however. You may wish to
    rephrase it as to what it is you would actually want to do.
     
    Jim Langston, Sep 15, 2006
    #3
  4. vsgdp

    David Harmon Guest

    On Thu, 14 Sep 2006 18:19:53 -0700 in comp.lang.c++, "vsgdp"
    <> wrote,
    >Why can't I define variables in a case statement:
    >
    >case 1:
    > float u = expression;
    > ...
    > break;


    You cannot define variables in such a way that some code execution
    paths jump around them, leaving it ambiguous whether they would be
    defined or not. Oldy moldy switch/case is rather unstructured in
    that regard.

    If you want a local variable in a case, bracket it:

    case 1: {
    float u = expression;
    ...
    }
    break;
     
    David Harmon, Sep 15, 2006
    #4
  5. vsgdp

    Default User Guest

    David Harmon wrote:

    > On Thu, 14 Sep 2006 18:19:53 -0700 in comp.lang.c++, "vsgdp"
    > <> wrote,
    > > Why can't I define variables in a case statement:
    > >
    > > case 1:
    > > float u = expression;
    > > ...
    > > break;

    >
    > You cannot define variables in such a way that some code execution
    > paths jump around them, leaving it ambiguous whether they would be
    > defined or not. Oldy moldy switch/case is rather unstructured in
    > that regard.


    I think that's only the case when the declared variable is initialized.
    So this:

    switch(i)
    {
    case 1:
    float u = 0;
    break;

    case 2:
    break;

    }

    Requires a diagnostic, but if you remove the "= 0" part it would be ok.



    Brian
     
    Default User, Sep 15, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Franks
    Replies:
    2
    Views:
    1,268
    Steve Franks
    Jun 10, 2004
  2. Tee
    Replies:
    3
    Views:
    7,859
    Herfried K. Wagner [MVP]
    Jun 23, 2004
  3. Janice

    lower case to upper case

    Janice, Dec 10, 2004, in forum: C Programming
    Replies:
    17
    Views:
    1,198
    Richard Bos
    Dec 14, 2004
  4. Replies:
    1
    Views:
    2,505
    Mark P
    Apr 6, 2007
  5. darrel

    'case' vs. 'case is'

    darrel, Aug 14, 2008, in forum: ASP .Net
    Replies:
    3
    Views:
    283
    Rory Becker
    Aug 14, 2008
Loading...

Share This Page