Something like this:

#include<iostream>
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); 
 
   system("pause");
   return 0;
}

E.g.
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.

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)
{
    std::string s("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    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 article has been dead for over six months. Start a new discussion instead.