assign letters to random number

does anybody know how to assign uppercase letters to random numbers i.e 65-91? eg. 65 to A, 66 to B etc. my question paper says to "generate an element in between the range from 65 to 91 and cast it, you can get any letter from ā€˜Aā€™ to ā€˜Zā€™.'

i've seen some places said to put the letters in an array then assign numbers to the letters. is that the only way to do it? is there another way? if so, how do i go about assigning? and what does it mean by "cast it"?
Last edited on
Are you sure about the random part? The ascii code for 'A' is 65. Or 0x41 (in hex , which is easier to look up in in an ascii code table). And the code for 'Z' is 90. Or 0x5A.


#include <iostream>
using namespace std;

int main()
    char ch = 'O';
    int i = 75;

    // cast char to int
    cout << ch << " = " << (int)ch << endl;

    // cast int to char
    cout << (char)i << " = " << i << endl;

    return 0;

This code uses C-style casts. With C++ you can also use a function style casts like char(i) instead of (char)i. But this doesn't work with types like char*.

And there are the C++ cast operators -- static_cast<>, dynamic_cast<>, reinterpret_cast<> and const_cast<> -- which require more typing but are safer to use as they're more specific (e.g. only const_cast<> can remove const-ness). For example:

cout << static_cast<char>(i) << " = " << i << endl;


PS "Type conversion" (== casting)

Last edited on
I'm an extreme novice at C++. I'll tell you what I've learned in class so far.

One way I learned to make sure any letter entered is uppercase is to use an assignment something like this: letter = toupper(letter); That will make any lowercase letter also work as an uppercase letter. So, if a char variable should be entered as A, if you use the toupper command, it doesn't matter if you enter it as a lowercase or an uppercase A.

So far all I've learned about casting is using static_cast operator to avoid implicit conversions. The static_cast operator forces an explicit conversion. Compilers will either promote or demote between data types. Usually promotion doesn't cause a problem, but demotion does.
Topic archived. No new replies allowed.