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.

Dani AI

Generated

You can treat letters as numbers: in most common encodings (e.g., ASCII), the uppercase alphabet is contiguous, so you can map an index 0..25 to a letter by offsetting from 'A'. To make it robust, normalize the input into the 0..25 range and guard the assumption that 'A'..'Z' are contiguous. Also note that character literals use single quotes (e.g., 'A') and create a single char, while double quotes create a C-style string (e.g., "A" is an array of chars). See cppreference: character literal.

char convert(int num)
{
    // Normalize to [0, 25] even for negative inputs
    int offset = num % 26;
    if (offset < 0) offset += 26;

    // Ensure the implementation has contiguous 'A'..'Z'
    static_assert('Z' - 'A' == 25, "Noncontiguous alphabet; use a lookup mapping here.");

    return static_cast<char>('A' + offset);
}

Why normalize? In C++, the remainder keeps the sign of the dividend, so -1 % 26 == -1. The adjustment above ensures wrap-around works for negatives too. Details: cppreference: arithmetic operators.

Portability note: If you ever target a platform with a noncontiguous uppercase alphabet (e.g., certain EBCDIC code pages), the static_assert will fail; in that case, replace the final line with a lookup from a constant alphabet string. Background on EBCDIC: IBM docs.

Recommended Answers

All 3 Replies

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() );
}

See EBCDIC for an example of a character set that doesn't have consecutive A..Z.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.