how to output alphabet with decimal input

I need help on a very simple program,

how to I write a code with the following output:

If user enters decimal integer 6, it should give the output F
If user enters decimal integer 7, it should give the output G
If user enters decimal integer 8, it should give the output H
So on and so of for all 26 alphabet

Thanks
You can use the fact that, in ASCII (which all normal consumer computers use), the capital English letters are contiguous and have numeric, integer values.
For example, 'A' + 1 == 'B'.

http://www.asciitable.com/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()
{
    int user_input = 6;

    // 1 --> A, 2 --> B, 3 --> C, 4 --> D, 5 --> E, etc.
    if (user_input < 1 || user_input > 26)
    {
        // ... handle error
    }
    else
    {
        std::cout << (char)('A' + user_input - 1) << '\n';
    }
}
Last edited on
thank you for your help!
Topic archived. No new replies allowed.