enum

A

adrian suri

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
 
B

Bob Hairgrove

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

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)"
 
V

Victor Bazarov

adrian said:
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

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
 
I

Ivan Vecerina

: 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[] = ".ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int driveNb = 3;
cout << drvIdMap[driveNb]; // prints 'C'


Ivan
 
R

Richard Herring

adrian suri said:
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

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

Yes, if the numerical values are consecutive and bounded.

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Blue J Ciphertext Program 2
My Status, Ciphertext 2
Crossword 2
Fibonacci 0
How can I fix my pattern coding error in c++ 0
Something is wrong 1
Cannot find my infinite loop 1
How to play corresponding sound? 2

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top