Using a char variable in a array position

Is it possible to use a char variable to point to a position in an array

For Example,

<code>

char position;

std::cin >> position;

char array[] = { 'A', 'B', 'C', 'D' };

std::cout << array[ position ];

</code>

I think I understand why it doesn't work.

Its because a number in a char is not the same number, i.e. '1' = 49, '2' = 50, but is it possible to convert it to an integer




Answer this question

Using a char variable in a array position

  • Mark Henneman

    I am not quite sure what you want to do, but you may convert a Char to a byte by means of:

    Byte = Asc(Char).

    Then you can use the byte as an index in the array.


  • KevinKing

    Sorry my mistake. I forgot I was in the Visual Basic forum

    Thanks Carsten Kanstrup, I will give that a go.



  • Patrice Calve

    stuart.marsh wrote:

    Is it possible to use a char variable to point to a position in an array

    For Example,

    <code>

    char position;

    std::cin >> position;

    char array[] = { 'A', 'B', 'C', 'D' };

    std::cout << array[ position ];

    </code>

    I think I understand why it doesn't work.

    Its because a number in a char is not the same number, i.e. '1' = 49, '2' = 50, but is it possible to convert it to an integer

    Well, since this is a Visual Basic forum, it might be easier if you asked this in a C++ or C# forum. And my C++ coding is too rusty to be of any help to you. Try asking in the correct group and you will get your answer pretty fast.

    james

    aka;Trucker


  • Using a char variable in a array position