'How can a char value be converted to a specific int value

I have an assignment where I have to make a program that allows a person to input a seven letter word and converts it to a telephone number (1-800-PAINTER to 1-800-724-6837 for example). I'm trying to make each letter convert to a specific number to be outputted to the user, with each letter corresponding to its number on a telephone keypad (so a, A, b, B or c, C equals 1, i.e, more info: https://en.wikipedia.org/wiki/Telephone_keypad).

Currently I have it set up so that each letter of the input word represents a char variable of one, two, three, four, five, six, or seven respectively. Then, using switch and if statements, the idea was to convert a char to an int variable of xtwo = 2, xthree = 3, etc. This isn't working however. Is there a better way to do this?

Example of code (up to first switch, though mostly it's a repeating pattern like so):

int main()
{
    char one, two, three, four, five, six, seven;

    cout << "Enter seven letter word (1-800-***-****): " << "\n";

    cin >> one >> two >> three >> four >> five >> six >> seven;
    int xtwo = 2; int xthree = 3; int xfour = 4; int xfive = 5; int xsix = 6; int xseven = 7; int xeight = 8;
    int xnine = 9;

    switch (one)
    {
    case 1:
        if (one == 'a' || one == 'b' || one == 'c' || one == 'A' || one == 'B' || one == 'C')
        {
            one = xtwo;
        }
        break;
    case 2:
        if (one == 'd' || one == 'e' || one == 'f' || one == 'D' || one == 'E' || one == 'F')
        {
            one = xthree;
        }
        break;
    case 3:
        if (one == 'g' || one == 'h' || one == 'l' || one == 'G' || one == 'H' || one == 'L')
        {
            one = xfour;
        }
        break;
    case 4:
        if (one == 'j' || one == 'k' || one == 'l' || one == 'J' || one == 'K' || one == 'L')
        {
            one = xfive;
        }
        break;
    case 5:
        if (one == 'm' || one == 'n' || one == 'o' || one == 'M' || one == 'N' || one == 'O')
        {
            one = xsix;
        }
        break;
    case 6:
        if (one == 'p' || one == 'q' || one == 'r' || one == 's' || one == 'P' || one == 'Q' || one == 'R' || one == 'S')
        {
            one = xseven;
        }
        break;
    case 7:
        if (one == 't' || one == 'u' || one == 'v' || one == 'T' || one == 'U' || one == 'V')
        {
            one = xeight;
        }
        break;
    case 8:
        if (one == 'w' || one == 'x' || one == 'y' || one == 'z' || one == 'W' || one == 'X' || one == 'Y' || one == 'Z')
        {
            one = xnine;
        }
        break;
    }

So, in essence, how can a char variable of a letter be converted to a specific int variable?



Solution 1:[1]

You could use a std::map.

For example, you could have

std::map<char,int> char_to_dig {
  {'a',1}, {'b',1}, {'c',1},
  {'d',2}, {'e',2}, {'f',2}
};

Then

char_to_dig['a']

will give you 1.


Alternatively, you could write a function that does the mapping. Something along the lines of this:

int char_to_dig(char c) {
  static const char _c[] = "abcdefghi";
  static const int  _i[] = { 1,1,1,2,2,2,3,3,3 };
  for (unsigned i=0; i<9; ++i) {
    if (_c[i]==c) return _i[i];
  }
  return -1; // some value to signal error
}

Or, instead of using arrays, you could perform arithmetic on the chars (since they are just small integers).

int char_to_dig(char c) {
  c = std::toupper(c);
  if (c < 'A' || c > 'Z') return -1;
  if (c == 'Z') return 9;
  if (c > 'R') --c;
  return ((c-'A')/3)+2;
}

This will give you numbers like on this pad:

enter image description here


Apparently, there's been a similar code golf question.

Solution 2:[2]

It has been years since I wrote any c/c++ code and I don't even have a compiler installed to test with .. but this should get you started on the right track Check functions and syntax...all out of my head. Needs checking. //

int numArray[7];
char inputStr[10]; 
cout << " give me 7 characters";
cin >> input;

/*
use a for loop to read the string letter by letter (a string in c is an 
array of characters)
convert the characters to uppercase 
fall through case statements for each group of letters
assing value to output array to do wiht as you like.
*/


for(i=0; i < 7; i++){
    inputStr[i] = toupper(inputStr[i]);
    switch(input[i]){
      case 'A':
      case 'B':
      case 'C':
        numArray[i] = 2;
        break;
      case 'D':
      case 'E':
      case 'F':
        numArray[i] = 3;
        break;

    and so on and so foth....


      }

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2