Hi,
I am developing a program in c++ and what i want to do is to assign an integer to a character. To become more specific i want to make an alphabet and assign to each letter a number. So far so good. The problem is that i want it with user input. For instance see the code below:

int main () {
   const unsigned int a=1, b=2, c=3, d=4, e=5, f=6, g=7; //and so on..
   int input;
   cout << "Please enter a word: " <<endl;
   cin >> input;
   cout << "The output is: " << ...

<< moderator edit: added [code][/code] tags, reduced indent >>

For example if the user decides to type the word 'bad' i want the output to be displayed in numbers: in our example that would be: 2 1 4 which stands for b a d...
I know it sound easy but i am kinda new to c++ and i dont know even what to look for in the net to concerning this issue, so any help would be greatly appreciated.

--Thanx in advance--

I'd use an array so you could use subtraction to find the letter.

#include <iostream>

int main ( void )
{
   static const char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
   int input;
   while ( std::cin >> input )
   {
      std::cout << alphabet [ (input - 1) % (sizeof alphabet - 1) ] << '\n';
   }
   return 0;
}

/* my output
2 1 4
b
a
d
^C
*/

The % (sizeof alphabet - 1) is a safety measure.

Go to google.com and do an image search for 'ASCII Table'.
The images you'll find will show you the decimal representation that computers generally use (by default with C/C++, I believe) to represent letters. For example, the decimal representation of A is 55 or 56. Can't remember which right now.

So, using that table (And assuming that the user keeps to one case (upper or lower)), you can just do a little math on the characters entered, and get your output without assigning all the values by hand.

It will help you greatly if you put the following in your code:

#include <strings>

or

#include "strings.h"

if the first one doesn't work.
Those let you use the 'string' data class, so you can store a word that's been typed in.
Right now, you're trying to assign a word to a single int (which means integer, as in 1, 2, 3, etc.), which just won't work, on several levels.
Make input into:

string input;

when you declare it. That will let you store words that are typed in.
Alternatively, you could use an array of characters to store the information... Not sure if you have to do anything else to get that working with cin.

After that, you need to look up how to access a string character-by-character, and perform the conversion using the ASCII method.
Or, you can use enum, but that's a bit advanced for the moment.
Good luck.

ok problem solved! Thank u Dave - i hope one day i will reach the level of you expertice in c++ !!!
Also thank you Drowzee for u time explaining me..

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 input b a d and it would display me the corresponding numbers. The opposite of what dave sinkula did...

--Thanx in advance--

Input a char, loop through the array and see if it matches a character. If it does, a loop index would give you a numeric value.

Dave please look the new thread i created - i explain it better there on what i want - and please if it is easy and not time wasting for you please be more detailed on how to acheive the things i asked because it sounds a bit hard to me to implement what you say in one line - i'd be greatfull if u could provide examples as u explain - thanx again

If I aint totaly wrong Dave ment somthing like this:

char ch[] = "abcdefghijklmnopqrstuvwxyz"; // character array
    char testc = 'c'; // wath we are looking for
    int res = -1;
    for (int i = 0; ch[i] != '\0'; i++) // loop thru ch, untill it reach the end.
    {
        if (ch[i] == testc) // if we found the char
        {
            res = i; 
        }
    }
    if (res != -1) // dont print out if you dident find it.
    {
        std::cout << testc << " has the number " << res << "in the array \n";
    }
This article has been dead for over six months. Start a new discussion instead.