Something like this:

using namespace std;
// the function below takes an integer and outputs a corresponding letter
void convert (int num) 
int main
   int num;
   cout << "Enter an integer" << endl;
   cin >> num;
   convert (num); 
   return 0;

Input: 0 , Output: "A''
Input: 1 , Output: "B''
Input: 25 , Output: "Z''
Input: 26 , Output: "A'' (repeats the alphabet again)
Input: 2033, Output: ''F''

I've thought of using a switch in the function convert but of course there are too many integers ):
So what approach can I use? I'd rather not use arrays I haven't learnt them yet.

11 Years
Discussion Span
Last Post by roflol

Take advantage of the fact that characters are represented by integer values on your computer, and therefore may be added to other integer types.

char convert(int i)
    return (i%26) + 'A';

EDIT: Note - This depends on the character set on your computer having the letters 'A'..'Z' as a contiguous set. As far as I am aware, most character sets do (such as ASCII), but there is always a possibility that some obscure character set somewhere in the world doesn't.

EDIT2: For the more portable solution using the <string> library

char convert(int i)
    return s.at( i % s.size() );

Ah ok thanks very much for that. First I thought adding an integer to "A" wasn't possible but then I saw that only single quotation marks were used. So initializing any variable of type char requires single quotation marks? I'm sorry as I've only used numbers before.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.