ok - challenge number 2: What now if i would like to input letters and the letters would be displayed as numbers?
For instace i would like to input b a d and it would display me the corresponding numbers. The opposite of what Mr.Dave Sinkula (look thread:Need help with integers in c++) did in the previous thread...Moreover, what if i would like to assign my own numbers to the letters...?
For those who see this thread first time, please look first at thread "Need help with integers in c++"

--Thanx in advance--

Recommended Answers

All 6 Replies

anyone, please?

Why are you so impatient?

Make an array of integers of length 256. Maybe named 'nums'. Use the character as the index; to get the number for 'x', you'd write nums. You'll need to initialize the values of the array of course.

And pray that your characters are eight bits. This method does not scale too well for larger character sizes. If your characters are nine bits wide, you'll need an array of integers of length 512. If 16 bits wide, 65536. So stick with 256 and just check to make sure your character is less than 256.

How a correct initialazation of arrays would be correct?
static const char alphabet[] = "a=10, b=25, c=30"; //and so on
i get a compiler error when i initialize like this...

anyway - by trial and error i hope i find the answer
thanx for ur time Rashakil

Initialize arrays like this:

int some_primes[9] = {2, 3, 5, 7, 11, 13, 17, 19, 23};

How a correct initialazation of arrays would be correct?
static const char alphabet[] = "a=10, b=25, c=30"; //and so on
i get a compiler error when i initialize like this...

You can do it like this:

#include <iostream>
struct alphab
{
    int num;
    char ch;
};

int main()
{
    alphab al[] = {{1,'a'},{2,'b'},{3,'c'}}; // etc.
    int n = 1; // testing
    std::cout << al[n].ch << std::endl; //shall print out b.
}
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.