rEgonicS
rEgonicS

Reputation: 211

How to retrieve an integer value from a character?

How can I convert a character into an integer value? For example, I've been trying to read "A" as 1, "B" as 2, and so forth. I tried comparing the character to each letter in the alphabet and return the appropriate value.

int intvalue(char letter)
{
if(letter == "A")
    return 1;
else if(letter == "B")
    return 2;
else if(letter == "C")
    return 3;
else if(letter == "D")
    return 4;
else if(letter == "E")
    return 5;
else if(letter == "F")
    return 6;
else if(letter == "G")
    return 7;
else if(letter == "H")
    return 8;
else if(letter == "I")
    return 9;
else if(letter == "J")
    return 10;
else if(letter == "K")
    return 11;
else if(letter == "L")
    return 12;
else if(letter == "M")
    return 13;
else if(letter == "N")
    return 14;
else if(letter == "O")
    return 15;
else if(letter == "P")
    return 16;
else if(letter == "Q")
    return 17;
else if(letter == "R")
    return 18;
else if(letter == "S")
    return 19;
else if(letter == "T")
    return 20;
else if(letter == "U")
    return 21;
else if(letter == "V")
    return 22;
else if(letter == "W")
    return 23;
else if(letter == "X")
    return 24;
else if(letter == "Y")
    return 25;
else if(letter == "Z")
    return 26;

}

I got "error: ISO C++ forbids comparison between pointer and integer". Does anyone know how to fix this? Or even better, a different way to go about this? I feel like my above function is very brute-forceish.

Upvotes: 3

Views: 10892

Answers (6)

Ziezi
Ziezi

Reputation: 6467

A possible approach is just using an explicit cast from char to int:

#include <iostream>

int main()
{
    char c;
    std::cout << "Enter a character: ";
    std::cin >> c; // for more than one chars you may need to use getline() or ignore()

    std::cout << "ASCII value of: " << c << ", is: " << int(c) <<".\n";
}

Input:

p

Output:

ASCII value of: p, is: 112.

Upvotes: 0

Thomas Matthews
Thomas Matthews

Reputation: 57728

A more portable implementation involves searching an array of characters and using the index as your value:

int Value_From_Char(char c)
{
  static const char valid_letters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  const std::string letter_str(valid_letters);

  // Search the letters for the given char.
  std::string::size_type  position = 0;
  position = letter_str.find(c);
  int result = 0;
  if (position == std::string::npos)
  {
    result = -1;
  }
  else
  {
    result = (int) position;
  }
  return result;
}

This function will work with UTF formats as well. This method makes no assumptions about the collating sequence.

Upvotes: 1

JSBձոգչ
JSBձոգչ

Reputation: 41378

In C++, a char is a number. So there's no need for an elaborate method like above: you can just use the char directly. If you want to make it based with A=1 as shown above, then you can do:

int intValue(char value)
{
    return (value - 'A') + 1;
}

As for the error you're getting, in C/C++, a double-quoted string is a char* (or, more accurately, a char[], but the difference isn't important here). That's why you're getting an error: you're trying to compare between a char and a char*.

Upvotes: 5

Michael Mrozek
Michael Mrozek

Reputation: 175595

Characters and their ASCII value integers are interchangeable. The character's ASCII value minus 'A's ASCII value would be 0, so adding 1 would give you 1:

return letter - 'A' + 1;

Technically you could also just subtract '@' (the character before 'A' in the table), but I think it's probably less clear. Your problem is you were using "A" instead of 'A'; the former is a string (technically a pointer to a character), while the latter is a single character

Upvotes: 1

James McNellis
James McNellis

Reputation: 355197

You need to use character literals, not string literals, e.g.,

if (letter == 'A')
              ^ note the single quotes

That said, if you are willing to assume you are running on a system using the ASCII character set, you can simply use arithmetic:

int charIndex = (letter - 'A') + 1;

In the ASCII character set, letters are at consecutive indices, so this works. This may not work with other character sets.

Upvotes: 10

Stephen
Stephen

Reputation: 49206

Try this:

return letter - 'A' + 1;

(Although you may want to handle an out-of-range letter input)

Note that char is a single character, which uses single quotes: 'A'. Strings are multiple characters and use double quotes: "ABC".

You can treat characters as integers (their ascii values) and manipulate them.

Upvotes: 1

Related Questions