enum

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • adrian suri

    #1

    enum

    Hi

    playing with enum, for example system DosCall return int values for disk
    partition so say I use an enum

    #include <iostream>

    using namespace std;
    int main()
    {
    enum drv { A=1, B, C,D,E,F,G,H,I,J ,K,L,M,N,O,P };

    char dv=drv(1);
    cout <<dv;
    }

    how can I get the letter from the number value char dv=drv(1); does not
    work, or is it best to use an array of char instead

    adrian
  • Bob Hairgrove

    #2
    Re: enum

    On Thu, 16 Feb 2006 15:49:33 +0100, adrian suri <asuri@tele2.se >
    wrote:
    [color=blue]
    >Hi
    >
    >playing with enum, for example system DosCall return int values for disk
    >partition so say I use an enum
    >
    >#include <iostream>
    >
    >using namespace std;
    >int main()
    >{
    >enum drv { A=1, B, C,D,E,F,G,H,I,J ,K,L,M,N,O,P };
    >
    >char dv=drv(1);
    >cout <<dv;
    >}
    >
    >how can I get the letter from the number value char dv=drv(1); does not
    >work, or is it best to use an array of char instead
    >
    >adrian[/color]

    Maybe like this?

    enum drv { A='A', B, C,D,E,F,G,H,I,J ,K,L,M,N,O,P };

    Actually, an array would probably serve you better because with an
    enum, you can't do "drv(1)"

    --
    Bob Hairgrove
    NoSpamPlease@Ho me.com

    Comment

    • Ivan Vecerina

      #3
      Re: enum

      "adrian suri" <asuri@tele2.se > wrote in message
      news:Yh0Jf.8$Ox 5.34@nntpserver .swip.net...
      : Hi
      :
      : playing with enum, for example system DosCall return int values for disk
      : partition so say I use an enum
      :
      : #include <iostream>
      :
      : using namespace std;
      : int main()
      : {
      : enum drv { A=1, B, C,D,E,F,G,H,I,J ,K,L,M,N,O,P };
      :
      : char dv=drv(1);
      : cout <<dv;
      : }

      In the above code, you actually cast the enum value to a char
      (ie. as if you are specifying its ASCII value), then try to
      print it. And character values <0x20 are not printable.

      But anyway, in C++, enums just print as integers.
      Enum-defined identifiers are only useful within
      the source code.

      : how can I get the letter from the number value char dv=drv(1); does not
      : work, or is it best to use an array of char instead

      On an ASCII-based platform, you could write:
      int driveNb = 3;
      cout << ( ('A'-1)+driveNb ); // prints 'C'

      Using an array of char, as you suggested, will indeed be more portable:
      char const drvIdMap[] = ".ABCDEFGHIJKLM NOPQRSTUVWXYZ";
      int driveNb = 3;
      cout << drvIdMap[driveNb]; // prints 'C'


      Ivan
      --
      http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
      Brainbench MVP for C++ <> http://www.brainbench.com


      Comment

      • Victor Bazarov

        #4
        Re: enum

        adrian suri wrote:[color=blue]
        > playing with enum, for example system DosCall return int values for disk
        > partition so say I use an enum
        >
        > #include <iostream>
        >
        > using namespace std;
        > int main()
        > {
        > enum drv { A=1, B, C,D,E,F,G,H,I,J ,K,L,M,N,O,P };
        >
        > char dv=drv(1);
        > cout <<dv;
        > }
        >
        > how can I get the letter from the number value char dv=drv(1); does not
        > work, or is it best to use an array of char instead[/color]

        There is no "letter" at run-time. The "letter" exists only in your code.
        You need to write your own conversion from 'drv' to 'char' if you need to
        get the "letter". Of course, a simpler way would be to get the value and
        simply add 'A' to it after subtracting 1:

        char dv = whatever -1 + 'A';

        Now, 1 would be mapped to 'A', 2 to 'B' and so on.

        V
        --
        Please remove capital As from my address when replying by mail

        Comment

        • Richard Herring

          #5
          Re: enum

          In message <Yh0Jf.8$Ox5.34 @nntpserver.swi p.net>, adrian suri
          <asuri@tele2.se > writes[color=blue]
          >Hi
          >
          >playing with enum, for example system DosCall return int values for disk
          >partition so say I use an enum
          >
          >#include <iostream>
          >
          >using namespace std;
          >int main()
          >{
          >enum drv { A=1, B, C,D,E,F,G,H,I,J ,K,L,M,N,O,P };
          >
          >char dv=drv(1);
          >cout <<dv;
          >}
          >
          >how can I get the letter from the number value[/color]

          You can't. The source-code names of C++ objects are not available at
          runtime.
          [color=blue]
          >char dv=drv(1); does not
          >work, or is it best to use an array of char instead[/color]

          Yes, if the numerical values are consecutive and bounded.

          If not, you might be better off using a std::map<int, char>.

          --
          Richard Herring

          Comment

          Working...