I have the following code:

#include <iostream>
using namespace std;


int main () {


char alphabet[]="abcdefghijklmnopqrstuvwxyz";
char word;
cout <<"Please input a word: " <<endl;
cin >> word;


int res = -1;


for (int i = 0; alphabet != '\0'; i++)
{
if (alphabet == word)
{
res = i;
}
}
if (res != -1)
{
cout << word << " has the number " << res << " in the array \n";
}
}

//Thanx to dave sinkula and zyruz for their help so far

What i want to do is to input a word and get as output a number - the problem is that i dont know how to assign my own values (numbers) to the char array. For instance i dont want a to be 0, b to be 1 etc - i want a to be 17, b to be 23 and so on - what i want to do is to assign to each letter my own value.

Any suggestions?

This is a somewhat slow solution because you end up with an expensive search, but it does what you want. The alphabet string holds all of your valid characters and the values array has the corresponding value at each index. Once you have the index for alphabet, you can get the value from values:

#include <iomanip>
#include <iostream>

int main()
{
  char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
  int values[] = {
    25,24,23,22,21,20,19,18,
    17,16,15,14,13,12,11,10,
    9,8,7,6,5,4,3,2,1,0
  };

  char word[10];

  std::cout<<"Enter a word: ";
  std::cin>> std::setw ( 10 ) >> word;

  for ( int i = 0; word[i] != '\0'; i++ ) {
    for ( int j = 0; alphabet[j] != '\0'; j++ ) {
      if ( word[i] == alphabet[j] )
        std::cout<< values[j] <<' ';
    }
  }
}

Thanx Narue - That is exaclly what i wanted!
P.S: I think u are the most pretty woman programer i saw so far;)
Thanx again!

Couldn't you have just used the ascii value and then subtracted to get its value in the alphabet?

Sorry my bad, i didn't read well enough. Your either writing a hash routine or weak encryption program aren't ya?

>Couldn't you have just used the ascii value and then subtracted to get its
>value in the alphabet?
Even if that were a solution, it would still be a poor one. You would be making a non-portable assumption about the character set. This trick is only guaranteed to work with the digit characters, '0' through '9'.

>Your either writing a hash routine or weak encryption program aren't ya?
My bet is for encryption.

If you're allowed to do other encryption schemes, just use the XOR operator with a character mask. Byte-level mixing is quick and dirty.

Pretty easy to crack, though.

>Pretty easy to crack, though.
Actually, if done correctly, the XOR encryption can be used for a one-time pad, which is virtually impossible to crack.

Well, I suppose it could. My imagination probably isn't implementing it correctly, though.

I'm just going with my limited experience in wireless communications... all the class had to do was a simple encryption scheme so the master could talk to the slave robot and transmit simple information packets. So, we didn't get very complex with it.

Ok now what i want to do is for every word that will be entered, to be added with the next word that will be inputed. Example: if i enter the word 'b' the output will be '24' - but if i continue entering words for instance i enter the letter 'e' which results to '21' in our case - i want 24 and 21 to sum up and give me a total of 45. More than that how can i make in c++ to recognize the 'space' character?I want this because i want to input a whole phrase, like: "I am a new programmer" and i want c++ to recognize the space characters and add each letter that i entered with its next one...! I hope i made myself clear on what i want! thanx in advance again for ur intrest to help

>how can i make in c++ to recognize the 'space' character?
Just add a space to the alphabet and a corresponding value, same as all of the other characters you want to recognize.

ok beauty! thanx

But how can i add the values? for instance if i type: "I am new" - how can i make it to sum up all the values together ?

Simple, just keep a running total through the loop:

int sum = 0;

for ( int i = 0; word[i] != '\0'; i++ ) {
  for ( int j = 0; alphabet[j] != '\0'; j++ ) {
    if ( word[i] == alphabet[j] )
      sum += values[j];
  }
}

Thanx again! I am sure that when u have grandchildren they will be proud about you for indeed u are a usefull person to humanity ;) by the way have u ever been in Greece?

Not to my knowledge.

Not to my knowledge.

Well if u ever decide to come let me know!! Thanx again

what do u mean by

just keep a running total through the loop:

; i try to add and replace the coding with

 for ( int i = 0; word[i] != '\0'; i++ ) {
    >   for ( int j = 0; alphabet[j] != '\0'; j++ ) {
    >     if ( word[i] == alphabet[j] )
    >       sum += values[j];
    >   }
    > }

but it didnt working..

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.